Gance
淺談CLOSURE
Many of modern programming
languages supports Closure.
Is Java going to support Closure?
A closure (閉包) is a function
that has an environment of its
own
Closures are a technique for
implementing lexically scoped
name binding in languages with
first-class functions.
Closure Example 1
js> function doSome() {
> var x = 10;
> function f(y) {
> return x + y;
> }
> return f;
> }
js> var foo = doSome();
js> foo(20);
30
js> foo(30);
40
Closure Example 2
js> function doOther() {
> var x = 10;
> function f(y) {
> return x + y;
> }
> x = 100;
> return f;
> }
js> var foo = doOther();
js> foo(20);
120
js> foo(30);
130
Closure Example 3
js> function doOther() {
> var x = 10;
> function f() {
> x--;
> return x;
> }
> return f;
> }
js> var f1 = doOther();
js> var f2 = doOther();
js> f1();
9
js> f2();
9
More About Closure
 Closure是擁有閒置變數(Free variable)的運
算式。
 如果函式的閒置變數與當時語彙環境(Lexical
environment)綁定,該函式才稱為Closure。
 Closure關閉的是變數的reference,而不是變數
的value
Java 8 introduces the support
for Lambda. Is it Closure?
Lambda Expression
 A lambda expression is an “anonymous
function”
 a method without a declaration (access
modifier, return value declaration, and name)
 Syntax: (argument) -> (body)
(int a, int b) -> { return a + b; }
() -> System.out.println("Hello World");
(String s) -> { System.out.println(s); }
() -> 42
() -> { return 3.1415 };
Lambda Example 1
//Old way:
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Hello from thread");
}
}).start();
//New way:
new Thread(
() -> System.out.println("Hello from thread")
).start();
Lambda Example 2
//Old way:
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("The button was clicked using old fashion code!");
}
});
//New way:
button.addActionListener( (e) -> {
System.out.println("The button was clicked. From lambda expressions !");
});
Lambda Example 3
public static void main(String[] args) {
int number = 5;
ObjectCallback callback = () -> {
return (number = number + 1);
};
Object result = callback.Callback();
System.out.println(result);
}
number should be effectively final
Conclusion
 A closure is any function which closes over
the environment in which it was defined.
 A lambda is just an "anonymous function" - a
function defined with no name.
 Lambda in Java 8 is NOT closure!
歐美防毒/安全軟體的領導品牌Avast Software台北辦公
室,徵一名資深Android RD (Android Tech-Lead
Developer)
• 與頂尖的Android RD、architect一同開發給全世界數千萬使用
者使用的app
• Onboard後立即到美麗的捷克布拉格training三週以上
• 每年公司全額補助一次參加類似Google I/O的大型conference
• 15天年假 +彈性工時 +(partial) remote working
• 詳見
http://www.104.com.tw/job/?jobno=4hqzm&jobsource=checkc
歡迎熱愛app、熟悉OO的Android高手加入我們!
Android taipei 20160225   淺談closure

Android taipei 20160225 淺談closure

  • 1.
  • 3.
    Many of modernprogramming languages supports Closure.
  • 4.
    Is Java goingto support Closure?
  • 6.
    A closure (閉包)is a function that has an environment of its own
  • 7.
    Closures are atechnique for implementing lexically scoped name binding in languages with first-class functions.
  • 8.
    Closure Example 1 js>function doSome() { > var x = 10; > function f(y) { > return x + y; > } > return f; > } js> var foo = doSome(); js> foo(20); 30 js> foo(30); 40
  • 9.
    Closure Example 2 js>function doOther() { > var x = 10; > function f(y) { > return x + y; > } > x = 100; > return f; > } js> var foo = doOther(); js> foo(20); 120 js> foo(30); 130
  • 10.
    Closure Example 3 js>function doOther() { > var x = 10; > function f() { > x--; > return x; > } > return f; > } js> var f1 = doOther(); js> var f2 = doOther(); js> f1(); 9 js> f2(); 9
  • 11.
    More About Closure Closure是擁有閒置變數(Free variable)的運 算式。  如果函式的閒置變數與當時語彙環境(Lexical environment)綁定,該函式才稱為Closure。  Closure關閉的是變數的reference,而不是變數 的value
  • 12.
    Java 8 introducesthe support for Lambda. Is it Closure?
  • 13.
    Lambda Expression  Alambda expression is an “anonymous function”  a method without a declaration (access modifier, return value declaration, and name)  Syntax: (argument) -> (body) (int a, int b) -> { return a + b; } () -> System.out.println("Hello World"); (String s) -> { System.out.println(s); } () -> 42 () -> { return 3.1415 };
  • 14.
    Lambda Example 1 //Oldway: new Thread(new Runnable() { @Override public void run() { System.out.println("Hello from thread"); } }).start(); //New way: new Thread( () -> System.out.println("Hello from thread") ).start();
  • 15.
    Lambda Example 2 //Oldway: button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("The button was clicked using old fashion code!"); } }); //New way: button.addActionListener( (e) -> { System.out.println("The button was clicked. From lambda expressions !"); });
  • 16.
    Lambda Example 3 publicstatic void main(String[] args) { int number = 5; ObjectCallback callback = () -> { return (number = number + 1); }; Object result = callback.Callback(); System.out.println(result); } number should be effectively final
  • 17.
    Conclusion  A closureis any function which closes over the environment in which it was defined.  A lambda is just an "anonymous function" - a function defined with no name.  Lambda in Java 8 is NOT closure!
  • 18.
    歐美防毒/安全軟體的領導品牌Avast Software台北辦公 室,徵一名資深Android RD(Android Tech-Lead Developer) • 與頂尖的Android RD、architect一同開發給全世界數千萬使用 者使用的app • Onboard後立即到美麗的捷克布拉格training三週以上 • 每年公司全額補助一次參加類似Google I/O的大型conference • 15天年假 +彈性工時 +(partial) remote working • 詳見 http://www.104.com.tw/job/?jobno=4hqzm&jobsource=checkc 歡迎熱愛app、熟悉OO的Android高手加入我們!