DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

Lambda and Closure


closure本身是指 将 一个处于open状态(包含自由变量)的函数转换为closed状态 (所有变量都被绑定) 所需要的 外部环境。

从编程实践角度,closure是一个对象,包含 一段代码 以及 它被创建时所处的环境。

$ sdk i groovy
$ groovysh
groovy:000> :set interpreterMode true
groovy:000> def myadd = { x, y -> x + y + 2 }
===> groovysh_evaluate$_run_closure1@7fb95505
groovy:000> def l1 = [1,2,3]
===> [1, 2, 3]
groovy:000> def l2 = [10,20,30]
===> [10, 20, 30]
groovy:000> res = [l1, l2].transpose().collect(myadd)
===> [13, 24, 35]
groovy:000> res.class
===> class java.util.ArrayList
groovy:000> exit

$ ipython
In [1]: myadd = lambda x, y: x + y + 2
In [2]: res = map(myadd, [1,2,3], [10,20,30])
In [3]: list(res)
Out[3]: [13, 24, 35]
In [4]: exit

$ sdk i kotlin
$ kotlinc-jvm

Ref:

http://stackoverflow.com/a/36878651/701420

http://stackoverflow.com/questions/220658/what-is-the-difference-between-a-closure-and-a-lambda

https://en.wikipedia.org/wiki/Closure_(computer_programming)

https://en.wikipedia.org/wiki/Map_(higher-order_function)#Language_comparison

http://stackoverflow.com/questions/35973196/which-kotlin-features-are-not-available-in-statically-compiled-groovy



Published

Dec 15, 2016

Last Updated

Dec 15, 2016

Category

Tech

Tags

  • closure 3
  • function 4
  • lambda 3

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor