SlideShare a Scribd company logo
© Copyright Azul Systems 2017
© Copyright Azul Systems 2015
@speakjava
It’s Java, Jim, But Not
As We Know It!
Simon Ritter
Deputy CTO, Azul Systems
1
© Copyright Azul Systems 2017
Agenda
 Lambda expression details
 Lambda expression performance
 How far can we take lambdas?
2
Interesting
and useful
Interesting but
not so useful
© Copyright Azul Systems 2017
Java Lambda Expressions
© Copyright Azul Systems 2017
Java Lambda Expressions
 Old style, anonymous inner classes
 New style, using a Lambda expression
4
new Thread(new Runnable() {
public void run() {
doSomeStuff();
}
}).start();
new Thread(() -> doSomeStuff()).start();
© Copyright Azul Systems 2017
Java Lambda Expressions
 Simplified representation of behaviour in Java
 Assign to variable, pass as parameter
 Use wherever the type is a Functional Interface
– Much simpler than adding a function type to Java
– Single abstract method
– Not necessarily single method
 default and static methods don’t count
5
© Copyright Azul Systems 2017
Lambda Expression Syntax
 Like a method
– But not associated with a class
– Typed parameters, body, return type, exceptions
 Closure over values, not types
– Only capture effectively-final variables
6
(parameters) -> body
Lambda operator
© Copyright Azul Systems 2017
Capturing Lambdas
7
class DataProcessor {
private int currentValue;
public void process() {
DataSet myData = myFactory.getDataSet();
dataSet.forEach(d -> d.use(currentValue++));
}
}
© Copyright Azul Systems 2017
Capturing Lambdas
8
class DataProcessor {
private int currentValue;
public void process() {
DataSet myData = myFactory.getDataSet();
dataSet.forEach(d -> d.use(this.currentValue++));
}
}
Reference to this inserted
by compiler
© Copyright Azul Systems 2017
Method References
 Method references let us reuse a method as a lambda
expression
FileFilter x = File f -> f.canRead();
FileFilter x = File::canRead;
© Copyright Azul Systems 2017
Method References
 Format: target_reference::method_name
 Three kinds of method reference
– Static method
– Instance method of an arbitrary type
– Instance method of an existing object
10
© Copyright Azul Systems 2017
Method References
11
Lambda
Method Ref
Lambda
Method Ref
Lambda
Method Ref
(args) -> ClassName.staticMethod(args)
(arg0, rest) -> arg0.instanceMethod(rest)
(args) -> expr.instanceMethod(args)
ClassName::staticMethod
ClassName::instanceMethod
expr::instanceMethod
Rules For Construction
© Copyright Azul Systems 2017
Method References
(String s) -> Integer.parseInt(s);
(String s, int i) -> s.substring(i)
Axis a -> getLength(a)
Integer::parseInt
String::substring
this::getLength
Lambda
Method Ref
Lambda
Method Ref
Lambda
Method Ref
Examples
© Copyright Azul Systems 2017
Lambda Expression
Performance
© Copyright Azul Systems 2017
Lambdas & Anonymous Inner Classes
 Functionally equivalent
14
myList.forEach(w -> System.out.println(w));
myList.forEach(new Consumer<String>() {
@Override
public void accept(String w) {
System.out.println(w);
}
});
myList.forEach(System.out::println);
© Copyright Azul Systems 2017
Anonymous Inner Classes
 As the name suggests, we are dealing with classes
– Compiler generates class with name like Foo$1
– Type pollution
 The class must be loaded at run time
 Instantiated like any other class
 Lambda expressions could be implemented this way
– Originally they were
– Forces an inner class where you didn’t ask for it
 You wanted a function
15
© Copyright Azul Systems 2017
Lambda Implementation
 A better answer: invokedynamic and Method Handles
– Introduced in Java SE 7 to improve performance of
dynamically typed languages running on the JVM
– Separate binary representation of Lambda creation from
mechanics of evaluating the Lambda
– Defers implementation of the Lambda to runtime
 Not fixed in the binary format
 JVM can provide better implementations when ready
16
© Copyright Azul Systems 2017
Lambda Compilation
 Lambda body converted to method
 Non-capturing Lambda
– Simple conversion to static method in the class where the
lambda is used
 Capturing Lambdas
– Static method with captured variables prepended as
parameters
 Instance capturing Lambdas
– Synthetic instance method of class using Lambda
17
© Copyright Azul Systems 2017
Lambda Compilation
 Generate invokedynamic call (Lambda factory)
– Call site returns instance of (lambda) functional interface
 Bootstrap method for Lambda factory (meta-factory)
– java.lang.invoke.LambdaMetaFactory
– Call sites are lazily evaluated
 Factory sites that are never invoked are never linked
 Unused lambdas have no overhead
18
© Copyright Azul Systems 2017
Implementation Differences
 Lambdas
– Linkage (CallSite)
– Capture
– Invocation
19
 Anonymous inner classes
– Class loading
– Instantiation
– Invocation
 Non-capturing lambdas automatically optimise
 Method references are slightly more optimal
 -XX:+TieredCompilation gives better Lambda results
– Advice is don’t use -XX:-TieredCompilation
© Copyright Azul Systems 2017
How Far Can We Take
Lambdas?
With inspiration from Jarek Ratajski
© Copyright Azul Systems 2017 21
Alonso Church
The λ Calculus (1936)
What does this have to do with Java?
© Copyright Azul Systems 2017
Exploding Head Lambdas
 Java programmers are typically imperative programmers
 Functional programming is not imperative
– As we’ll see
 Lambda Calculus and Turing Machines are equivalent
 But will give you a headache
