IFTI - first impressions

Oskar Linde oskar.lindeREM at OVEgmail.com
Wed Mar 8 05:06:10 PST 2006


Oskar Linde skrev:

> const int[] data = [1,2,3,76,2,1,3,45,2];
> writefln("data = ", data);
> 
> auto squared = map(data, function int(int x) { return x*x; });
> writefln("squared = ", squared);
> 
> auto sqrooted = map(data, function double(int x) { return 
> sqrt(cast(float)x); });
> writefln("sqrooted = ", sqrooted);
> 
> auto even = filter(data, function bool(int x) { return (x&1) == 0; });
> writefln("even = ", even);
> 
> auto odd = filter(data, function bool(int x) { return (x&1) == 1; });
> writefln("odd = ", odd);

I just noticed that with implicit array member functions, you can even 
write the last one as:

auto odd = data.filter(function bool(int x) { return (x&1) == 1; });
writefln("odd = ", odd);

Isn't this even better?

You can also make a:

template stable_sort(ArrTy) {
	ArrTy stable_sort(ArrTy arr) {
		...
	}
}

and make a library alternative to the built in sort:

a.stable_sort();

The only difference to the built in sort is the tailing parenthesis. I 
wish we could get rid of those or force .sort to be called as .sort(). 
Then we could make .sort a pure library implementation together with 
other array functions.

/Oskar



More information about the Digitalmars-d mailing list