Intrinsics, std.bind problems, list comphrensions, recursivity test

Downs default_357-line at yahoo.de
Fri Aug 24 01:56:42 PDT 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

FWIW, you can express those in D in a similar fashion using the
tools.iter module I posted over in d.D ..

bearophile wrote:
> print [x*x for x in xrange(16) if x & 1]
writefln(Integers[0..16]~filters!("_&1")~maps!("_*_")~toArray);
> # Prints: [1, 9, 25, 49, 81, 121, 169, 225]
> 
> print [x+y for x in xrange(5) if x & 1 for y in xrange(5)]
The code for doing this (tscross) only exists on my local version, and
looks too ugly to post. But it works.
writefln(Integers[0..5]~filters!("_&1")
  ~tscross({return Integers[0..5]; })
  ~maps!("_.values[0]+_.values[1]")~toArray);
> # Prints: [1, 2, 3, 4, 5, 3, 4, 5, 6, 7]
> 
> print [[x+y for x in xrange(3)] for y in xrange(3)]
// Can't use simplified syntax for this one. Dangit.
writefln(Integers[0..3]~map((int e) {
  return Integers[0..3]~map((int f) {
    return f+e;
  })~toArray;
})~toArray);
> # Prints: [[0, 1, 2], [1, 2, 3], [2, 3, 4]]
> 
> def odds():
>   x = 0
>   while x < 36:
>     yield x
>     x += 1
> 
> print [x*x for x in odds() if x > 30]
// I admit this isn't nearly as neat as the python version.
// We really need to come up with a way to do yield.
// StackThreads, anyone?
class odds : Iterator!(int) {
  int x=0;
  bool next(ref int foo) {
    if (x!<36) return false; foo=x; x+=1; return true;
  }
}
writefln((new odds)~filters!("_>30")~maps!("_*_")~toArray);
> # Prints: [961, 1024, 1089, 1156, 1225]

Still, despite the ugliness D is actually not that far off.
 --downs
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGzp1KpEPJRr05fBERAjdNAJ9BLWgjjz/fiSMkZQm21ul8LPezOACfcwNr
z3oUu+mF/ojlx2gkLIccSjg=
=dpId
-----END PGP SIGNATURE-----


More information about the Digitalmars-d-learn mailing list