if-expressions + list comprehensions

Daniel Keep daniel.keep.lists at gmail.com
Thu May 31 09:44:39 PDT 2007



Leandro Lucarella wrote:
> Daniel Keep, el 29 de mayo a las 12:19 me escribiste:
>> double[] as = [1.,2.,3.,4.,5.];
>> double[] bs = foreach(a;as) a*a;
>>
>> List comprehensions would be cooler still, but I'm not holding my breath
>> on that one :P
> 
> More pythonic:
> 
> double[] bs = a * a foreach(a; as) if (a % 2);
> 
> would give you filtering too (leaving bs = [1.0, 9.0, 25.0]) =)

Problem with that one is that it's ambiguous.  If we pretend that the
"double[]" isn't there (for the sake of argument), then it could
legitimately mean:

(bs = a * a) foreach(a; as) if (a % 2);

bs = (a * a) foreach(a; as) if (a % 2);

bs = a * (a) foreach(a; as) if (a % 2);

(bs = a * (a) foreach(a; as)) if (a % 2);

Which is one reason why I'm not a big fan of postfix keywords :P

-- 
int getRandomNumber()
{
    return 4; // chosen by fair dice roll.
              // guaranteed to be random.
}

http://xkcd.com/

v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/



More information about the Digitalmars-d mailing list