О функциональном программировании (ФП), преимуществах его использования и о том, какие возможности для него добавили в новом релизе Java. Слушатели также узнают, почему уже сейчас стоит начать изучать ФП.
Митап состоялся ->> https://plus.google.com/u/0/b/100893943920756626864/events/c0b2b1ih9ft0opcdnmdgp3453rk
2. Google Developers Group
Almaty
• Некоммерческое сообщество, в основе
которого лежит обмен опытом и знаниями
технологии Google для разработчиков -
Android, Google Maps, App Engine, Chrome,
Web Toolkit, Google Plus и другие.
• Среди нас есть программисты, архитекторы,
дизайнеры, IT-менеджеры, студенты и
руководители. И мы всегда рады новым
знакомствам :)
15. First Lambdas
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.out.println("button clicked")
}
});
button.addActionListener(event ->
System.out.println("button clicked"));
16. Синтаксис Lambda
Выражений
• Lambda - anonymous function
(arg1, arg2...) -> { body }
(int x, int y) -> x + y
(x, y) -> x + y
() -> System.out.println(“Hello World”)
17. Functional Interface
T => Boolean
@FunctionalInterface
public interface Predicate<T>{
boolean test(T t)
}
T => void
@FunctionalInterface
public interface Consumer<T>{
void accept(T t)
}
T => R
@FunctionalInterface
public interface Function<T, R>{
R apply(T t)
}
Def: A functional interface is an
interface with a single abstract
method that is used as the type
of a lambda expression.
18. Functional Interface
Represents a function that accepts one argument and produces a result.
T => R
@FunctionalInterface
public interface Function<T, R>{
R apply(T t)
}
Function<Apple, String> = (Apple a ) -> a.toString()
19. Functional Interface
Represents a predicate (boolean-valued function) of one argument. -
Filter
T => Boolean
@FunctionalInterface
public interface Predicate<T>{
boolean test(T t)
}
Predicate<Integer> atLeast5 = ( x ) -> x > 5;
20. Functional Interface
Represents an operation that accepts a single input argument and returns no
result.
T => void
@FunctionalInterface
public interface Consumer<T>{
void accept(T t)
}
Consumer<Apple> = (Apple a) -> System.out.println(a.toString())
22. Streams
• No Storage
• Lazy Evaluations
• Infinite/Finite Stream
• Single Usage
• Ordered/Unordered
• Parallel/Sequential
23. Streams
Declarative Style
what to do NOT how
List<String> myList = Arrays.asList("a1", "a2", "b1", "c2", "c1");
myList
.stream()
.filter(s -> s.startsWith("c"))
.map(String::toUpperCase)
.sorted()
.forEach(System.out::println);
29. Количество зеленых яблок в колекции?
int count = 0;
for (Apple apple : allApples) {
if (apple.equals("green")) {
count++;
} }
return count;
30. Количество зеленых яблок в колекции?
int count = 0;
Iterator<Apple> iterator = allApples.iterator();
while(iterator.hasNext()) {
Apple apple = iterator.next();
if (apple.equals("green")) {
count++; }
}
31. Parallel Streams
public int parallelArraySum() {
return albums.parallelStream()
.mapToInt((Track tr) -> tr.getLength)
.sum();
}
● Fork and Join Framework
● NOT keep order of stream