View mode: basic / threaded / horizontal-split · Log in · Help
July 24, 2012
Take and website
I am wondering if the examples at
http://dlang.org/phobos/std_range.html#take need some attention. As far
as I can see they only work because the input is a list. If you take
from the result of an algorithm such as recurrence then you have to
explicitly create an array from the result of the take in order for the
[] operator to be defined.

-- 
Russel.
=============================================================================
Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder@ekiga.net
41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder
July 24, 2012
Re: Take and website
On 7/24/2012 10:10 AM, Russel Winder wrote:
> I am wondering if the examples at
> http://dlang.org/phobos/std_range.html#take need some attention. As far
> as I can see they only work because the input is a list. If you take
> from the result of an algorithm such as recurrence then you have to
> explicitly create an array from the result of the take in order for the
> [] operator to be defined.

I don't know about that specifically, but I've been trying to do some range 
based programs and have found that Phobos in general needs to be gone through 
and "range-ified".

Ranges are a huge deal for D, perhaps even a killer feature, and Phobos ought to 
be a showcase for them (like STL is for C++).

(Why a killer feature? Ranges make "snap together" component programming 
actually work in D.)
July 24, 2012
Re: Take and website
On 7/24/12 1:10 PM, Russel Winder wrote:
> I am wondering if the examples at
> http://dlang.org/phobos/std_range.html#take need some attention. As far
> as I can see they only work because the input is a list. If you take
> from the result of an algorithm such as recurrence then you have to
> explicitly create an array from the result of the take in order for the
> [] operator to be defined.

The example is:

int[] arr1 = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
auto s = take(arr1, 5);
assert(s.length == 5);
assert(s[4] == 5);
assert(equal(s, [ 1, 2, 3, 4, 5 ][]));

Were you referring to this? Example code does not need to be generic, 
and in this case it's fine if the code relies on random access because 
it uses an array of integers.


Andrei
July 24, 2012
Re: Take and website
On Tue, 2012-07-24 at 13:56 -0400, Andrei Alexandrescu wrote:
[…]
> The example is:
> 
> int[] arr1 = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
> auto s = take(arr1, 5);
> assert(s.length == 5);
> assert(s[4] == 5);
> assert(equal(s, [ 1, 2, 3, 4, 5 ][]));
> 
> Were you referring to this? Example code does not need to be generic, 
> and in this case it's fine if the code relies on random access because 
> it uses an array of integers.

That's the one.

s[4] relies on the fact that arr1 is an array:

( takeExactly ( recurrence ! ( "a[n-1] + a[n-2]" ) ( 0L , 1L ) , cast
( size_t ) ( n + 1 ) ) ) [ n ]

fails with operator [] not defined, I find I have to:

array ( takeExactly ( recurrence ! ( "a[n-1] + a[n-2]" ) ( 0L , 1L ) ,
cast ( size_t ) ( n + 1 ) ) ) [ n ] 

So the functions available on the results of a take depends on the data
input. In one sense obvious, in another sense a total fubar since it is
not necessarily clear what the types are in more complex situations.

-- 
Russel.
=============================================================================
Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder@ekiga.net
41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder
July 24, 2012
Re: Take and website
On 7/24/12 2:17 PM, Russel Winder wrote:
> That's the one.
>
> s[4] relies on the fact that arr1 is an array:
>
>   ( takeExactly ( recurrence ! ( "a[n-1] + a[n-2]" ) ( 0L , 1L ) , cast
> ( size_t ) ( n + 1 ) ) ) [ n ]
>
> fails with operator [] not defined, I find I have to:
>
> array ( takeExactly ( recurrence ! ( "a[n-1] + a[n-2]" ) ( 0L , 1L ) ,
> cast ( size_t ) ( n + 1 ) ) ) [ n ]
>
> So the functions available on the results of a take depends on the data
> input. In one sense obvious, in another sense a total fubar since it is
> not necessarily clear what the types are in more complex situations.

I must have gotten a bit too used to it, but I think that's quite a 
basic reality imposed by the types involved... yes, that's as clear as 
xyz = 4.5 only works if xyz is of some certain types.

Anyhow, an example that shows how to print the nth Fibonacci number will 
be helpful. Could I impose on you to write a pull request?


Thanks,

Andrei
July 24, 2012
Re: Take and website
On Tue, 2012-07-24 at 14:21 -0400, Andrei Alexandrescu wrote:
[…]
> I must have gotten a bit too used to it, but I think that's quite a 
> basic reality imposed by the types involved... yes, that's as clear as 
> xyz = 4.5 only works if xyz is of some certain types.

