Project Lambda - Closures after all?
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Project Lambda - Closures after all?

on

  • 1,471 views

Will it be closures in Java 7 after all?

Will it be closures in Java 7 after all?

Statistics

Views

Total Views
1,471
Views on SlideShare
1,470
Embed Views
1

Actions

Likes
0
Downloads
3
Comments
0

1 Embed 1

http://www.linkedin.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Project Lambda - Closures after all? Presentation Transcript

  • 1. Project Lambda or #()(”Project Lambda”) 2010-04-29
  • 2. 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?
  • 3. Lambda Expressions – Suggested Syntax
    • A new way of writing anonymous functions
    • A functions which take an int and returns its double
    #() (42) //no args, returns 42 #(int x) (x+x)
  • 4. 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; }
  • 5. Function Types
    • Every expression in Java must have a type, i.e. introduce function types
    • … and they could be invoked
    #int() fortyTwo = #()(42); #int(int) doubler = #(int x)(x + x); assert fortyTwo() == 42; assert doubler(fortyTwo()) == 84;
  • 6. 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; }
  • 7. 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(); } )
  • 8. Variable Capture shared int count = 0; Collections.sort(data, #(String a, String b){ count++; return a.length() - b.length()}); System.out.println(count);