fun with java 8
TRANSCRIPT
![Page 1: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/1.jpg)
Fun with Java 8Victor Perepelitsky
twitter: @victor_perepelemail: [email protected]: https://github.com/victor-prp/java8samples/
![Page 2: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/2.jpg)
This presentation focuses on
What functional programming is ?
Why functional ?
How to use java 8 for functional ?
![Page 3: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/3.jpg)
Functional ProgrammingIn computer science, functional programming is a
programming paradigm, a style of building the structureand elements of computer programs, that treatscomputation as the evaluation of mathematical functionsand avoids changing state and mutable data. It is a declarativeprogramming paradigm, which means programming isdone with expressions.
![Page 4: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/4.jpg)
Functional Programming
Functions
Avoids mutable data
Declarative
Expressions
![Page 5: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/5.jpg)
Why Functional?Less code
Expressive code
Correct code
It is FUN
Performance*
![Page 6: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/6.jpg)
Functional building blocks in java 8Function
Function reference
Lambda
Predefined Java8 functions - java.util.function
Stream APIp2
![Page 7: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/7.jpg)
Basic Function Definition
p1*
![Page 8: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/8.jpg)
Basic Function Usage
p1
![Page 9: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/9.jpg)
Fun Reference - static fun
p1
![Page 10: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/10.jpg)
Fun Reference - static fun
p1
![Page 11: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/11.jpg)
Higher Order Function - Definition
p1*
![Page 12: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/12.jpg)
p1
![Page 13: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/13.jpg)
pureFun doItTwice
pureFun
pureFun
p1
![Page 14: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/14.jpg)
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
![Page 15: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/15.jpg)
Lambda - simple example
p1
![Page 16: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/16.jpg)
Lambda - formal definitionAnonymous 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, or2. used to construct the result of a higher-
order function that needs to return a function.
p1
![Page 17: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/17.jpg)
LambdaAnonymous function
Passed as argument (common usage)
In java 8 it is only syntactic sugar for function
reference
p1
![Page 18: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/18.jpg)
Lambda
p1
![Page 19: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/19.jpg)
MonadIn 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_programming%29p3
![Page 20: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/20.jpg)
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 >
![Page 21: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/21.jpg)
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
![Page 22: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/22.jpg)
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
![Page 23: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/23.jpg)
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
![Page 24: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/24.jpg)
Basic building blocks summaryFunction
Function reference
Lambda
Predefined Java8 functions - java.util.function
Stream APIp2
![Page 25: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/25.jpg)
Why Functional?Less code
Expressive code
Correct code
It is FUN
Performance*p2
![Page 26: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/26.jpg)
Imperative
p5
![Page 27: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/27.jpg)
Declarative
p5
![Page 28: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/28.jpg)
Referential transparencyAn 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_
(computer_science)p2**
![Page 29: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/29.jpg)
Pure Function
1. Given same input, always provides same output
2. Execution does not cause observable side-
effects
1+2 = Referential Transparency
p2
![Page 30: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/30.jpg)
Why pure functions?
Allows automatic optimizations by a compiler
Minimizes moving parts
Easy to test
Increases decoupling
p2
![Page 31: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/31.jpg)
Pure functions - is it enough?
doIt1(..) and doIt2(..) - are pure functions?
but we wrote them in two different ways?!
p2
![Page 32: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/32.jpg)
Imperative
p5
![Page 33: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/33.jpg)
Declarative
p5
![Page 34: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/34.jpg)
Refactor toward functional
Let’s combine pure functions with declarative style
Example: Design a system that returns top UK and US music albums
p3
![Page 35: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/35.jpg)
p3*
![Page 36: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/36.jpg)
p3
![Page 37: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/37.jpg)
p3
![Page 38: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/38.jpg)
We want to search albums
By Name
and
By Year
p3*
![Page 39: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/39.jpg)
version 1
p3
![Page 40: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/40.jpg)
version 2 - pass behaviour
p3
![Page 41: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/41.jpg)
version 3 - compact naming
p3
![Page 42: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/42.jpg)
We want the data in specific form
Example: get years of hits albums
p3*
![Page 43: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/43.jpg)
version 1
p3
![Page 44: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/44.jpg)
version 2
p3
![Page 45: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/45.jpg)
We want the data reduced
Example: get the oldest album
p3
![Page 46: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/46.jpg)
version 1
p3
![Page 47: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/47.jpg)
version 2
p3
![Page 48: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/48.jpg)
So far we sawPure functions vs Non pure...
Declarative vs Imperative...
Functions as building blocks…
Passing behavior vs static behavior…
Function reference vs Lambda...
![Page 49: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/49.jpg)
Additional useful techniquesExecution guarantee
Builder pattern
Composition
Currying and Partial application
p4
![Page 50: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/50.jpg)
We want to
Guarantee correct locking byWrite it onceandLet it use in multiple places
p4*
![Page 51: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/51.jpg)
Execution guarantee
p4
![Page 52: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/52.jpg)
Builder patternStringBuilder is a good example:
p4
![Page 53: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/53.jpg)
How builder pattern helps?
We already saw it - streams
We can create our own builders to introduce fluent interfaces
p4
![Page 54: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/54.jpg)
Can we search by multiple filters?
p4
![Page 55: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/55.jpg)
We actually want this:
p4*
![Page 56: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/56.jpg)
How can we achieve this?
p4
![Page 57: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/57.jpg)
Composition
p4
![Page 58: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/58.jpg)
Partial Application
p4
![Page 59: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/59.jpg)
Search using match(...)
p4
![Page 60: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/60.jpg)
Search with Partial Application V1
p4
![Page 61: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/61.jpg)
Search with Partial Application V2
p4
![Page 62: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/62.jpg)
Why Functional?Less code
Expressive code
Correct code
It is FUN
Performance*
![Page 63: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/63.jpg)
Performance - is it better in java8?Depends:
● Pure functions may be memoized
● Stream API may be heavier
● There are more considerations….
p5
![Page 64: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/64.jpg)
Performance - example
We have doIt1 and doIt2 using both imperative
and declarative styles
p5
![Page 65: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/65.jpg)
Imperative
p5
![Page 66: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/66.jpg)
Declarative
p5
![Page 67: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/67.jpg)
Performance - example
Let’s run it 1000 times
Results:
● doIt1 took: 15ms
● doIt2 took: 63ms
p5
![Page 68: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/68.jpg)
Declarative - improved version
p5
![Page 69: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/69.jpg)
Performance - example
Let’s run it 1000 times
Results:
● doIt1 took: 15ms
● doIt2 took: 63ms
● doIt4 took: 30msp5
![Page 70: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/70.jpg)
Can Java 8 be used for functional programming?
Function reference
Composition
Lambda
Stream API
Currying and Partial Application*
![Page 71: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/71.jpg)
Any problems?
We have to define an interface for a function
Special treatment for functions with primitives
Partial application is not straightforward
Typed exceptions
![Page 72: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/72.jpg)
GuidelinesAim to declarativeAim to immutabilityAim to pure functionsConsider function ref over lambdaBe aware of performance when using streamUse multi paradigm programming
![Page 73: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/73.jpg)
![Page 74: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/74.jpg)
![Page 75: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/75.jpg)
![Page 76: Fun with java 8](https://reader034.vdocument.in/reader034/viewer/2022042602/55a927601a28abb8768b4589/html5/thumbnails/76.jpg)
all sample are available at: https://github.com/victor-prp/java8samples