Confused by C# lambda expression - c#

I have been learning about the lambda expression, I got happy when I finally can read/understand the => operator, it kind of means "where" to me
List<int> a = new List<int>(){0,1,2,1,3,4,5,6,7,8,9};
IEnumerable<int> b = a.FindAll(x => x>=5);
foreach (int x in b)
Console.WriteLine(x);
Reading the above line, personally makes sense to read it as "Find all x's from this list WHERE x is greater than or equal 5", very good.
But then I come across a different use of the lambda expression with the Select method.
List<int> a = new List<int>(){0,1,2,1,3,4,5,6,7,8,9};
IEnumerable<int> b1 = a.Select(x => x*2);
foreach (int x in b)
Console.WriteLine(x);
With this one, the previous way of reading this operator doesn't make sense, as to me this code does "For each x return x*2", which is very different "function" to what the same operator does in the previous case.
I understand that the difference is between .FindAll and .Select, different way of dealing with input and output parameters, but I am talking about the use of the operator => in the lambda expression.

There's no question in this question, so let's make one up.
Characterizing the lambda operator as "where" works when the lambda returns a bool and is used as a predicate to test a value. Is there a more general characterization of the lambda operator that makes sense in other contexts, such as projection?
Yes. Read the lambda operator as "goes to".
a.Select(x => x * 2);
"each x goes to x times two"
You can use that for predicates as well:
a.Where(x => x > 2);
"each x goes to 'is x greater than two?'"
But that's awkward. As you note, it is easier to think of this as "where" or "such that"
"each x such that x is greater than two"
Similarly
a.OrderBy(x => x.LastName)
"order each x by x goes to last name" is awkward. Just say "order each x by last name".
In short: the English language interpretation of the operator depends on the context. The formal interpretation of the operator is always the same: it simply describes a function.

The => operator has exactly the same meaning in both cases: it creates a function whose parameter is the thing on the left, and whose return value is the thing on the right.
You wrote in a comment that the x in the first case is not a parameter as you understand it. That's not correct; it is a parameter in both cases here.
Here's your first example, again:
List<int> a = new List<int>(){0,1,2,1,3,4,5,6,7,8,9};
IEnumerable<int> b = a.FindAll(x => x>=5);
foreach (int x in b)
Console.WriteLine(x);
If you wanted to write this without using lambda notation, you would define a function somewhere, like this...
static bool MyCondition(int x)
{
return x >= 5;
}
...and then use that function as the argument to FindAll:
List<int> a = new List<int>(){0,1,2,1,3,4,5,6,7,8,9};
IEnumerable<int> b = a.FindAll(MyCondition);
foreach (int x in b)
Console.WriteLine(x);
The lambda notation is a shorter notation which allows you to define the function right where you use it.
Likewise, if you wanted to write your second example without using lambda notation, you'd define a function elsewhere, like this...
static int MyOperation(int x)
{
return x * 2;
}
...and pass your function as the argument to Select, like this:
List<int> a = new List<int>(){0,1,2,1,3,4,5,6,7,8,9};
IEnumerable<int> b1 = a.Select(MyOperation);
foreach (int x in b)
Console.WriteLine(x);

Think of it this way:
Mathematics: f(x) = x + x
This is a mathematical function f that takes a number x and spits out its double.
Lambda: f = x => x + x C#'s way of defining the same function f.
Another example:
Mathematics: g(x, y) = x > y
g is a function that takes two numbers x and y and returns wether the former is greater than the latter.
Lambda: g = (x, y) => x > y C#'s way of defining the same function g.
Clearer now?
P.D: I've omitted talking about type inference and the type of the lambda's themselves; its an unnecessary distraction considering the context of this question.

Related

What is the difference between these three delegate declarations?

