List comprehensions in D?

David Medlock ashleymedlock at no.spam.yahoo.com
Sun Jun 25 10:13:46 PDT 2006


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.
> -DavidM

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 ); }

x = vector[ (int t){return t>5;} ]; // subset of vector
vector[] = (inout int x) { x += 100; } ; // update all members of vector

And of course extending to opSlice/opSliceAssign....

-DavidM



More information about the Digitalmars-d mailing list