For a person without a comp-sci background, what is a lambda in the world of Computer Science?
相关问题
- Why wrapping a function into a lambda potentially
- Generating powerset in one function, no explicit r
- Check if tie in count of lists using linq
- C# to VB - How do I convert this anonymous method
- applying a list to an entered function to check fo
相关文章
- Why doesn't C11 support lambda functions
- Algorithm for partially filling a polygonal mesh
- Should client-server code be written in one “proje
- Algorithm for maximizing coverage of rectangular a
- Is there an existing solution for these particular
- Will java allow to use functional interfaces as me
- Detect if C++ lambda can be converted to function
- What is Scope Creep? [closed]
I got it too. I`ve tried it in JS with this one:
It adds 2 to 4 then mults the result by 6. However I find it sometimes hard to read :(
Also I`ve made an interesting forEach function:
forEach([1,2,3,4,5])(console.log);
This method will iterate an array and performs an action - in the case printing to the console. Now I too get why labmdas are powerful.
An example of a lambda in Ruby is as follows:
Will genereate the following output:
The lambda calculus is a consistent mathematical theory of substitution. In school mathematics one sees for example
x+y=5
paired withx−y=1
. Along with ways to manipulate individual equations it's also possible to put the information from these two together, provided cross-equation substitutions are done logically. Lambda calculus codifies the correct way to do these substitutions.Given that
y = x−1
is a valid rearrangement of the second equation, this:λ y = x−1
means a function substituting the symbolsx−1
for the symboly
. Now imagine applyingλ y
to each term in the first equation. If a term isy
then perform the substitution; otherwise do nothing. If you do this out on paper you'll see how applying thatλ y
will make the first equation solvable.That's an answer without any computer science or programming.
The simplest programming example I can think of comes from http://en.wikipedia.org/wiki/Joy_(programming_language)#How_it_works:
Added: http://imgur.com/a/XBHub
The name "lambda" is just a historical artifact. All we're talking about is an expression whose value is a function.
A simple example (using Scala for the next line) is:
where the argument to the
foreach
method is an expression for an anonymous function. The above line is more or less the same as writing something like this (not quite real code, but you'll get the idea):except that you don't need to bother with:
Once you're used to function values, having to do without them seems as silly as being required to name every expression, such as:
instead of just writing the expression where you need it:
The exact notation varies from language to language; Greek isn't always required! ;-)
Slightly oversimplified: a lambda function is one that can be passed round to other functions and it's logic accessed.
In C# lambda syntax is often compiled to simple methods in the same way as anonymous delegates, but it can also be broken down and its logic read.
For instance (in C#3):
LinqToSql can read that function (x > 15) and convert it to the actual SQL to execute using expression trees.
The statement above becomes:
This is different from normal methods or anonymous delegates (which are just compiler magic really) because they cannot be read.
Not all methods in C# that use lambda syntax can be compiled to expression trees (i.e. actual lambda functions). For instance:
Now the expression tree cannot be read - SomeComplexCheck cannot be broken down. The SQL statement will execute without the where, and every row in the data will be put through
SomeComplexCheck
.Lambda functions should not be confused with anonymous methods. For instance:
This also has an 'inline' function, but this time it's just compiler magic - the C# compiler will split this out to a new instance method with an autogenerated name.
Anonymous methods can't be read, and so the logic can't be translated out as it can for lambda functions.
It is a function that has no name. For e.g. in c# you can use
to return the numbers that are greater than 5.
is the lambda part here. It represents a function which takes a parameter (number) and returns a boolean value (number > 5). GetMatchingItems method uses this lambda on all the items in the collection and returns the matching items.