The focus of this series is on practical usage of lambdas, not in principal on the fundamental concepts of lambdas. You don't need to have any theoretical knowledge of lambda calculus or a strong background in functional programming. And don't break your mind on what closures are, just think of them as anonymous functions or code blocks that can be passed around. But despite you don't need to know about all this, I don't want to discourage you from researching these topics.
Note: If you want to try out some examples there's a little trick to get the lambda prototype working, see here.
Lambda expressions
To get started, let's have a look at a very simple example of an anonymous function or "lambda expression". The following is a lambda expression that takes an integer
x
and returns 2*x
:#(int x)(2*x)The hash symbol introduces the lambda expression, or "function literal". The first pair of parentheses is a comma-separated argument list and the second pair of parentheses encloses the expression, which is evaluated on invocation. Here is another example of a lambda expression, that takes two arguments
x
and y
and returns their sum:#(int x, int y)(x+y)What we would naturally do with a lambda expression is to evaluate it. We could directly invoke one of the function literals above directly with
#(int x)(2*x).(7)
, but this will probably be a bit uncommon. Function types
Instead invocation will mostly happen on variables of a function type. So we first bind a function to variable and do the invocation on that. The syntax for function types is very similar to the syntax of function literals:
#int(int) doubler = #(int x)(2*x);This declares a variable
doubler
of type #int(int)
, i.e. of type function that takes an int (in parentheses) and returns an int (after the #). Now, to invoke doubler
we can writeint n = doubler.(3); assert n == 6;Notice the dot before the parentheses. For another example, let's look at the sum lambda again:
#int(int, int) sum = #(int x, int y)(x+y); int x = sum.(3, 7); assert x == 10;More complex expressions
So far the body of the lambda expressions was just a single expression. In this case it can be included in parentheses and the
return
keyword can be omitted. In the more complex case the body can be a block with curly braces and must explicitly return a value (if it's not void
):#int(int, int) max = #(int x, int y) { if (x >= y) return x; else return y; }; int z = max.(3,4); assert z == 4;Higher-order functions
Of course, functions can also be passed as arguments to methods and other functions. This will be the most common usage. A function, that takes an integer
n
and function f
, that executes f
n
times could look like this:public static void times(int n, #void(int) f) { for (int i=0;i<n;i++) { f.(i); } }The following will print the squares of 0 to 4 on the console.
times(5, #(int index)(System.out.println(index*index)));Functions and methods can also have a function as their return value. The following method takes an integer
x
and returns a function that takes another integer y
and returns x*y
.public static #int(int) multiplier(final int x) { return #(int y)(x*y); }The invocation looks like this:
#int(int) mult5 = multiplier(5); assert 20 == mult5.(4);This case also shows, that it is possible to capture variables from the enclosing scope (this is what makes it a closure by the way).
x
is a free variable and its definition is copied over into the body of the lambda expression at runtime. For this to work x
must be declared effectively-final or shared (see straw-man proposal for details).That's it
That's basically it for the fundamentals of lambda expression in Java. But you will probably have noticed by now, that it will have a huge impact on Java, both the language and the libraries.
Side note: Some people don't like the syntax of lambdas as above. I don't want to start the discussion here again, just two points. First, the syntax can actually be awkward in some cases, but most of the time it's just passing around functions as literals or variables, which doesn't look awkward. And second, because Java is a statically typed language and has features like checked exceptions and others, closures won't look like in dynamically typed languages or languages with strong type inferencing or languages without checked exceptions.
Function conversion
This last section is about function conversion, which isn't something that is essential to lambda expressions, but will also have a huge impact. Many Java libraries use so called SAM types (single abstract method) - interfaces with only a single method and abstract classes with only one abstract method. Function conversion means that a function of appropriate type can be converted into an anonymous instance of a SAM type as needed. For example, the
Collections.sort
method takes a List
and a Comparator
, which has a single abstract method int compare(T x, T y)
. Up to now this would look like this:// This would be just '= [4,2,1,3]' // with collection literals List<Integer> list = Arrays.asList(new Integer[]{4,2,1,3}); Collections.sort(list, new Comparator<Integer>() { public int compare(Integer x, Integer y) { return -x.compareTo(y); } });But with function conversion we can substitute a function of appropriate type:
Collections.sort(list, #(Integer x, Integer y)(-x.compareTo(y)));This will probably be used very often, because SAM types are so common in the Java world. As a side note, an open questions to me is, if API designers should choose to take functions as arguments to their methods/functions or SAM types, which may be more flexible and more expressive.
Final example
For a final example we implement the Fibonacci function and call it several times in parallel threads.
class ParallelFib { final static #void(int,int) fib = #(int c, int n) { int result = fib(n); System.out.println(c + ") " + "fib(" + n + ") = " + result); }; public static int fib(int n) { if (n == 0 || n == 1) return 1; else return fib(n-1) + fib(n-2); } public static void main(String[] args) { for (int i=0;i<10;i++) { final int i2 = i; new Thread(#()(fib.(i2, 32))).start(); } } }
fib
first occurs as a class variable of function type. This lambda expression takes a counter c
and input n
, calls the method fib (I don't know, if recursive lambda calls are possible at the moment) and then prints the counter and the result. The main method creates 10 threads each taking the fib lambda expression, which is converted implicitly into a Runnable
. The output is something like this:
1) fib(32) = 3524578 3) fib(32) = 3524578 2) fib(32) = 3524578 0) fib(32) = 3524578 7) fib(32) = 3524578 4) fib(32) = 3524578 9) fib(32) = 3524578 6) fib(32) = 3524578 8) fib(32) = 3524578 5) fib(32) = 3524578Feel free to post comments on what you think about lambdas right here, or give feedback directly to the members of project lambda, which I think is always welcome.