<p>Lambda来自<a href="http://en.wikipedia.org/wiki/Lambda_calculus" rel="noreferrer">Lambda Calculus</a>,在编程中引用匿名函数。</p>
<p>为什么这么酷?它允许您编写快速丢弃的函数,而不必命名它们。它还提供了一种编写闭包的好方法。有了这种能力你可以做这样的事情。</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>从Python的片段中可以看到,函数加法器接受一个参数x,并返回一个匿名函数lambda,它接受另一个参数y。该匿名函数允许您从函数创建函数。这是一个简单的例子,但是它应该传达lambdas和闭包的强大功能。</p>
<p><strong>其他语言的示例</strong></p>
<p><strong>Perl 5</strong></p>
<pre class="lang-pl prettyprint-override"><code>sub adder {
my ($x) = @_;
return sub {
my ($y) = @_;
$x + $y
}
}
my $add5 = adder(5);
print &$add5(1) == 6 ? "ok\n" : "not ok\n";
</code></pre>
<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 => y => x + y;
add5 = adder(5);
add5(1) == 6
</code></pre>
<p><strong>方案</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<int, Func<int, int>> adder =
(int x) => (int y) => x + y; // `int` declarations optional
Func<int, int> 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<int, int> addWithClosure =
(x) => x + yEnclosed;
Debug.Assert(addWithClosure(2) == 3);
</code></pre>
<p><strong>迅捷</strong></p>
<pre class="lang-swift prettyprint-override"><code>func adder(x: Int) -> (Int) -> 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 (&$a, &$b) {
echo $a + $b;
};
echo $lambda();
</code></pre>
<p><strong>哈斯克尔</strong></p>
<pre><code>(\x y -> x + y)
</code></pre>
<p><strong>Java</strong>请参阅<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<Integer> pred = x -> x % 2 == 0; // Tests if the parameter is even.
boolean result = pred.test(4); // true
</code></pre>
<p><strong>Lua</strong></p>
<pre class="lang-lua prettyprint-override"><code>adder = function(x)
return function(y)
return x + y
end
end
add5 = adder(5)
add5(1) == 6 -- true
</code></pre>
<p><strong>科特林</strong></p>
<pre class="lang-kotlin prettyprint-override"><code>val pred = { x: Int -> x % 2 == 0 }
val result = pred(4) // true
</code></pre>
<p><strong>红宝石</strong></p>
<p>Ruby有一点不同,它不能使用与调用函数完全相同的语法来调用lambda,但是它仍然有lambda。</p>
<pre class="lang-ruby prettyprint-override"><code>def adder(x)
lambda { |y| x + y }
end
add5 = adder(5)
add5[1] == 6
</code></pre>
<p>Ruby是Ruby,有一个lambdas的简写,因此您可以通过这种方式定义<code>adder</code>:</p>
<pre><code>def adder(x)
-> y { x + y }
end
</code></pre>
<p><strong>R</strong></p>
<pre class="lang-r prettyprint-override"><code>adder <- function(x) {
function(y) x + y
}
add5 <- adder(5)
add5(1)
#> [1] 6
</code></pre>