I think the point here is that the type of the underlying data structure
passes through the various "wrappers". This definitely is not a problem
as long as it is clear to the programmer. The current examples are fine
but just not giving wide enough coverage to make this point.

> Anyhow, an example that shows how to print the nth Fibonacci number will 
> be helpful. Could I impose on you to write a pull request?

Having raised the issue, I am duty bound to deliver :-) But it may take
a while. Not Deep Though type "a while", but a few days.

Is the website documentation generated from the Phobos source or is it a
separate  repository? (He says giving away his green-ness at the D
community infrastructure!)

-- 
Russel.
=============================================================================
Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder@ekiga.net
41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder
July 24, 2012
Re: Take and website
On Tue, Jul 24, 2012 at 11:47 AM, Russel Winder <russel@winder.org.uk> wrote:
> On Tue, 2012-07-24 at 14:21 -0400, Andrei Alexandrescu wrote:
> […]
> Is the website documentation generated from the Phobos source or is it a
> separate  repository?

That particular example should be in the ddoc block for that
function/template in the source code. Those pages are automatically
generated using make and dmd.
July 24, 2012
Re: Take and website
On 7/24/12 2:47 PM, Russel Winder wrote:
> On Tue, 2012-07-24 at 14:21 -0400, Andrei Alexandrescu wrote:
> […]
>> I must have gotten a bit too used to it, but I think that's quite a
>> basic reality imposed by the types involved... yes, that's as clear as
>> xyz = 4.5 only works if xyz is of some certain types.
>
> I think the point here is that the type of the underlying data structure
> passes through the various "wrappers". This definitely is not a problem
> as long as it is clear to the programmer. The current examples are fine
> but just not giving wide enough coverage to make this point.

A fair point.

>> Anyhow, an example that shows how to print the nth Fibonacci number will
>> be helpful. Could I impose on you to write a pull request?
>
> Having raised the issue, I am duty bound to deliver :-) But it may take
> a while. Not Deep Though type "a while", but a few days.

http://i3.kym-cdn.com/photos/images/newsfeed/000/159/325/1262055260350.jpg

> Is the website documentation generated from the Phobos source or is it a
> separate  repository? (He says giving away his green-ness at the D
> community infrastructure!)

Straight from the Phobos source. For a quick edit, just go straight here:

https://github.com/D-Programming-Language/phobos/edit/master/std/algorithm.d


Andrei
July 24, 2012
Re: Take and website
On Tue, 24 Jul 2012 19:47:42 +0100
Russel Winder <russel@winder.org.uk> wrote:
> Is the website documentation generated from the Phobos source or is
> it a separate  repository? (He says giving away his green-ness at the
> D community infrastructure!)
> 

The Phobos documentation is generated from the Phobos source. The rest
of the website is in the d-programming-language.org repo.
July 25, 2012
Re: Take and website
Russel Winder , dans le message (digitalmars.D:173102), a écrit :
> 
> --=-aHxuwwF1pyt7fCGYFQXP
> Content-Type: text/plain; charset="UTF-8"
> Content-Transfer-Encoding: quoted-printable
> 
> On Tue, 2012-07-24 at 13:56 -0400, Andrei Alexandrescu wrote:
> [=E2=80=A6]
>> The example is:
>>=20
>> int[] arr1 =3D [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
>> auto s =3D take(arr1, 5);
>> assert(s.length =3D=3D 5);
>> assert(s[4] =3D=3D 5);
>> assert(equal(s, [ 1, 2, 3, 4, 5 ][]));
>>=20
>> Were you referring to this? Example code does not need to be generic,=20
>> and in this case it's fine if the code relies on random access because=
> =20
>> it uses an array of integers.
> 
> That's the one.
> 
> s[4] relies on the fact that arr1 is an array:
> 
>  ( takeExactly ( recurrence ! ( "a[n-1] + a[n-2]" ) ( 0L , 1L ) , cast
> ( size_t ) ( n + 1 ) ) ) [ n ]
> 
> fails with operator [] not defined, I find I have to:

This is expressed in the doc, not in the example:
> If the range offers random access and length, Take offers them as 
> well. 

recurrence does not offer random access, so take!recurrence does not. 
You may try to make sentence this clearer, but it's pretty clear to me.

-- 
Christophe
Top | Discussion index | About this forum | D home