R
raylopez99
Took a look at all the fuss about "lambda expressions" from Jon
Skeet's excellent book "C# in Depth". Jon has an example, reproduced
below (excerpt) on lambda expressions.
My n00b take: it's like SQL and those 'high level' languages that try
to be too clever by half and pack a lot of syntax in as short a space
as possible. Like Egyptian hieroglyphics, like Chinese, like those
non-alphabet languages, it's just way too much information in too
short a space. Sure, writing a program with this syntax will reduce
your source code from 100 pages to 10, but at a cost of readability.
But what do I know? I only have a couple months real experience in
the language. Don't listen to me.
RL
"To try and outstrip each other in the arms race, or to expect to win
a nuclear war, is dangerous madness." - Leonid Brezhnev
this is equivalent: from C#1.0
ArrayList products = Product.GetSampleProducts(); //fills arraylist
(fanciful example)
foreach (Product X in products) {
if (X.price > 10m) { Console.WriteLine(X);} } [suffix m =
decimal]
now redo in C#2 mode:
ArrayList products = Product.GetSampleProducts(); //fills arraylist
Predicate <Product> products test = delegate (Product p) {return
p.Price > 10m;} ;
List<Product> matches = products.FindAll(test);
Action<Product> print = delegate(Product p) {Console.WriteLine(p);};
matches.ForEach (print);
which is actually equivalent to (lambda expressions):
ArrayList products = Product.GetSampleProducts(); //fills, as before
foreach (Product product in products.Where (p =>p.Price > 10))
{Console.WriteLine(product);}
//Jon humorously states "the combination of the lambda expression
putting the test in just the right place and a well-named method means
we can almost read the code out loud and understand it without even
thinking" HA HA HA! Right. I guess beauty is in the eye of the
beholder.
RL
Skeet's excellent book "C# in Depth". Jon has an example, reproduced
below (excerpt) on lambda expressions.
My n00b take: it's like SQL and those 'high level' languages that try
to be too clever by half and pack a lot of syntax in as short a space
as possible. Like Egyptian hieroglyphics, like Chinese, like those
non-alphabet languages, it's just way too much information in too
short a space. Sure, writing a program with this syntax will reduce
your source code from 100 pages to 10, but at a cost of readability.
But what do I know? I only have a couple months real experience in
the language. Don't listen to me.
RL
"To try and outstrip each other in the arms race, or to expect to win
a nuclear war, is dangerous madness." - Leonid Brezhnev
this is equivalent: from C#1.0
ArrayList products = Product.GetSampleProducts(); //fills arraylist
(fanciful example)
foreach (Product X in products) {
if (X.price > 10m) { Console.WriteLine(X);} } [suffix m =
decimal]
now redo in C#2 mode:
ArrayList products = Product.GetSampleProducts(); //fills arraylist
Predicate <Product> products test = delegate (Product p) {return
p.Price > 10m;} ;
List<Product> matches = products.FindAll(test);
Action<Product> print = delegate(Product p) {Console.WriteLine(p);};
matches.ForEach (print);
which is actually equivalent to (lambda expressions):
ArrayList products = Product.GetSampleProducts(); //fills, as before
foreach (Product product in products.Where (p =>p.Price > 10))
{Console.WriteLine(product);}
//Jon humorously states "the combination of the lambda expression
putting the test in just the right place and a well-named method means
we can almost read the code out loud and understand it without even
thinking" HA HA HA! Right. I guess beauty is in the eye of the
beholder.
RL