List comprehensions in D?

Oskar Linde oskar.lindeREM at OVEgmail.com
Thu Jun 29 09:13:20 PDT 2006


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.

> 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, 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



More information about the Digitalmars-d mailing list