List comprehensions in D?
David Medlock
noone at nowhere.com
Thu Jun 29 12:10:29 PDT 2006
Oskar Linde wrote:
> David Medlock wrote:
>
>> David Medlock wrote:
>>
>>> My meager attempts to clone some list functionality in Python:
>>>
>>> r = [ y for y in array if y > 10 ];
>>
>> <snip>
>>
>>> Pretty cool, IMO.
>
>
> Yes, the new delegate syntax is very convenient. The above functions are
> also in my std.array proposal (under different names). Your update
> function with an inout argument instead of my suggested doMap with a
> pure functional argument is interesting:
>
> arr.update((inout int x){ x++; });
> vs
> arr.doMap((int x){ return x+1; });
>
> A smart implementation would be able to support both versions in one
> function. I wounder if that is appropriate. It would be helpful if there
> were any way to tell if the arguments of a delegate type were
> in/out/inout. I guess some .mangleof hackery could help there.
I'm not sure, but since arrays are by reference I would prefer to make
update functions named differently (sort and reverse still bite me on
occasion).
>
>> in addition,
>>
>> If you consider that arrays are just machine-optimized versions of
>> maps(or graphs) with the allowed index as integers between 0 and N,
>> then passing a predicate function on those indexes makes some sense.
>>
>> for containers: (depending on your view of the syntax)
>>
>> T[] opIndex( bool delegate(T) dg ) { return items.where( dg ); }
>> T[] opIndexAssign( void delegate( inout T) dg ){ items.update( dg ); }
>
>
> In my view of the syntax, opIndex(bool delegate) should return a view,
> rater then a new array with copied elements,
Do you mean a container which transforms the elements passed through
opIndex using the delegate? That could be an issue with the delegate
going out of scope.
Otherwise I don't see how you could do it without making a new array.
similar to how a slice of
> an array returns a view rather than copied elements. A combined
> select/update would also be cool:
>
> void opIndexAssign(T delegate(T) updater, bool delegate(T) selecter)
>
> /Oskar
Yes combined functionality is nice and getting close to the list
comprehensions i covet from python.
I also toyed with the idea of using the (just added) opIn operator.
int[] result = container in (int a) {return a>5;};
But this seemed too 'out of the ordinary'.
PS. I will be glad to see your array lib in phobos.
-DavidM
More information about the Digitalmars-d
mailing list