Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Lambda comes from the <a href="http://en.wikipedia.org/wiki/Lambda_calculus" rel="noreferrer">Lambda Calculus</a> and refers to anonymous functions in programming.</p> <p>Why is this cool? It allows you to write quick throw away functions without naming them. It also provides a nice way to write closures. With that power you can do things like this.</p> <p><strong>Python</strong></p> <pre class="lang-py prettyprint-override"><code>def adder(x): return lambda y: x + y add5 = adder(5) add5(1) 6 </code></pre> <p>As you can see from the snippet of Python, the function adder takes in an argument x, and returns an anonymous function, or lambda, that takes another argument y. That anonymous function allows you to create functions from functions. This is a simple example, but it should convey the power lambdas and closures have.</p> <p><strong>Examples in other languages</strong></p> <p><strong>JavaScript</strong></p> <pre class="lang-js prettyprint-override"><code>var adder = function (x) { return function (y) { return x + y; }; }; add5 = adder(5); add5(1) == 6 </code></pre> <p><strong>JavaScript (ES6)</strong></p> <pre class="lang-js prettyprint-override"><code>const adder = x =&gt; y =&gt; x + y; add5 = adder(5); add5(1) == 6 </code></pre> <p><strong>Scheme</strong></p> <pre class="lang-scheme prettyprint-override"><code>(define adder (lambda (x) (lambda (y) (+ x y)))) (define add5 (adder 5)) (add5 1) 6 </code></pre> <p><strong><a href="http://msdn.microsoft.com/en-us/library/0yw3tz5k%28v=vs.110%29.aspx" rel="noreferrer">C# 3.5 or higher</a></strong></p> <pre class="lang-cs prettyprint-override"><code>Func&lt;int, Func&lt;int, int&gt;&gt; adder = (int x) =&gt; (int y) =&gt; x + y; // `int` declarations optional Func&lt;int, int&gt; add5 = adder(5); var add6 = adder(6); // Using implicit typing Debug.Assert(add5(1) == 6); Debug.Assert(add6(-1) == 5); // Closure example int yEnclosed = 1; Func&lt;int, int&gt; addWithClosure = (x) =&gt; x + yEnclosed; Debug.Assert(addWithClosure(2) == 3); </code></pre> <p><strong>Swift</strong></p> <pre class="lang-swift prettyprint-override"><code>func adder(x: Int) -&gt; (Int) -&gt; Int{ return { y in x + y } } let add5 = adder(5) add5(1) 6 </code></pre> <p><strong>PHP</strong></p> <pre class="lang-php prettyprint-override"><code>$a = 1; $b = 2; $lambda = function () use (&amp;$a, &amp;$b) { echo $a + $b; }; echo $lambda(); </code></pre> <p><strong>Haskell</strong></p> <pre><code>(\x y -&gt; x + y) </code></pre> <p><strong>Java</strong> see <a href="https://stackoverflow.com/questions/36233477/lambda-expression-in-java-1-8/36233545#36233545">this post</a></p> <pre class="lang-java prettyprint-override"><code>// The following is an example of Predicate : // a functional interface that takes an argument // and returns a boolean primitive type. Predicate&lt;Integer&gt; pred = x -&gt; x % 2 == 0; // Tests if the parameter is even. boolean result = pred.test(4); // true </code></pre> <p><strong>Lua</strong></p> <pre><code>adder = function(x) return function(y) return x + y end end add5 = adder(5) add5(1) == 6 -- true </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload