Post on 10-May-2015
description
λ
functions as values
var square = function(x) { return x * x };
var apply = function(data, func) { return func(data);}
console.log(apply(10, square));
Ang Java ay parangkami ng ex ko...
..wala paring closure.
- anon
Anonymous Inner ClassesCollections.sort(personList, new Comparator<Person>(){ public int compare(Person p1, Person p2){ return p1.firstName.compareTo(p2.firstName); }});
Anonymous Inner Classes
Lambda Expressions
Collections.sort(personList, new Comparator<Person>(){ public int compare(Person p1, Person p2){ return p1.firstName.compareTo(p2.firstName); }});
Collections.sort(personList, (Person p1, Person p2) -> p1.firstName.compareTo(p2.firstName));
Lambda Expressions
Lambda Exp. (shorter)
Collections.sort(personList, (Person p1, Person p2) -> p1.firstName.compareTo(p2.firstName));
Collections.sort(personList, (p1, p2) -> p1.firstName.compareTo(p2.firstName));
JEP 107Bulk Data Operations
for Collections(e.g filter/map/reduce)
filterList<Employee> old = new ArrayList<Employee>();
for (Employee e : employeeList) { if (e.age() > 60) { old.add(e); }}
filterList<Employee> old = new ArrayList<Employee>();
for (Employee e : employeeList) { if (e.age() > 60) { old.add(e); }}
List<Employee> old = employeeList.stream() .filter(e -> e.age() > 60) .collect( Collectors.toCollection( () -> new ArrayList<>() ) );
filterList<Employee> old = new ArrayList<Employee>();
for (Employee e : employeeList) { if (e.age() > 60) { old.add(e); }}
List<Employee> old = employeeList.stream() .filter(e -> e.age() > 60) .collect(Collectors.toCollection( () -> new ArrayList<>()));
filterList<Employee> old = new ArrayList<Employee>();
for (Employee e : employeeList) { if (e.age() > 60) { old.add(e); }}
List<Employee> old = employeeList.stream() .filter(e -> e.age() > 60) .collect( Collectors.toCollection( ArrayList::new ) );
filterList<Employee> old = new ArrayList<Employee>();
for (Employee e : employeeList) { if (e.age() > 60) { old.add(e); }}
List<Employee> old = employeeList.stream() .filter(e -> e.age() > 60) .collect(Collectors.toCollection(ArrayList::new));
filterList<Employee> old = new ArrayList<Employee>();
for (Employee e : employeeList) { if (e.age() > 60) { old.add(e); }}
List<Employee> old = employeeList.stream() .filter(e -> e.age() > 60) .collect(Collectors.toList());
mapList<String> names = new ArrayList<String>();
for (Employee e : employeeList) { names.add(e.firstName() + " " + e.lastName());}
mapList<String> names = new ArrayList<String>();
for (Employee e : employeeList) { names.add(e.firstName() + " " + e.lastName());}
List<String> names = employeeList.stream() .map(e -> e.firstName() + " " + e.lastName()) .collect(Collectors.toList());
reduceBigDecimal totalSalary = BigDecimal.ZERO;
for (Employee e : employeeList) { totalTax = totalTax.add(e.salary());}
reduceBigDecimal totalSalary = BigDecimal.ZERO;
for (Employee e : employeeList) { totalTax = totalTax.add(e.salary());}
BigDecimal totalSalary = employeeList.stream() .reduce(BigDecimal.ZERO, (sum, e) -> sum.add(e.salary()), (bd1, bd2) -> bd1.add(bd2));
reduceBigDecimal totalSalary = BigDecimal.ZERO;
for (Employee e : employeeList) { totalTax = totalTax.add(e.salary());}
BigDecimal totalSalary = employeeList.stream() .reduce(BigDecimal.ZERO, (sum, e) -> sum.add(e.salary()), BigDecimal::add);
chainingBigDecimal totalOldTax = employeeList.stream() .filter(e -> e.age() > 60) .map(e -> e.salary().multiply(e.taxRate())) .reduce(BigDecimal.ZERO, (sum, tax) -> sum.add(tax);
just like SQLBigDecimal totalOldTax = employeeList.stream() .filter(e -> e.age() > 60) .map(e -> e.salary().multiply(e.taxRate())) .reduce(BigDecimal.ZERO, (sum, tax) -> sum.add(tax);
SELECT SUM(salary * tax_rate)FROM employeesWHERE age > 60;
just like SQL
filter() => WHERE, GROUP BY
map() => SELECT, inline query
reduce() => aggregate functions
sort() => ORDER BY
λJava 8
Where to get LambdaProject Pagehttp://openjdk.java.net/projects/lambda/
JDK8http://jdk8.java.net/download.html
JDK8 w/ Lambdahttp://jdk8.java.net/lambda/
NetBeans Nightlyhttp://bertram2.netbeans.org:8080/job/jdk8lambda/lastSuccessfulBuild/artifact/nbbuild/
Thank you for listening!
bryanbibat.net | @bry_bibatspeakerdeck.com/bryanbibat