In this Meetup Victor Perepelitsky - R&D Technical Leader at LivePerson leading the 'Real Time Event Processing Platform' team , will talk about Java 8', 'Stream API', 'Lambda', and 'Method reference'.
Victor will clarify what functional programming is and how can you use java 8 in order to create better software.
Victor will also cover some pain points that Java 8 did not solve regarding functionality and see how you can work around it.
Human Factors of XR: Using Human Factors to Design XR Systems
Functional programming with Java 8
1. Functional Programming with Java 8
Victor Perepelitsky
slides: http://www.slideshare.net/victorperepelitsky/fun-with-java-8
twitter: @victor_perepel
email: victor.prp@gmail.com
git-hub: https://github.com/victor-prp/java8samples/
2. This presentation focuses on
What functional programming is ?
Why functional ?
How to use java 8 for functional ?
3. Functional Programming
In computer science, functional programming is a
programming paradigm, a style of building the structure
and elements of computer programs, that treats
computation as the evaluation of mathematical functions
and avoids changing state and mutable data. It is a declarative
programming paradigm, which means programming is
done with expressions.
14. java.util.function
Function<T, R> accepts T, returns R
BiFunction<T, U, R> accepts T and U, returns R
Consumer<T> accepts T, returns void
Supplier<T> accepts nothing, returns T
Predicate<T> accepts T, returns boolean
p1
16. Lambda - formal definition
Anonymous function (also function literal or
lambda abstraction) is a function definition that is
not bound to an identifier. Lambdas are often:
1. passed as arguments to higher-order
functions, or
2. used to construct the result of a higher-
order function that needs to return a function.
p1
19. Monad
In functional programming, a monad is a structure that
represents computations defined as sequences of
steps.
A type with a monad structure defines what it means to
chain operations, or nest functions of that type together
http://en.wikipedia.org/wiki/Monad_%28functional_prog
p3
20. In Java 8 Stream is a Monad
Chaining stream operations forming a stream pipeline
p3
menu filter sorted map collect
how to filter how to sort how to map how to collect
List<String >
21. Java 8 Stream
● A stream represents a sequence of elements and supports
different kind of operations to perform computations upon
those elements
● Stream operations are either intermediate or terminal.
● Intermediate operations return a stream so we can chain
multiple intermediate operations.
● Terminal operations are either void or return a non-stream
result.
p3
22. Let’s see it again
p3
menu
stream
filter sorted map collect
how to filter how to sort how to map how to collect
List<String >
intermediate final
23. Stream vs Collection
● Like a collection, a stream provides an interface to a
sequenced set of values of a specific element type
● Because collections are data structures, they’re
mostly about storing and accessing elements with
specific time/space complexities
● Streams are about expressing computations such
as filter, sorted, and map that
● Collections are about data; streams are about
computations
p2
24. Basic building blocks summary
Function
Function reference
Lambda
Predefined Java8 functions - java.util.function
Stream API
p2
28. Referential transparency
An expression is said to be referentially transparent if it can
be replaced with its value without changing the behavior
of a program (in other words, yielding a program that has
the same effects and output on the same input). The
opposite term is referential opaqueness.
http://en.wikipedia.org/wiki/Referential_transparency_(com
puter_science)p2**
29. Pure Function
1. Given same input, always provides same output
2. Execution does not cause observable side-
effects
1+2 = Referential Transparency
p2
30. Why pure functions?
Allows automatic optimizations by a compiler
Minimizes moving parts
Easy to test
Increases decoupling
p2
31. Pure functions - is it enough?
doIt1(..) and doIt2(..) - are pure functions?
but we wrote them in two different ways?!
p2
48. So far we saw
Pure functions vs Non pure...
Declarative vs Imperative...
Functions as building blocks…
Passing behavior vs static behavior…
Function reference vs Lambda...
69. Performance - example
Let’s run it 1000 times
Results:
● doIt1 took: 15ms
● doIt2 took: 63ms
● doIt4 took: 30ms
p5
70. Can Java 8 be used for functional
programming?
Function reference
Composition
Lambda
Stream API
Currying and Partial Application*
71. Any problems?
We have to define an interface for a function
Special treatment for functions with primitives
Partial application is not straightforward
Typed exceptions
72. Guidelines
Aim to declarative
Aim to immutability
Aim to pure functions
Consider function ref over lambda
Be aware of performance when using stream
Use multi paradigm programming
73.
74.
75.
76. all sample are available at:
https://github.com/victor-prp/java8samples