if-expressions + list comprehensions

Leandro Lucarella llucax at gmail.com
Thu May 31 11:02:49 PDT 2007


Daniel Keep, el  1 de junio a las 02:44 me escribiste:
> 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

it's a question of precedence, just like bs = a * a, which could mean (bs
= a) * a or bs = (a * a).

Python use that syntax (bs = a * a for a in as if a % 2) and it's not
ambiguous.

The last varian is not possible because (in python) there's no
"expression" if without an else (and there is no possible else in
"expression" for).

-- 
LUCA - Leandro Lucarella - Usando Debian GNU/Linux Sid - GNU Generation
------------------------------------------------------------------------
E-Mail / JID:     luca at lugmen.org.ar
GPG Fingerprint:  D9E1 4545 0F4B 7928 E82C  375D 4B02 0FE0 B08B 4FB2 
GPG Key:          gpg --keyserver pks.lugmen.org.ar --recv-keys B08B4FB2
------------------------------------------------------------------------
Every day 21 new born babies will be given to the wrong parents



More information about the Digitalmars-d mailing list