Re: Java 8 Lambda binary snapshot
On 11/13/2011 11:39 AM, Arne Vajh?j wrote:
Java 8 lambda:
import java.util.ArrayList;
import java.util.Collections;
public class Lambda {
public static void main(String[] args) {
ArrayList<String> lst = new ArrayList<String>();
lst.add("A");
lst.add("BB");
lst.add("CCC");
Collections.sort(lst, (s1,s2)-> -s1.compareTo(s2));
for(String s : lst) {
System.out.println(s);
}
}
}
Arne
ps. I just heard about this Lambda thing ;)
This looks like the ability to have, what is called in Mathematica,
or may be other functional languages, a pure function, that one
can define and use on the fly ?
For example, in Mathematica, one can write
"Sort[list,p]
sorts using the ordering function p. "
and is coded like this, for example:
In[11]:= Sort[{4,1,3,2,2},#1>#2&]
Out[11]= {4,3,2,2,1}
where the '&' indicates the end of the function, and
#1 means first argument, and #2 mean the second argument.
One can also write the pure function more explicit, like this:
In[8]:= Sort[{4,1,3,2,2},Function[{x,y},x<y]]
Out[8]= {1,2,2,3,4}
Is this, somewhat, close to what Lambda in Java allows?
It is very useful feature to be able to do this sort of thing.
(I need to read more about Java 8, sounds interesting)
--Nasser