Assume there is the following delegate:
delegate int Foo(int x);
I've seen people create this delegate in multiple ways, for example:
Example 1:
Foo f = Bar;
With Bar being:
int Bar(int x) => x * 2;
Example 2:
Foo f = new Foo(x => x * 2);
Example 3:
Foo f = x => x * 2;
Are there any differences (pro's, con's, efficiency, etc.) other than personal coding style preference?
Although examples 2 and 3 are pretty much identical, the two may become different from example 1 when used as local declarations.
There is no difference between 2 and 3: in 2, you specify delegate type explicitly, while in the second case you let the compiler figure out the type of the delegate by looking at the type of f. When f is a local variable, you can further simplify the declaration to
var f = new Foo(x => x * 2);
so that Foo is specified only once.
The first example uses a lambda-bodied method to define a delegate through a method group. The obvious difference here is that this way of declaring a delegate requires a separate method.
However, there is another subtle difference here: when delegates from examples 2 and 3 are used in a local context, they can close over local variables. In contrast, example 1 cannot capture local variables, even if you use it in a local context.
Code examples 2 and 3 let you do this:
Foo Make(int y) {
var res = x => x * y; // y is captured from the context
return res;
}
while example 1 is limited to using x that you pass and any fields of the object that declares Bar.
Therefore, you need to consider these points when deciding among these three options:
If you need a delegate that does not close over local variables, and relies on logic that can be used elsewhere, use method group,
If you need a delegate that closes over local variables, use 2 with var, or 3,
If you are declaring a field, use 3, because you must specify the type of the field anyway.
The main difference is that in Example 1 you can reuse your function Bar. I believe that 2 and 3 are synonymous and that the lambda expression is just syntactic sugar for the anonymous function.
The first, example below is called expression bodied method, so the delegate will refer to this method which returns an int and will take whatever value of x is and multiply it by 2.
int Bar(int x) => x * 2;
The second, example below is using a lambda expression, it is instantiating the delegate and passing a reference to the method that will return the value of x multiplied by 2.
Foo f = new Foo(x => x * 2);
The last example is just syntactic sugar which behind the scenes is pretty much the same as the example above.
Foo f = x => x * 2;
Are there any differences (pro's, con's, efficiency, etc.) other than
personal coding style preference?
they all essentially accomplish the same end result, however, the first example you could reuse the method as many times as you want whereas the last two you're passing in a behaviour that just gets executed (at a later time) without any identifier to the method for later reuse.

Intermediate and return values in continuation-passing style