– At least it did me!
 What can we do only using Lambda expressions?
– And one functional interface
22
© Copyright Azul Systems 2017
Functional Interface
@FunctionalInterface
public interface Lambda {
Lambda apply(Lambda lambda);
}
© Copyright Azul Systems 2017
Function Basics
 Identity [ λx.x ]
Lambda identity = (x -> x);
Lambda identity = new Lambda() {
Lambda apply(Lambda x) {
return x;
}
};
© Copyright Azul Systems 2017
Function Basics (Booleans)
 Boolean false [ λf.λx.x ]
boolFalse = f -> (x -> x); // Always returns identity
boolFalse = new Lambda() {
Lambda apply(Lambda f) {
return new Lambda {
Lambda apply(Lambda x) {
return x;
}
}}};
© Copyright Azul Systems 2017
Function Basics (Booleans)
 Boolean true [ λf.λx.f ]
boolTrue = f -> (x -> f); // Never returns identity
© Copyright Azul Systems 2017
Church Numerals
 Zero [ λf.λx.x ]
• Identity for addition and subtraction (a ± 0 = a)
• The Lambda is the same as false
• The function, f, is applied zero times to the argument
zero = f -> (x -> x);
 One [ λf.λx.(f x) ]
one = f -> (x -> f.apply(x));
 Two [ λf.λx.(f (f x)) ]
two = f -> (x -> f.apply(f.apply(x)));
27
© Copyright Azul Systems 2017
Church Encoding
 Successor: n++ [ λn.λf.λx.f(n f x) ]
successor =
n -> f -> x -> f.apply(n.apply(f).apply(x));
28
© Copyright Azul Systems 2017
Church Encoding
 Predecessor: n--
29
predecessor = n -> f -> x ->
n.apply(g -> h -> h.apply(g.apply(f)))
.apply(u -> x).apply(u -> u);
[ λn.λf.λx.n(λg.λh.h(g f))(λu.x)(λu.u) ]
© Copyright Azul Systems 2017
Church Encoding
 Add: m + n [ λm.λn.λf.λx ((m f) ((n f) x)) ]
m -> n -> f -> x -> m.apply(f).apply(n.apply(f).apply(x))
 Subtract: m - n [ λm.λn.(n predecessor) m ]
