• Save
Lambda functions in java 8
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Lambda functions in java 8

on

  • 2,472 views

 

Statistics

Views

Total Views
2,472
Views on SlideShare
2,472
Embed Views
0

Actions

Likes
7
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

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
  • -Alonzo Church: formulated lambda calculus as a formal system of expressing computation by way of function composition. Functional programming languages are the most prominent counterpart to lambda calculus, as well as proof theory.-Can’t be recursive because there’s no name associated with the function. Requires an anonymous fixpoint; a higher-order function that creates a fixed point of other functions-If you assign the lambda expression to a variable name, it can then reference itselffinal Runnable r = () -> { // This reference to 'r' is legal: if (!allDone) { workQueue.add(r); } else { displayResults(); } };
  • Interfaces that have just 1 method: not including stuff inherited from Object, etc.
  • -Compiler knows that the lambda must conform to the actionPerformed() signature; sees that it returns void, and that e is an ActionEvent
  • -“this” when unqualified always means the inner class itself-For lambdas, “this” has the same meaning that it does just outside of the lambda-…meaning that the lambda can’t refer to itself directly
  • -No need to store intermediate results, but you possibly lose the clarity that comes with giving everything a name

Lambda functions in java 8 Presentation Transcript

  • 1. …in Java 8
  • 2. ...coming 2013
  • 3. What’s a lambda function? A.K.A.:  Anonymous function  Anonymous method  Function literal  Function constant  Closure Wikipedia: “a function (or a subroutine) defined, and possibly called, without being bound to an identifier” Typically can’t be recursive directly Simple example: (int x, int y) -> x + y Supported in a bunch of languages…
  • 4. Already supported in… ActionScript, C#, C++, Clojure, Curl, D, Dart, Delphi, Dylan, Erlang, F#, Frink, Go, Groovy, Haskell, JavaScript, Lisp, Logtalk, Lua, Mathematica, Maple, Matlab, Maxima, Ocaml, Octave, Objective-C, Perl, PHP, Python, R, Racket, Ruby, Scala, Scheme, Smalltalk, Vala, Visual Basic, Visual Prolog…
  • 5. More Examples x -> x + 1 (x) -> x + 1 (int x) -> x + 1 (int x, int y) -> x + y (x, y) -> x + y (x, y) -> { System.out.printf("%d + %d = %d%n", x, y, x+y); } () -> { System.out.println("I am a Runnable"); }
  • 6. Meanwhile, in Java 7… Functional Interfaces  SAM – “Single Abstract Method”  Interfaces that have just 1 method java.lang.Runnable java.util.concurrent.Callable java.security.PrivilegedAction java.util.Comparator java.io.FileFilter java.nio.file.PathMatcher java.lang.reflect.InvocationHandler java.beans.PropertyChangeListener java.awt.event.ActionListener javax.swing.event.ChangeListener
  • 7. Meanwhile, in Java 7… Typically: button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { panel.doStuff(e.getModifiers()); } }); Five lines to do one statement This would become:button.addActionListener(e -> { panel.doStuff(e.getModifiers()); });
  • 8. More Anonymous Class Issues(?) Lexical scoping  What does this mean? final Runnable r = () -> { // This reference to r is legal: if (!allDone) { workQueue.add(r); } else { displayResults(); } }; Accessing non-final variables  Lambdas: same variable access as you’d have just outside the expression
  • 9. java.util.functions PredicateList<String> names = Arrays.asList("Alice", "Bob","Charlie", "Dave");List<String> filteredNames = names .filter(e -> e.length() >= 4) .into(new ArrayList<String>());  filter: only retain elements where the predicate holds true  into: fills up an ArrayList with the filtered elements, then gets set as filteredNames
  • 10. java.util.functions Block  No more for loops…?List<String> names = Arrays.asList("Alice", "Bob","Charlie", "Dave");names .filter(e -> e.length() >= 4) .forEach(e -> { System.out.println(e); }); forEach: Can only see one element at a time Lazy element computation
  • 11. java.util.functions Method chainingList<String> names = Arrays.asList("Alice", "Bob","Charlie", "Dave");names .mapped(e -> { return e.length(); }) .asIterable() // returns an Iterable of BiValue // elements; an elements key is the // persons name, its value is the // string length .filter(e -> e.getValue() >= 4) .sorted((a, b) -> a.getValue() - b.getValue()) .forEach(e -> { System.out.println(e.getKey() + t + e.getValue()); }); Is method chaining good…?
  • 12. Method References Can pass around references to methods like you can with anonymous functions by using :: Static and instance methodsArrays.asList("Alice", "Bob", "Charlie","Dave").forEach(System.out::println); Make factories easily:Factory<Biscuit> biscuitFactory = Biscuit::new;Biscuit biscuit = biscuitFactory.make();
  • 13. Default Methods Currently, interfaces are basically “set in stone”  Altering an interface will break existing implementations Blurred line between abstract/concrete methods…?interface Foo { void bar() default { System.out.println(“baz”); }}interface AnotherFoo extends Foo { void bar() default { System.out.println(“not baz”); }}interface YetAnotherFoo extends Foo { void bar();}
  • 14. More Java 8 Stuff… http://openjdk.java.net/projects/jigsaw/ Sources:  http://java.dzone.com/news/java-8-lambda- syntax-closures  http://datumedge.blogspot.ca/2012/06/java- 8-lambdas.html  http://cr.openjdk.java.net/~briangoetz/lambd a/lambda-state-4.html