I am coming from a OOP, non-functional background, so I am having trouble fully visualizing several online examples regarding continuation passing. Also, functional languages like Scheme don't have to specify types of arguments or return values, so I am unsure whether I got the idea correctly.
Since C# supports lambdas, I took the first example from the Wikipedia article and tried to port it to C# with strong typing, to see how the pattern would apply:
// (Scheme)
// direct function
(define (pyth x y)
(sqrt (+ (* x x) (* y y))))
// rewriten with CPS
(define (pyth& x y k)
(*& x x (lambda (x2)
(*& y y (lambda (y2)
(+& x2 y2 (lambda (x2py2)
(sqrt& x2py2 k))))))))
// where *&, +& and sqrt& are defined to
// calculate *, + and sqrt respectively and pass the result to k
(define (*& x y k)
(k (* x y)))
So, rewriting the CPS pyth& version in C# resulted in:
// (C#6)
// continuation function signature
delegate double Cont(double a);
// *&, +& and sqrt& functions
static double MulCont(double a, double b, Cont k) => k(a * b);
static double AddCont(double a, double b, Cont k) => k(a + b);
static double SqrtCont(double a, Cont k) => k(Math.Sqrt(a));
// sqrt(x*x + y*y), cps style
static double PythCont(double x, double y, Cont k) =>
MulCont(x, x, x2 =>
MulCont(y, y, y2 =>
AddCont(x2, y2, x2py2 =>
SqrtCont(x2py2, k))));
I could have used generics instead of double, but signatures would be longer. Anyway, what I am not sure is:
Is the Cont signature above correct (i.e. Func<double, double>)? Should the continuation fn. accept the parameter, process it, and then return the value of the same type back?
When I first started reading about continuations, I got the feeling that this continuation function will get invoked for each step in the call stack, but in the example above it's only passed to sqrt&, and all other calls get lambdas which don't really "pass" intermediate values to the original continuation. The code above in the function above is basically analogue to k(Math.Sqrt(x * x + y * y)), so does this mean my assumption about intermediate "hooks" is wrong?
Yes, unless you want to do anything non-numerical with the outermost continuation, it is correct.
You would only need more "Cont"s when your original expression involves more types, e.g.
(define (foo x) (if (= x 0) 1 0))
in which case it might look like this (sorry I write in scheme for brevity):
(define (foo& x k)
(=& x 0 (lambda (r1)
(if r1 (k 1) (k 0)))))
-- now the outermost continuation has a number (let's say an int) as input, while the one provided to "=&" has bool->int type.
You are almost right (up to duality) -- each step on the call stack is now a call to some continuation.
In general you might be confusing first-class continuations with cps -- the former is a language feature (as in scheme where you can access the current continuation with call/cc operator), the latter is a technique you can use anywhere.
You actually can convert expressions to cps without even having higher-order functions in your language (by just representing them somehow).
Another thing you asked is how cps relates to control flow. Well, notice that in applicative, functional language (like scheme) the only thing you have specified is that in case of application, you first evaluate the operands and the operator, and then apply the latter to the former. It does not matter in what order you evaluate the operands -- you might do it left-to-right, right-to-left [or perhaps in some crazy way]. But what if you're not using purely functional style, and the operands cause some side effects? They might e.g. print something to stdout, and later return some value. In that case, you would like to have control over the order. If I remember well, programs compiled with gambit-C evaluate arguments right-to-left, while interpreted with gambit's interpreter left-to-right -- so the problem really exists ;). And precisely then the cps might save you [actually there are other means as well, but we're about cps right now!].
In the scheme example you posted it is forced that the arguments of "+" are evaluated left-to-right.
You might alter that easily:
(define (pyth& x y k)
(*& y y (lambda (y2)
(*& x x (lambda (x2)
(+& x2 y2 (lambda (x2py2)
(sqrt& x2py2 k))))))))
And that's the thing.
Of some further applications, as guys already said in the comments, transformation to CPS moves every application to tail-position, so the call stack is being replaced with lambdas, and further if you defunctionalize them, what you get is a data structure representing the control flow -- a neat form to be converted to, say C, or some other imperative language. Fully automagicaly!
Or, if you'd like to implement some monad mumbo-jumbo, say Maybe monad, in CPS it's easy, just prepend to each continuation-lambda the test on whether the received value is "Just something" (in which case do the job and push the result to your continuation), or "Nothing", in which case you just push Nothing (to the continuation-lambda).
Of course rather by another program or macro, not by hand, as it might be tedious -- the most magic behing cps is that it's so easy to automate the transformation to cps.
Hope I didn't make it unnecessarily complicated.
I have created a very comprehensive introduction to the Continuation monad that you can Find Here Discovering the Continuation Monad in C#
Also you can find a.Net Fiddle here
I Repeat it in summary here
Starting from an initial Function
int Square(int x ){return (x * x);}
Use Callback and remove return type
public static void Square(int x, Action<int> callback)
{
callback(x * x);
}
Curry the Callback
public static Action<Action<int>> Square(int x)
{
return (callback) => { callback(x * x); };
}
Generalize the returned Continuation
public static Func<Func<int,T>,T> Square<T>(int x)
{
return (callback) => { callback(x * x); };
}
Extract the Continuation Structure Also Known As the Return Method of the monad
delegate T Cont<U, T>(Func<U, T> f);
public static Cont<U, T> ToContinuation<U, T>(this U x)
{
return (callback) => callback(x);
}
square.ToContinuation<Func<int, int>, int>()
Add The bind Monadic method and thus Complete the Monad
public static Cont<V, Answer> Bind<T, U, V, Answer>(
this Cont<T, Answer> m,
Func<T, Cont<U, Answer>> k,
Func<T, U, V> selector)
{
return (Func<V, Answer> c) =>
m(t => k(t)(y => c(selector(t, y))));
}

What is the different in the lambda expression from group1 and group2?

What is the difference between Group1 and Group2 in the following syntax. I have just started using these types of expressions and I see one that has a delegate and looks like a function (for the most part) and then I see Group1 (=>) with this syntax and I'm not certain which is different or which is better.
It also says that Func<> should only take two arguments but in Group1 example it takes three variables. Why is that?
http://msdn.microsoft.com/en-us/library/bb549151(v=vs.110).aspx
Group1
Action<bool> print1 = q => Console.WriteLine(q);
Action<int> printInt = q => Console.WriteLine(q);
Func<double, double> square1 = c => c * c;
Func<double, double, double> add1 = (x, y) => x + y;
Predicate<double> isLessThanTen1 = f => f < 10;
Group2
Func<string, string> convert = delegate(string s)
{
return s.ToUpper();
};
Group 1 is a shorthand way of creating a delegate, without the need for delegate(). It's "preferred" because it's shorter and easier to read.
Action<> is a delegate that takes an argument (or multiple) and returns void
Func<> is a delegate that takes an argument (or multiple) and returns something
Predicate<> is a delegate that takes an argument (or multiple) and returns bool
In other words
Func<int, void> == Action<int>
and
Func<int, bool> == Predicate<int>
In the MSDN page you linked to, there's other overloads of Func<> that take a varying amount of parameters. For example, here's a version that accepts 4 parameters.
One of the best resources on the topic is C# in Depth by Jon Skeet. It gives a nice history of delegates and the syntax changes in each version of C#.

Does comparing to Math.Min or Math.Max short-circuit?

When comparing to a minimum or maximum of two numbers/functions, does C# short-circuit if the case is true for the first one and would imply truth for the second? Specific examples of these cases are
if(x < Math.Max(y, z()))
and
if(x > Math.Min(y, z()))
Since Math.Max(y, z()) will return a value at least as large as y, if x < y then there is no need to evaluate z(), which could take a while. Similar situation with Math.Min.
I realize that these could both be rewritten along the lines of
if(x < y || x < z())
in order to short-circuit, but I think it's more clear what the comparison is without rewriting. Does this short-circuit?
As others have pointed out, the compiler knows nothing about the semantics of Min or Max that would allow it to break the rule that arguments are evaluated before the method is called.
If you wanted to write your own, you could do so easily enough:
static bool LazyLessThan(int x, int y, Func<int> z)
{
return x < y || x < z();
}
and then call it
if (LazyLessThan(x, y, z))
or
if (LazyLessThan(x, y, ()=>z()))
Or for that matter:
static bool LazyRelation<T>(T x, T y, Func<T> z, Func<T, T, bool> relation)
{
return relation(x, y) || relation(x, z());
}
...
if (LazyRelation(x, y, ()=>z, (a,b)=> a < b)))
No, it doesn't short circuit and z() will always be evaluated. If you want the short circuiting behavior you should rewrite as you have done.
Math.Min() and Math.Max() are methods just like any other. They have to be evaluated in order to return the value which will be used as the second argument in the comparison. If you want short-circuiting then you will have to write the condition using the || operator as you have demonstrated.
(Nothing particularly new to add, but I figured I'd share the results of a test I ran on it.)
Math.Max() could easily be inlined by the CLR's just-in-time compiler and from there I was curious whether it might further optimize the code in such a way that it is short-circuited.
So I whipped up a microbenchmark that evaluates the two expressions 1,000,000 times each.
For z(), I used a function that calculates Fib(15) using the recursive method. Here are the results of running the two:
x < Math.Max(y, z()) : 8097 ms
x < y || x < z() : 29 ms
I'm guessing the CLR won't transform the code in any way that prevents method calls from executing, because it doesn't know (and doesn't check to see if) the routine has any side effects.
No, it doesnt short circuit, at least at the C# compiler level. Math.Min or Math.Max are two ordinary static method calls and the compiler will not optimize in that sense.
The order of evaluation of the code will be: z(), Math.Max, x > ...
If you really want to make sure, check out the IL code.

C# Sorting a list by X and Y

I need to sort a list, by X and Y. I read some on other questions to sort X and Y, but I didn't understand the:
List<SomeClass>() a;
List<SomeClass> b = a.OrderBy(x => x.x).ThenBy(x => x.y).ToList();
I mean, x => x.x gives undeclared variable error.
Question is: How to sort by X and Y in a list, or: What variable to have instead of x => x.x????
edit: right, my list is like this:
List<Class1> MapTiles;
and in class: int ID, int X, int Y
Thanx.
C# is case sensitive. You need
// Note the capital letters
List<SomeClass> b = a.OrderBy(item => item.X).ThenBy(item => item.Y).ToList();
dlev's answer is the correct one to this question, the following is just an extended explanation of a concept that Marcus may have not understood fully
Just to clarify some point that was brought up in the comments, what's happening is you're actually passing in a delegate to do the sorting.
The following snippet
List<Item> b = a.OrderBy(item => item.X);
Is like creating a static compare function that sorts by comparing the field (or property) X of an object of type Item and passing that compare function into a sort function, as you might do in C++.
The OrderBy(...) is just a very short and convenient way of accomplishing this.

Categories

Resources