The idea is this. Create methods that receive a Function[0-9] or Action[0-9], and then create that lambda with either my current lambda implementation F[0-9] or A[0-9] now, or when Java 7 finally implements lambdas in the syntax, simply convert them to that official syntax #{a->a}
Let's look at an example:
Get all the people over 35 years old
To do this, we want to use the method
Query.where(List
S1 implements Function1, so we can write the following in Java 6
List<Person> people =
Query.where(getStudents(), new S1<Person>(a){{ret(a.getAge() > 35);}});
However, once Java 7 releases the official Lambdas, we can convert this to the much simpler call
List<Person> people =
Query.where(getStudents(),#{a -> a.getAge() > 35});
Because Function1 is a SAM interface (single abstract method)
Why Use Lambdas?
Hopefully this will make it easier to start using lambdas in your production code. Lambdas(Closures) are all about using the open/closed principal . They are a bit tricky to use at 1st. But soon they will become essential to you. I think of them as the next step in subroutines.
In simpler languages, you could create a subroutine, but not pass variables in. If you wanted to have a function
People.whereMimimumAgeIs21()
you could, but you could not have
People.where(int MimimumAge )
and call
People.where(21)
Parameters allowed you to reduce duplication by saying "this variable can be different for each call".
Lambdas do the same thing, but with duplicate code. Now you can say
People.where(Function1 whereClause )
and call
People.where(#{p -> p.age > 21})
Note: For the record, I dislike the currently proposed (1/8/2011) Java 1.7 lambda syntax, and vastly prefer the C# syntax
People.where(p => p.age > 21)
Links: