Javslang & FP in java

Emmanuel Touzery

Javaslang & FP in java

Emmanuel Touzery

January 2017

Who am I

Javaslang

Why javaslang?

It has an interface named λ.
I rest my case ∎

FP key features

Big picture

Immutable?

No 'void' modifiers. Example of mutable classes: List, Map, old Date, Calendar

List newList = existing.prepend(extraCar);

Note that we now have two lists: existing is still available and unchanged, and newList is added, with the change included.

We say of a 'void' returning function that it's only side-effecting.

Immutability, what for

No more 'getters' which allow to modify your state


Mutability increases complexity by adding the dimension of time to all objects.

Mike Piccolo, Jose Valim

Immutability is winning

Some immutable classes in the JDK (many of them newer!)
  • String
  • Datetime
  • Path
  • Optional
  • Stream
  • CompletableFuture
  • Java9 collection factories
  • Project Valhalla -- java value objects

Immutability and performance 1/3

Key points: it's monitored, and same order of magnitude

for `1026` elements
Java mutable @ ArrayList → 19,072 bytes
PCollections persistent @ TreePVector → 56,096 bytes
Eclipse Collections persistent @ ImmutableArrayList → 19,064 bytes
Clojure persistent @ PersistentVector → 20,552 bytes
Scala persistent @ Vector → 20,168 bytes
Javaslang persistent @ Vector → 19,816 bytes
Javaslang persistent @ Vector → 4,888 bytes
Javaslang persistent @ Vector → 1,816 bytes

Immutability and performance 2/3

Key points: it's monitored, and same order of magnitude

Operation Ratio 10 100 1026
Create slang_persistent/java_mutable 1.68× 1.31× 1.28×
Head slang_persistent/java_mutable 0.66× 0.49× 0.39×
Tail slang_persistent/java_mutable 0.88× 0.48× 0.49×
Get slang_persistent/java_mutable 0.80× 0.41× 0.27×
Update slang_persistent/java_mutable 0.18× 0.06× 0.04×
Map slang_persistent/java_mutable 2.16× 2.37× 2.30×
Map slang_persistent/java_mutable_loop 0.48× 0.51× 0.72×
Filter slang_persistent/java_mutable 1.88× 1.94× 1.53×
Prepend slang_persistent/java_mutable 0.11× 0.12× 0.23×
Append slang_persistent/java_mutable 0.04× 0.03× 0.02×
GroupBy slang_persistent/java_mutable 0.35× 0.53× 0.89×
Slice slang_persistent/java_mutable 0.63× 0.43× 0.45×
Iterate slang_persistent/java_mutable 0.75× 0.30× 0.40×

Immutability and performance 3/3

Javaslang authors noticed a performance regression in scala...

What about guava or others?

Javaslang List doesn't inherit from java.util.List, unlike guava or Collections.unmodifiableList for instance

"Functors" 1/2

Picture from adit.io

"Functors" 2/2

  • List, Array, Vector
  • Map
  • CharSeq
  • Option
  • Tuple
  • Either
  • Try
  • Future
  • Validation
  • Lazy

Java8 has Optional, Stream, CompletableFuture

REPL

Option<T>

Railway-oriented programming

Scott Wlaschin

Railway-oriented programming

Scott Wlaschin

Railway-oriented programming

Scott Wlaschin

Try<T>

Either<L,R>

Validation<E,T>

Functional and Reactive domain modeling, Debasish Ghosh, p125

Tuple

Expressions vs statements

"if" is a statement, ternaries are an expression. Expressions compose, statements don't.
1. good old

Expressions vs statements

2. java streams

Patterns are extracted and immediately visible, while a for loop could perform anything.

Expressions vs statements

3. javaslang

Expressions vs statements

Functions

In mathematics, a is a relation between a set of inputs and a set of permissible outputs with the property that each input is related to exactly one output.

Wikipedia
list.map(f).map(g) is equivalent to list.map(f.andThen(g)) only if both functions are pure (free of side-effects).

Side effects: anything that depends on an external system. Filesystem, global mutable state..

Function composition

You can wrap a function with another one, or compose them. Composed function are better testable (in tests and in the REPL)

getTranslationIdentifierToKey
→ calls getTranslations
getTranslations
→ calls decodeTranslationFile
→ extracts the translations

Function composition

You can wrap a function with another one, or compose them. Composed function are better testable (in tests and in the REPL)

Don't underestimate Option!

Don't underestimate Option!

Option and DTOs: before

Option and DTOs: after

Infinite streams

Pattern matching again

Pattern matching deconstruction 1/2

Pattern matching deconstruction 2/2

And more...

Universality

These concepts are general, and you will find them in many environments. Scala, Haskell but also Javascript libraries, F#, elm, lisps...

Missing from javaslang

  1. Simple immutable types (see java immutables, upcoming Project Valhalla)
  2. Algebraic Data Types (derive4j?)
  3. Advanced types & type-level programming (not workable in java)
  4. No tail recursion in java

Annex: immutables for value objects

Immutables is a library offering value types for java.

References and links