Download Performance Development Review

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Project Lambda
or
#()(”Project Lambda”)
2010-04-29
© 2004 Capgemini - All rights reserved
Lambda Expressions (aka Closures)

“a first-class function with free variables that are bound in the lexical
environment” - Wikipedia

HUGE debate about various closures decision earlier
BGGA, CICE, FCM, …

Oracle seems to have decided this without JSR/JCP
- most likely that this will be included in Java 7

Straw-Man Proposol
- first-class functions
- function types
- lambda expression

Syntax and content not definite, may change

Complexity budget reached?
Sida 2
24 maj 2017
© 2009 Capgemini - All rights reserved
Lambda Expressions – Suggested Syntax
 A new way of writing anonymous functions
#() (42) //no args, returns 42
 A functions which take an int and returns its double
#(int x) (x+x)
Sida 3
24 maj 2017
© 2009 Capgemini - All rights reserved
Lambda Expressions
 Complex expressions
#(int x, int y){ int z = expensiveComputation(x, y);
if (z < 0) return x;
if (z > 0) return y;
return 0;
}
Sida 4
24 maj 2017
© 2009 Capgemini - All rights reserved
Function Types
 Every expression in Java must have a type, i.e. introduce function
types
#int() fortyTwo = #()(42);
#int(int) doubler = #(int x)(x + x);
 …and they could be invoked
assert fortyTwo() == 42;
assert doubler(fortyTwo()) == 84;
Sida 5
24 maj 2017
© 2009 Capgemini - All rights reserved
Functions as arguments
 Methods can take a function as an argument
public int[] map(int[] a, #int(int) fn) {
int[] b = new int[a.length];
for (int i = 0; i < a.length; i++)
b[i] = fn(a[i]);
return b;
}
Sida 6
24 maj 2017
© 2009 Capgemini - All rights reserved
SAM types
 Interfaces with just one method (SAM)
Thread th = new Thread(new Runnable() {
public void run() {
doSomeStuff();
doMoreStuff();
} });
Thread th = new Thread(#(){
doSomeStuff();
doMoreStuff(); } )
Sida 7
24 maj 2017
© 2009 Capgemini - All rights reserved
Variable Capture
shared int count = 0;
Collections.sort(data, #(String a, String b){
count++;
return a.length() - b.length()});
System.out.println(count);
Sida 8
24 maj 2017
© 2009 Capgemini - All rights reserved
Related documents