Your SlideShare is downloading. ×
Lambda expression par Christophe Huntzinger
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Lambda expression par Christophe Huntzinger

1,640
views

Published on

Auteur : Christophe Huntzinger

Auteur : Christophe Huntzinger

Published in: Technology

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,640
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Lambda expressionA JDK 8 feature,syntaxic sugar or revolution?@ FinistJUGApril 2013
  • 2. myselfChristophe HuntzingerSoftware architect and Java evangelistCurrently working @ Thales04/2013Ch. Huntzinger 2
  • 3. Some historyYou said lambda in java, but● Smalltalk said block or closure● C# said closure● “Grubby” said closureIts a block of code, that can be used a methodparameter, thread safe, and not wrapped in a classSo no more time waiting it in Java!04/2013Ch. Huntzinger 3
  • 4. Whats that, lambda?“In computer science, a closure (also lexical closure orfunction closure) is a function or reference to afunction together with a referencing environment ofthat function.A closure—unlike a plain function pointer—allows afunction to access those non-local variables evenwhen invoked outside of its immediate lexical scope.”From wikipedia04/2013Ch. Huntzinger 4
  • 5. How it could deserveExplicit function in code,provide function as method parameters,manage better concurrency04/2013Ch. Huntzinger 5
  • 6. Some old battleThe three possible shape of lambda● BGGA: Gilad Bracha, Neal Gafter, James Gosling, Peter von der Ahébutton.addActionListener(ActionEvent ev => {// handle the event});● CICE: simplified inner classesbutton.addActionListener(ActionListener(ActionEvent ev) {// handle the event});● FCM: First Class Methodbutton.addActionListener(#(ActionEvent ev) {// handle the event});04/2013Ch. Huntzinger 6
  • 7. Your first lambdaSimple shape:Argument List Body of closure(int x, int y) -> x+yCh. Huntzinger704/2013Complex shape:Argument List Body of closure: multi lines possible(int x, int y) -> {System.err.println(x+”+”y);};
  • 8. Lambda by sample● Event04/2013Ch. Huntzinger 8ActionListener al = new ActionListener(){public void actionPerformed(ActionEvent e){System.err.println(“clicked!”);}};ActionListener al = (e) -> {System.err.println(“clicked!”);};
  • 9. Lambda by sample● Runnable04/2013Ch. Huntzinger 9Runnable r = new Runnable(){public void run(){System.err.println(“Hello world!”);}};Runnable r = () -> {System.err.println(“Hello world!”);};
  • 10. Lambda under the hood● Interface & default, @FunctionalInterface● Not only an Inner class in bytecode04/2013Ch. Huntzinger 10public class SimpleSample {// Lambda Runnableprotected Runnable r2 = () -> {System.out.println("Hello world two!");}}SimpleSampleSimpleSample lambda$3@FunctionalInterfacepublic interface Function<T, R> {R apply(T t);default <V > Function<V, R> compose(Function<? super V, ? extends T> before) {Objects.requireNonNull(before);return (V v) -> apply(before.apply(v));
  • 11. New API for lambda● New Collection API: new method injava.util.Map and java.util.List to useclosure new capabilities● java.util.function: a set of interface for basicclosure instantiation. Create your owns!● java.util.stream: a set of interface to processobject’s Stream.04/2013Ch. Huntzinger 11
  • 12. advantages• More concise code• Ease concurency• Better API capabilities:o Simplify Pipelineo Simplify filteringo Explicit functional code04/2013Ch. Huntzinger 12
  • 13. Thank you!04/2013Ch. Huntzinger 13Any question?