Lambda Expressions of Java 8
Table of Contents
1 Abstract
Lambda Expressions, method refs, functional interfaces, etc. of Java 8.
2 Background
2.1 First Class Entities
- Entities: any "thing" that can be defined in the prog language
- values of builtin types
- labels (targets of goto)
- functions, methods, a section of code
- type defs
- First-Class entities can be: (i) passed as a parameter; (ii) returned from a function; (iii) assigned to a variable;
- Second-Class entities can be: (i), not (ii), not (iii)
- Third-Class entities can be: none of (i) (ii) (iii)
2.2 Lexical and Dynamic Scoping
- Consider the example below,
int n := 123; // written in pseudo code define meenie() { print("meenie: " + n); } define minie(int n) { print("minie: " + n); meenie(); // which n does this use? } print("main: " + n); minie(456); meenie();
- main: 123 minie: 456 meenie: 123 meenie: 123 // lexical scoping
- main: 123 minie: 456 meenie: 456 meenie: 123 // dynamic scoping
2.3 Closures
- In a block, bound variables are those decalred within. All others (i.e., declared outside) are free variables.
- An environment is a table of triplets: the name of a variable, its declaration, its current value. Nested scopes give rise to a stack of environments.
- A closure is a block of code that can be referenced and passed around. In general, it has free variables. These free variables are yield the values they have in that environment, regardless of where this closure is evaluated.
- Def: Closures are procedure contexts as
first-class objects.
- Define: first-class objects. See below.
- Define: procedure contexts. section of code
- Closures are a feature present in many (functional, as well as procedural) languages.
- Java8 associates a closure with a lambda expression, paired with an environment. So the two terms are often used interchangeably in the Java community. Other languages do not treat closures and lambda expressions like so.
- If we use the above CS definition of what closures are, Java8 does not have them.
- https://en.wikipedia.org/wiki/Closure_(computer_programming)
3 Java8 Lambdas
3.1 Lambda Calculus
- Alonzo Church, the Lambda Calculus, 1936; Used the Greek λ character
- Grammar: expr ::= λ id : expr | expr expr | id | (expr)
- Lambda calculus is mathematically equivalent to Turing Machines.
3.2 Function Defs without Names
- Suppose we have
f(x, y)
defined as(x + 1)*(y + 2)
. So,f(3, 4)
is24
. - The anonymous version of f is:
lambda x, y: (x + 1)*(y + 2) ;
- The
f(3, 4)
is written as(lambda x, y: (x + 1)*(y + 2))(3, 4)
- Function definition appears in-line with function-invocation.
- Another, but same, in-line def makes it another anonymous function that we happen to know is the same.
3.3 Java8 Functional interfaces
- A functional interface defines exactly one abstract method.
java.lang.Runnable
public abstract void run();
abstract can be omitted
3.4 Java8 Method References
3.5 Java8 Functions without Names
(int x, int y) -> { return (x + 1)*(y + 2); }
(x, y) -> { (x + 1)*(y + 2); }
as above but with inferred typesx -> { System.out.println(x); }
is an example of single parameter with inferred type on the left of the arrow, and a block with no return value on the rhs.- Without Lambda exp (No inputs)
Runnable r1 = new Runnable() { public void run() { System.out.println("Running without Lambda"); } };
- With Lambda exp (No inputs)
Runnable r2 = () -> { System.out.println("Running with Lambda"); };
- Selecting persons based on some criteria
printPersons( roster, (Person p) -> p.getGender() == Person.Sex.MALE && p.getAge() >= 18 );
4 References
- Lectured using these PDF slides by www.cs.cmu.edu/~charlie. "Lambdas and Streams in Java8", 2014. Required Reading.
- Oracle.com, Lambda Expressions, Nested Classes. Recommended Reading.
- https://dzone.com/articles/dark-side-java-8 A critque. Recommended Reading.
- http://www.lambdafaq.org/ Reference