m -> n -> m.apply(predecessor).apply(n)
30
© Copyright Azul Systems 2017
Solving 2 + 2 With Lambdas
Lambda two = f -> x -> f.apply(f.apply(x));
Lambda plus = m -> n ->
f -> x -> m.apply(f).apply(n.apply(f).apply(x));
Lambda four = plus.apply(two).apply(two);
4 = + 2 2 (Polish notation)
© Copyright Azul Systems 2017
Solving 2 + 2 With Lambdas
m -> n -> f -> x -> m.apply(f).apply(n.apply(f).apply(x))
n -> f -> x -> f -> x -> f.apply(f.apply(x)).apply(f)
.apply(n.apply(f).apply(x))
© Copyright Azul Systems 2017
Solving 2 + 2 With Lambdas
m -> n -> f -> x -> m.apply(f).apply(n.apply(f).apply(x))
n -> f -> x -> f -> x -> f.apply(f.apply(x)).apply(f)
.apply(n.apply(f).apply(x))
f -> x -> f -> x -> f.apply(f.apply(x)).apply(f)
.apply(f -> x -> f.apply(f.apply(x).apply(f).apply(x))
© Copyright Azul Systems 2017
Solving 2 + 2 With Lambdas
f -> x ->
f -> x -> f.apply(f.apply(x)).apply(f)
.apply(f -> x -> f.apply(f.apply(x)).apply(f).apply(x))
f -> x ->
x -> f.apply(f.apply(x))
.apply(f -> x -> f.apply(f.apply(x)).apply(f).apply(x))
© Copyright Azul Systems 2017
Solving 2 + 2 With Lambdas
f -> x ->
f -> x -> f.apply(f.apply(x)).apply(f)
.apply(f -> x -> f.apply(f.apply(x)).apply(f).apply(x))
f -> x ->
x -> f.apply(f.apply(x))
.apply(f -> x -> f.apply(f.apply(x)).apply(f).apply(x))
f -> x ->
x -> f.apply(f.apply(x))
.apply(x -> f.apply(f.apply(x)).apply(x))
© Copyright Azul Systems 2017
Solving 2 + 2 With Lambdas
f -> x -> x -> f.apply(f.apply(x))
.apply(x -> f.apply(f.apply(x)).apply(x))
f -> x -> x -> f.apply(f.apply(x))
.apply(f.apply(f.apply(x)))
f -> x -> x -> f.apply(f.apply(x))
.apply(f.apply(f.apply(x)))
f -> x -> f.apply(f.apply(f.apply(f.apply(x))) = 4!
© Copyright Azul Systems 2017
Summary
© Copyright Azul Systems 2017
Lambda Expressions
 Very useful and powerful
– Succinct way to parameterise behaviour
 Better performance than anonymous inner class
– Invokedynamic implementation
 Can be used in weird and wonderful ways
– Not necessarily to be recommended!
38
© Copyright Azul Systems 2017
More Information
 Dixin Yan’s blog
– weblogs.asp.net/dixin (C# based, but useful)
– October 2016
 Jarek’s presentation from Voxxed Zurich
– https://www.youtube.com/watch?v=Dun8ewSeX6c
39
© Copyright Azul Systems 2017
© Copyright Azul Systems 2015
@speakjava
It’s Java, Jim, But Not
As We Know It!
Simon Ritter
Deputy CTO, Azul Systems
40

More Related Content

What's hot

Kotlin Receiver Types 介紹
Kotlin Receiver Types 介紹Kotlin Receiver Types 介紹
Kotlin Receiver Types 介紹Kros Huang
 
Unit testing of spark applications
Unit testing of spark applicationsUnit testing of spark applications
Unit testing of spark applicationsKnoldus Inc.
 
Rxjava 介紹與 Android 中的 RxJava
Rxjava 介紹與 Android 中的 RxJavaRxjava 介紹與 Android 中的 RxJava
Rxjava 介紹與 Android 中的 RxJavaKros Huang
 
Reactive programming with RxJava
Reactive programming with RxJavaReactive programming with RxJava
Reactive programming with RxJavaJobaer Chowdhury
 
Brief introduction on Hadoop,Dremel, Pig, FlumeJava and Cassandra
Brief introduction on Hadoop,Dremel, Pig, FlumeJava and CassandraBrief introduction on Hadoop,Dremel, Pig, FlumeJava and Cassandra
Brief introduction on Hadoop,Dremel, Pig, FlumeJava and CassandraSomnath Mazumdar
 
Enabling Vectorized Engine in Apache Spark
Enabling Vectorized Engine in Apache SparkEnabling Vectorized Engine in Apache Spark
Enabling Vectorized Engine in Apache SparkKazuaki Ishizaki
 
Practical RxJava for Android
Practical RxJava for AndroidPractical RxJava for Android
Practical RxJava for AndroidTomáš Kypta
 
whats new in java 8
whats new in java 8 whats new in java 8
whats new in java 8 Dori Waldman
 
Shooting the Rapids: Getting the Best from Java 8 Streams
Shooting the Rapids: Getting the Best from Java 8 StreamsShooting the Rapids: Getting the Best from Java 8 Streams
Shooting the Rapids: Getting the Best from Java 8 StreamsMaurice Naftalin
 
How to Think in RxJava Before Reacting
How to Think in RxJava Before ReactingHow to Think in RxJava Before Reacting
How to Think in RxJava Before ReactingIndicThreads
 
Reactive programming on Android
Reactive programming on AndroidReactive programming on Android
Reactive programming on AndroidTomáš Kypta
 
あなたのScalaを爆速にする7つの方法
あなたのScalaを爆速にする7つの方法あなたのScalaを爆速にする7つの方法
あなたのScalaを爆速にする7つの方法x1 ichi
 
Good and Wicked Fairies, and the Tragedy of the Commons: Understanding the Pe...
Good and Wicked Fairies, and the Tragedy of the Commons: Understanding the Pe...Good and Wicked Fairies, and the Tragedy of the Commons: Understanding the Pe...
Good and Wicked Fairies, and the Tragedy of the Commons: Understanding the Pe...Maurice Naftalin
 
Introduction to ScalaZ
Introduction to ScalaZIntroduction to ScalaZ
Introduction to ScalaZKnoldus Inc.
 
Building Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJavaBuilding Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJavaRick Warren
 
Stream processing from single node to a cluster
Stream processing from single node to a clusterStream processing from single node to a cluster
Stream processing from single node to a clusterGal Marder
 
JDK 9, 10, 11 and Beyond
JDK 9, 10, 11 and BeyondJDK 9, 10, 11 and Beyond
JDK 9, 10, 11 and BeyondSimon Ritter
 
Parallel-Ready Java Code: Managing Mutation in an Imperative Language
Parallel-Ready Java Code: Managing Mutation in an Imperative LanguageParallel-Ready Java Code: Managing Mutation in an Imperative Language
Parallel-Ready Java Code: Managing Mutation in an Imperative LanguageMaurice Naftalin
 

What's hot (20)

Kotlin Receiver Types 介紹
Kotlin Receiver Types 介紹Kotlin Receiver Types 介紹
Kotlin Receiver Types 介紹
 
Unit testing of spark applications
Unit testing of spark applicationsUnit testing of spark applications
Unit testing of spark applications
 
Rxjava 介紹與 Android 中的 RxJava
Rxjava 介紹與 Android 中的 RxJavaRxjava 介紹與 Android 中的 RxJava
Rxjava 介紹與 Android 中的 RxJava
 
Reactive programming with RxJava
Reactive programming with RxJavaReactive programming with RxJava
Reactive programming with RxJava
 
Brief introduction on Hadoop,Dremel, Pig, FlumeJava and Cassandra
Brief introduction on Hadoop,Dremel, Pig, FlumeJava and CassandraBrief introduction on Hadoop,Dremel, Pig, FlumeJava and Cassandra
Brief introduction on Hadoop,Dremel, Pig, FlumeJava and Cassandra
 
Enabling Vectorized Engine in Apache Spark
Enabling Vectorized Engine in Apache SparkEnabling Vectorized Engine in Apache Spark
Enabling Vectorized Engine in Apache Spark
 
Practical RxJava for Android
Practical RxJava for AndroidPractical RxJava for Android
Practical RxJava for Android
 
whats new in java 8
whats new in java 8 whats new in java 8
whats new in java 8
 
Shooting the Rapids: Getting the Best from Java 8 Streams
Shooting the Rapids: Getting the Best from Java 8 StreamsShooting the Rapids: Getting the Best from Java 8 Streams
Shooting the Rapids: Getting the Best from Java 8 Streams
 
Shooting the Rapids
Shooting the RapidsShooting the Rapids
Shooting the Rapids
 
How to Think in RxJava Before Reacting
How to Think in RxJava Before ReactingHow to Think in RxJava Before Reacting
How to Think in RxJava Before Reacting
 
Reactive programming on Android
Reactive programming on AndroidReactive programming on Android
Reactive programming on Android
 
Let's Get to the Rapids
Let's Get to the RapidsLet's Get to the Rapids
Let's Get to the Rapids
 
あなたのScalaを爆速にする7つの方法
あなたのScalaを爆速にする7つの方法あなたのScalaを爆速にする7つの方法
あなたのScalaを爆速にする7つの方法
 
Good and Wicked Fairies, and the Tragedy of the Commons: Understanding the Pe...
Good and Wicked Fairies, and the Tragedy of the Commons: Understanding the Pe...Good and Wicked Fairies, and the Tragedy of the Commons: Understanding the Pe...
Good and Wicked Fairies, and the Tragedy of the Commons: Understanding the Pe...
 
Introduction to ScalaZ
Introduction to ScalaZIntroduction to ScalaZ
Introduction to ScalaZ
 
Building Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJavaBuilding Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJava
 
Stream processing from single node to a cluster
Stream processing from single node to a clusterStream processing from single node to a cluster
Stream processing from single node to a cluster
 
JDK 9, 10, 11 and Beyond
JDK 9, 10, 11 and BeyondJDK 9, 10, 11 and Beyond
JDK 9, 10, 11 and Beyond
 
Parallel-Ready Java Code: Managing Mutation in an Imperative Language
Parallel-Ready Java Code: Managing Mutation in an Imperative LanguageParallel-Ready Java Code: Managing Mutation in an Imperative Language
Parallel-Ready Java Code: Managing Mutation in an Imperative Language
 

Similar to It's Java, Jim, but not as we know it

What's new in java 8
What's new in java 8What's new in java 8
What's new in java 8Dian Aditya
 
Lessons Learnt With Lambdas and Streams in JDK 8
Lessons Learnt With Lambdas and Streams in JDK 8Lessons Learnt With Lambdas and Streams in JDK 8
Lessons Learnt With Lambdas and Streams in JDK 8Simon Ritter
 
Reproducibility with R
Reproducibility with RReproducibility with R
Reproducibility with RMartin Jung
 
Lambda expressions
Lambda expressionsLambda expressions
Lambda expressionsDoron Gold
 
Java8: what's new and what's hot
Java8: what's new and what's hotJava8: what's new and what's hot
Java8: what's new and what's hotSergii Maliarov
 
Functional Programming With Lambdas and Streams in JDK8
 Functional Programming With Lambdas and Streams in JDK8 Functional Programming With Lambdas and Streams in JDK8
Functional Programming With Lambdas and Streams in JDK8IndicThreads
 
Kicking off with Zend Expressive and Doctrine ORM (PHP South Africa 2018)
Kicking off with Zend Expressive and Doctrine ORM (PHP South Africa 2018)Kicking off with Zend Expressive and Doctrine ORM (PHP South Africa 2018)
Kicking off with Zend Expressive and Doctrine ORM (PHP South Africa 2018)James Titcumb
 
Project Lambda: Functional Programming Constructs in Java - Simon Ritter (Ora...
Project Lambda: Functional Programming Constructs in Java - Simon Ritter (Ora...Project Lambda: Functional Programming Constructs in Java - Simon Ritter (Ora...
Project Lambda: Functional Programming Constructs in Java - Simon Ritter (Ora...jaxLondonConference
 
New features in jdk8 iti
New features in jdk8 itiNew features in jdk8 iti
New features in jdk8 itiAhmed mar3y
 
JDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
JDK8 Lambdas and Streams: Changing The Way You Think When Developing JavaJDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
JDK8 Lambdas and Streams: Changing The Way You Think When Developing JavaSimon Ritter
 
Kicking off with Zend Expressive and Doctrine ORM (PHP MiNDS March 2018)
Kicking off with Zend Expressive and Doctrine ORM (PHP MiNDS March 2018)Kicking off with Zend Expressive and Doctrine ORM (PHP MiNDS March 2018)
Kicking off with Zend Expressive and Doctrine ORM (PHP MiNDS March 2018)James Titcumb
 
Effective Java, Third Edition - Keepin' it Effective
Effective Java, Third Edition - Keepin' it EffectiveEffective Java, Third Edition - Keepin' it Effective
Effective Java, Third Edition - Keepin' it EffectiveC4Media
 
Big Data, Analytics and Machine Learning on AWS Lambda - SRV402 - re:Invent 2017
Big Data, Analytics and Machine Learning on AWS Lambda - SRV402 - re:Invent 2017Big Data, Analytics and Machine Learning on AWS Lambda - SRV402 - re:Invent 2017
Big Data, Analytics and Machine Learning on AWS Lambda - SRV402 - re:Invent 2017Amazon Web Services
 
PHP 5.3 Part 2 - Lambda Functions & Closures
PHP 5.3 Part 2 - Lambda Functions & ClosuresPHP 5.3 Part 2 - Lambda Functions & Closures
PHP 5.3 Part 2 - Lambda Functions & Closuresmelechi
 

Similar to It's Java, Jim, but not as we know it (20)

What's new in java 8
What's new in java 8What's new in java 8
What's new in java 8
 
Lessons Learnt With Lambdas and Streams in JDK 8
Lessons Learnt With Lambdas and Streams in JDK 8Lessons Learnt With Lambdas and Streams in JDK 8
Lessons Learnt With Lambdas and Streams in JDK 8
 
Reproducibility with R
Reproducibility with RReproducibility with R
Reproducibility with R
 
Lambda expressions
Lambda expressionsLambda expressions
Lambda expressions
 
Apouc 2014-java-8-create-the-future
Apouc 2014-java-8-create-the-futureApouc 2014-java-8-create-the-future
Apouc 2014-java-8-create-the-future
 
Java 8
Java 8Java 8
Java 8
 
What`s New in Java 8
What`s New in Java 8What`s New in Java 8
What`s New in Java 8
 
Java8: what's new and what's hot
Java8: what's new and what's hotJava8: what's new and what's hot
Java8: what's new and what's hot
 
Functional Programming With Lambdas and Streams in JDK8
 Functional Programming With Lambdas and Streams in JDK8 Functional Programming With Lambdas and Streams in JDK8
Functional Programming With Lambdas and Streams in JDK8
 
Kicking off with Zend Expressive and Doctrine ORM (PHP South Africa 2018)
Kicking off with Zend Expressive and Doctrine ORM (PHP South Africa 2018)Kicking off with Zend Expressive and Doctrine ORM (PHP South Africa 2018)
Kicking off with Zend Expressive and Doctrine ORM (PHP South Africa 2018)
 
Project Lambda: Functional Programming Constructs in Java - Simon Ritter (Ora...
Project Lambda: Functional Programming Constructs in Java - Simon Ritter (Ora...Project Lambda: Functional Programming Constructs in Java - Simon Ritter (Ora...
Project Lambda: Functional Programming Constructs in Java - Simon Ritter (Ora...
 
New features in jdk8 iti
New features in jdk8 itiNew features in jdk8 iti
New features in jdk8 iti
 
JDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
JDK8 Lambdas and Streams: Changing The Way You Think When Developing JavaJDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
JDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
 
Lambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter LawreyLambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter Lawrey
 
Kicking off with Zend Expressive and Doctrine ORM (PHP MiNDS March 2018)
Kicking off with Zend Expressive and Doctrine ORM (PHP MiNDS March 2018)Kicking off with Zend Expressive and Doctrine ORM (PHP MiNDS March 2018)
Kicking off with Zend Expressive and Doctrine ORM (PHP MiNDS March 2018)
 
Effective Java, Third Edition - Keepin' it Effective
Effective Java, Third Edition - Keepin' it EffectiveEffective Java, Third Edition - Keepin' it Effective
Effective Java, Third Edition - Keepin' it Effective
 
Big Data, Analytics and Machine Learning on AWS Lambda - SRV402 - re:Invent 2017
Big Data, Analytics and Machine Learning on AWS Lambda - SRV402 - re:Invent 2017Big Data, Analytics and Machine Learning on AWS Lambda - SRV402 - re:Invent 2017
Big Data, Analytics and Machine Learning on AWS Lambda - SRV402 - re:Invent 2017
 
Java 8 Workshop
Java 8 WorkshopJava 8 Workshop
Java 8 Workshop
 
PHP 5.3 Part 2 - Lambda Functions & Closures
PHP 5.3 Part 2 - Lambda Functions & ClosuresPHP 5.3 Part 2 - Lambda Functions & Closures
PHP 5.3 Part 2 - Lambda Functions & Closures
 
Java8
Java8Java8
Java8
 

More from Simon Ritter

Cloud Native Compiler
Cloud Native CompilerCloud Native Compiler
Cloud Native CompilerSimon Ritter
 
The Art of Java Type Patterns
The Art of Java Type PatternsThe Art of Java Type Patterns
The Art of Java Type PatternsSimon Ritter
 
Java performance monitoring
Java performance monitoringJava performance monitoring
Java performance monitoringSimon Ritter
 
Modern Java Workshop
Modern Java WorkshopModern Java Workshop
Modern Java WorkshopSimon Ritter
 
Building a Better JVM
Building a Better JVMBuilding a Better JVM
Building a Better JVMSimon Ritter
 
JDK 14 Lots of New Features
JDK 14 Lots of New FeaturesJDK 14 Lots of New Features
JDK 14 Lots of New FeaturesSimon Ritter
 
The Latest in Enterprise JavaBeans Technology
The Latest in Enterprise JavaBeans TechnologyThe Latest in Enterprise JavaBeans Technology
The Latest in Enterprise JavaBeans TechnologySimon Ritter
 
Developing Enterprise Applications Using Java Technology
Developing Enterprise Applications Using Java TechnologyDeveloping Enterprise Applications Using Java Technology
Developing Enterprise Applications Using Java TechnologySimon Ritter
 
Is Java Still Free?
Is Java Still Free?Is Java Still Free?
Is Java Still Free?Simon Ritter
 
Moving Towards JDK 12
Moving Towards JDK 12Moving Towards JDK 12
Moving Towards JDK 12Simon Ritter
 
JDK 9, 10, 11 and Beyond
JDK 9, 10, 11 and BeyondJDK 9, 10, 11 and Beyond
JDK 9, 10, 11 and BeyondSimon Ritter
 
Java Is Still Free
Java Is Still FreeJava Is Still Free
Java Is Still FreeSimon Ritter
 
JDK 9 and JDK 10 Deep Dive
JDK 9 and JDK 10 Deep DiveJDK 9 and JDK 10 Deep Dive
JDK 9 and JDK 10 Deep DiveSimon Ritter
 
Java Support: What's changing
Java Support:  What's changingJava Support:  What's changing
Java Support: What's changingSimon Ritter
 
JDK 9: The Start of a New Future for Java
JDK 9: The Start of a New Future for JavaJDK 9: The Start of a New Future for Java
JDK 9: The Start of a New Future for JavaSimon Ritter
 
JDK 9: Mission Accomplished. What Next For Java?
JDK 9: Mission Accomplished. What Next For Java?JDK 9: Mission Accomplished. What Next For Java?
JDK 9: Mission Accomplished. What Next For Java?Simon Ritter
 
JDK 9: Migrating Applications
JDK 9: Migrating ApplicationsJDK 9: Migrating Applications
JDK 9: Migrating ApplicationsSimon Ritter
 
Building a Brain with Raspberry Pi and Zulu Embedded JVM
Building a Brain with Raspberry Pi and Zulu Embedded JVMBuilding a Brain with Raspberry Pi and Zulu Embedded JVM
Building a Brain with Raspberry Pi and Zulu Embedded JVMSimon Ritter
 

More from Simon Ritter (20)

Cloud Native Compiler
Cloud Native CompilerCloud Native Compiler
Cloud Native Compiler
 
Java On CRaC
Java On CRaCJava On CRaC
Java On CRaC
 
The Art of Java Type Patterns
The Art of Java Type PatternsThe Art of Java Type Patterns
The Art of Java Type Patterns
 
Java performance monitoring
Java performance monitoringJava performance monitoring
Java performance monitoring
 
Modern Java Workshop
Modern Java WorkshopModern Java Workshop
Modern Java Workshop
 
Building a Better JVM
Building a Better JVMBuilding a Better JVM
Building a Better JVM
 
JDK 14 Lots of New Features
JDK 14 Lots of New FeaturesJDK 14 Lots of New Features
JDK 14 Lots of New Features
 
The Latest in Enterprise JavaBeans Technology
The Latest in Enterprise JavaBeans TechnologyThe Latest in Enterprise JavaBeans Technology
The Latest in Enterprise JavaBeans Technology
 
Developing Enterprise Applications Using Java Technology
Developing Enterprise Applications Using Java TechnologyDeveloping Enterprise Applications Using Java Technology
Developing Enterprise Applications Using Java Technology
 
Is Java Still Free?
Is Java Still Free?Is Java Still Free?
Is Java Still Free?
 
Moving Towards JDK 12
Moving Towards JDK 12Moving Towards JDK 12
Moving Towards JDK 12
 
JDK 9, 10, 11 and Beyond
JDK 9, 10, 11 and BeyondJDK 9, 10, 11 and Beyond
JDK 9, 10, 11 and Beyond
 
Java Is Still Free
Java Is Still FreeJava Is Still Free
Java Is Still Free
 
JDK 9 and JDK 10 Deep Dive
JDK 9 and JDK 10 Deep DiveJDK 9 and JDK 10 Deep Dive
JDK 9 and JDK 10 Deep Dive
 
JDK 9 Deep Dive
JDK 9 Deep DiveJDK 9 Deep Dive
JDK 9 Deep Dive
 
Java Support: What's changing
Java Support:  What's changingJava Support:  What's changing
Java Support: What's changing
 
JDK 9: The Start of a New Future for Java
JDK 9: The Start of a New Future for JavaJDK 9: The Start of a New Future for Java
JDK 9: The Start of a New Future for Java
 
JDK 9: Mission Accomplished. What Next For Java?
JDK 9: Mission Accomplished. What Next For Java?JDK 9: Mission Accomplished. What Next For Java?
JDK 9: Mission Accomplished. What Next For Java?
 
JDK 9: Migrating Applications
JDK 9: Migrating ApplicationsJDK 9: Migrating Applications
JDK 9: Migrating Applications
 
Building a Brain with Raspberry Pi and Zulu Embedded JVM
Building a Brain with Raspberry Pi and Zulu Embedded JVMBuilding a Brain with Raspberry Pi and Zulu Embedded JVM
Building a Brain with Raspberry Pi and Zulu Embedded JVM
 

Recently uploaded

Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...rajkumar669520
 
Studiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting softwareStudiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting softwareinfo611746
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...informapgpstrackings
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamtakuyayamamoto1800
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Anthony Dahanne
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILNatan Silnitsky
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownloadvrstrong314
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyanic lab
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?XfilesPro
 
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAGAI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAGAlluxio, Inc.
 
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...Alluxio, Inc.
 
Crafting the Perfect Measurement Sheet with PLM Integration
Crafting the Perfect Measurement Sheet with PLM IntegrationCrafting the Perfect Measurement Sheet with PLM Integration
Crafting the Perfect Measurement Sheet with PLM IntegrationWave PLM
 
iGaming Platform & Lottery Solutions by Skilrock
iGaming Platform & Lottery Solutions by SkilrockiGaming Platform & Lottery Solutions by Skilrock
iGaming Platform & Lottery Solutions by SkilrockSkilrock Technologies
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfOrtus Solutions, Corp
 
Designing for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web ServicesDesigning for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web ServicesKrzysztofKkol1
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar
 
GraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysisGraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysisNeo4j
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke
 

Recently uploaded (20)

Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
 
Studiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting softwareStudiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting software
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
 
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAGAI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
 
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
 
Crafting the Perfect Measurement Sheet with PLM Integration
Crafting the Perfect Measurement Sheet with PLM IntegrationCrafting the Perfect Measurement Sheet with PLM Integration
Crafting the Perfect Measurement Sheet with PLM Integration
 
iGaming Platform & Lottery Solutions by Skilrock
iGaming Platform & Lottery Solutions by SkilrockiGaming Platform & Lottery Solutions by Skilrock
iGaming Platform & Lottery Solutions by Skilrock
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
Designing for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web ServicesDesigning for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web Services
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
GraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysisGraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysis
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 

It's Java, Jim, but not as we know it

  • 1. © Copyright Azul Systems 2017 © Copyright Azul Systems 2015 @speakjava It’s Java, Jim, But Not As We Know It! Simon Ritter Deputy CTO, Azul Systems 1
  • 2. © Copyright Azul Systems 2017 Agenda  Lambda expression details  Lambda expression performance  How far can we take lambdas? 2 Interesting and useful Interesting but not so useful
  • 3. © Copyright Azul Systems 2017 Java Lambda Expressions
  • 4. © Copyright Azul Systems 2017 Java Lambda Expressions  Old style, anonymous inner classes  New style, using a Lambda expression 4 new Thread(new Runnable() { public void run() { doSomeStuff(); } }).start(); new Thread(() -> doSomeStuff()).start();
  • 5. © Copyright Azul Systems 2017 Java Lambda Expressions  Simplified representation of behaviour in Java  Assign to variable, pass as parameter  Use wherever the type is a Functional Interface – Much simpler than adding a function type to Java – Single abstract method – Not necessarily single method  default and static methods don’t count 5
  • 6. © Copyright Azul Systems 2017 Lambda Expression Syntax  Like a method – But not associated with a class – Typed parameters, body, return type, exceptions  Closure over values, not types – Only capture effectively-final variables 6 (parameters) -> body Lambda operator
  • 7. © Copyright Azul Systems 2017 Capturing Lambdas 7 class DataProcessor { private int currentValue; public void process() { DataSet myData = myFactory.getDataSet(); dataSet.forEach(d -> d.use(currentValue++)); } }
  • 8. © Copyright Azul Systems 2017 Capturing Lambdas 8 class DataProcessor { private int currentValue; public void process() { DataSet myData = myFactory.getDataSet(); dataSet.forEach(d -> d.use(this.currentValue++)); } } Reference to this inserted by compiler
  • 9. © Copyright Azul Systems 2017 Method References  Method references let us reuse a method as a lambda expression FileFilter x = File f -> f.canRead(); FileFilter x = File::canRead;
  • 10. © Copyright Azul Systems 2017 Method References  Format: target_reference::method_name  Three kinds of method reference – Static method – Instance method of an arbitrary type – Instance method of an existing object 10
  • 11. © Copyright Azul Systems 2017 Method References 11 Lambda Method Ref Lambda Method Ref Lambda Method Ref (args) -> ClassName.staticMethod(args) (arg0, rest) -> arg0.instanceMethod(rest) (args) -> expr.instanceMethod(args) ClassName::staticMethod ClassName::instanceMethod expr::instanceMethod Rules For Construction
  • 12. © Copyright Azul Systems 2017 Method References (String s) -> Integer.parseInt(s); (String s, int i) -> s.substring(i) Axis a -> getLength(a) Integer::parseInt String::substring this::getLength Lambda Method Ref Lambda Method Ref Lambda Method Ref Examples
  • 13. © Copyright Azul Systems 2017 Lambda Expression Performance
  • 14. © Copyright Azul Systems 2017 Lambdas & Anonymous Inner Classes  Functionally equivalent 14 myList.forEach(w -> System.out.println(w)); myList.forEach(new Consumer<String>() { @Override public void accept(String w) { System.out.println(w); } }); myList.forEach(System.out::println);
  • 15. © Copyright Azul Systems 2017 Anonymous Inner Classes  As the name suggests, we are dealing with classes – Compiler generates class with name like Foo$1 – Type pollution  The class must be loaded at run time  Instantiated like any other class  Lambda expressions could be implemented this way – Originally they were – Forces an inner class where you didn’t ask for it  You wanted a function 15
  • 16. © Copyright Azul Systems 2017 Lambda Implementation  A better answer: invokedynamic and Method Handles – Introduced in Java SE 7 to improve performance of dynamically typed languages running on the JVM – Separate binary representation of Lambda creation from mechanics of evaluating the Lambda – Defers implementation of the Lambda to runtime  Not fixed in the binary format  JVM can provide better implementations when ready 16
  • 17. © Copyright Azul Systems 2017 Lambda Compilation  Lambda body converted to method  Non-capturing Lambda – Simple conversion to static method in the class where the lambda is used  Capturing Lambdas – Static method with captured variables prepended as parameters  Instance capturing Lambdas – Synthetic instance method of class using Lambda 17
  • 18. © Copyright Azul Systems 2017 Lambda Compilation  Generate invokedynamic call (Lambda factory) – Call site returns instance of (lambda) functional interface  Bootstrap method for Lambda factory (meta-factory) – java.lang.invoke.LambdaMetaFactory – Call sites are lazily evaluated  Factory sites that are never invoked are never linked  Unused lambdas have no overhead 18
  • 19. © Copyright Azul Systems 2017 Implementation Differences  Lambdas – Linkage (CallSite) – Capture – Invocation 19  Anonymous inner classes – Class loading – Instantiation – Invocation  Non-capturing lambdas automatically optimise  Method references are slightly more optimal  -XX:+TieredCompilation gives better Lambda results – Advice is don’t use -XX:-TieredCompilation
  • 20. © Copyright Azul Systems 2017 How Far Can We Take Lambdas? With inspiration from Jarek Ratajski
  • 21. © Copyright Azul Systems 2017 21 Alonso Church The λ Calculus (1936) What does this have to do with Java?
  • 22. © Copyright Azul Systems 2017 Exploding Head Lambdas  Java programmers are typically imperative programmers  Functional programming is not imperative – As we’ll see  Lambda Calculus and Turing Machines are equivalent  But will give you a headache – At least it did me!  What can we do only using Lambda expressions? – And one functional interface 22
  • 23. © Copyright Azul Systems 2017 Functional Interface @FunctionalInterface public interface Lambda { Lambda apply(Lambda lambda); }
  • 24. © Copyright Azul Systems 2017 Function Basics  Identity [ λx.x ] Lambda identity = (x -> x); Lambda identity = new Lambda() { Lambda apply(Lambda x) { return x; } };
  • 25. © Copyright Azul Systems 2017 Function Basics (Booleans)  Boolean false [ λf.λx.x ] boolFalse = f -> (x -> x); // Always returns identity boolFalse = new Lambda() { Lambda apply(Lambda f) { return new Lambda { Lambda apply(Lambda x) { return x; } }}};
  • 26. © Copyright Azul Systems 2017 Function Basics (Booleans)  Boolean true [ λf.λx.f ] boolTrue = f -> (x -> f); // Never returns identity
  • 27. © Copyright Azul Systems 2017 Church Numerals  Zero [ λf.λx.x ] • Identity for addition and subtraction (a ± 0 = a) • The Lambda is the same as false • The function, f, is applied zero times to the argument zero = f -> (x -> x);  One [ λf.λx.(f x) ] one = f -> (x -> f.apply(x));  Two [ λf.λx.(f (f x)) ] two = f -> (x -> f.apply(f.apply(x))); 27
  • 28. © Copyright Azul Systems 2017 Church Encoding  Successor: n++ [ λn.λf.λx.f(n f x) ] successor = n -> f -> x -> f.apply(n.apply(f).apply(x)); 28
  • 29. © Copyright Azul Systems 2017 Church Encoding  Predecessor: n-- 29 predecessor = n -> f -> x -> n.apply(g -> h -> h.apply(g.apply(f))) .apply(u -> x).apply(u -> u); [ λn.λf.λx.n(λg.λh.h(g f))(λu.x)(λu.u) ]
  • 30. © Copyright Azul Systems 2017 Church Encoding  Add: m + n [ λm.λn.λf.λx ((m f) ((n f) x)) ] m -> n -> f -> x -> m.apply(f).apply(n.apply(f).apply(x))  Subtract: m - n [ λm.λn.(n predecessor) m ] m -> n -> m.apply(predecessor).apply(n) 30
  • 31. © Copyright Azul Systems 2017 Solving 2 + 2 With Lambdas Lambda two = f -> x -> f.apply(f.apply(x)); Lambda plus = m -> n -> f -> x -> m.apply(f).apply(n.apply(f).apply(x)); Lambda four = plus.apply(two).apply(two); 4 = + 2 2 (Polish notation)
  • 32. © Copyright Azul Systems 2017 Solving 2 + 2 With Lambdas m -> n -> f -> x -> m.apply(f).apply(n.apply(f).apply(x)) n -> f -> x -> f -> x -> f.apply(f.apply(x)).apply(f) .apply(n.apply(f).apply(x))
  • 33. © Copyright Azul Systems 2017 Solving 2 + 2 With Lambdas m -> n -> f -> x -> m.apply(f).apply(n.apply(f).apply(x)) n -> f -> x -> f -> x -> f.apply(f.apply(x)).apply(f) .apply(n.apply(f).apply(x)) f -> x -> f -> x -> f.apply(f.apply(x)).apply(f) .apply(f -> x -> f.apply(f.apply(x).apply(f).apply(x))
  • 34. © Copyright Azul Systems 2017 Solving 2 + 2 With Lambdas f -> x -> f -> x -> f.apply(f.apply(x)).apply(f) .apply(f -> x -> f.apply(f.apply(x)).apply(f).apply(x)) f -> x -> x -> f.apply(f.apply(x)) .apply(f -> x -> f.apply(f.apply(x)).apply(f).apply(x))
  • 35. © Copyright Azul Systems 2017 Solving 2 + 2 With Lambdas f -> x -> f -> x -> f.apply(f.apply(x)).apply(f) .apply(f -> x -> f.apply(f.apply(x)).apply(f).apply(x)) f -> x -> x -> f.apply(f.apply(x)) .apply(f -> x -> f.apply(f.apply(x)).apply(f).apply(x)) f -> x -> x -> f.apply(f.apply(x)) .apply(x -> f.apply(f.apply(x)).apply(x))
  • 36. © Copyright Azul Systems 2017 Solving 2 + 2 With Lambdas f -> x -> x -> f.apply(f.apply(x)) .apply(x -> f.apply(f.apply(x)).apply(x)) f -> x -> x -> f.apply(f.apply(x)) .apply(f.apply(f.apply(x))) f -> x -> x -> f.apply(f.apply(x)) .apply(f.apply(f.apply(x))) f -> x -> f.apply(f.apply(f.apply(f.apply(x))) = 4!
  • 37. © Copyright Azul Systems 2017 Summary
  • 38. © Copyright Azul Systems 2017 Lambda Expressions  Very useful and powerful – Succinct way to parameterise behaviour  Better performance than anonymous inner class – Invokedynamic implementation  Can be used in weird and wonderful ways – Not necessarily to be recommended! 38
  • 39. © Copyright Azul Systems 2017 More Information  Dixin Yan’s blog – weblogs.asp.net/dixin (C# based, but useful) – October 2016  Jarek’s presentation from Voxxed Zurich – https://www.youtube.com/watch?v=Dun8ewSeX6c 39
  • 40. © Copyright Azul Systems 2017 © Copyright Azul Systems 2015 @speakjava It’s Java, Jim, But Not As We Know It! Simon Ritter Deputy CTO, Azul Systems 40

Editor's Notes

  1. ISOMORPHIC Libraries are backwardly compatible