Lambda syntax, etc
bearophile
bearophileHUGS at lycos.com
Thu Feb 5 07:46:38 PST 2009
Andrei Alexandrescu>Beyond that, I don't feel that changes like moving the parameters on one side or the other of "{" would be Earth-shattering.<
As you have seen there are several possible syntaxes for anonymous functions/delegates/closures. And the current syntax already works, so I think none of such changes can be Earth-shattering.
On the other hand, extensive usage of lambdas with my dlibs has shown me that having an uncluttered and un-noisy syntax is quite important if you want to use such functional style a lot, because otherwise noise builds up quickly and in production code you are forced to split things in several lines, otherwise no one can read and debug the code you write.
Regarding such noise I can show you an almost extreme example, this is an easy programming task:
http://projecteuler.net/index.php?section=problems&id=4
>Find the largest palindrome made from the product of two 3-digit numbers<
With Python:
>>> max(i*j for i in xrange(100,1000) for j in xrange(100,1000) if str(i*j) == str(i*j)[::-1])
906609
D1 with my dlibs:
import d.all;
void main() {
putr( max(select(i*j, i, xrange(100,1000), j, xrange(100,1000), str(i*j) == str(i*j).reverse)) );
}
Notice in this case Python is clearly better, not just because it's more readable, but also because that select() generates items eagerly, while the Python oneliner contains a lazy generator expression.
dlibs contain xmap too, but that code is so much more noisy that I don't want to show it :-)
I know this isn't an example of code that's normal in production, but sometimes you need extreme example to show your point :-)
Now, back to the topic: putting arguments into a single () or {} instead of a (){ return ;} (or with the => of C#) helps the eye see a single visual object isntead of two, this helps the human parsing of the code, improving visual chunking and reducing noise.
Bye,
bearophile
More information about the Digitalmars-d
mailing list