Functional Programming with Jan Köhnlein & Sven Efftinge
Functional programming is like describing your problem to a mathematician. Imperative programming is like giving instructions to an idiot.
Imperative vs Functional State Changes vs No State Change How? vs What?
2 Important Principles
#1 : No Side-Effects
Haskell is a purely-functional programming language
Most other functional programming languages are more relaxed, i.e. non-pure.
Most other functional programming languages are more relaxed, i.e. non-pure. But still embrace immutability!
Why Immutability? Thread-Safety Easy to test, construct and use Good Map keys and Set members Code is easier to read and understand
Java and Immutability
The beauty of java.lang.String Thread-Safety Easy to test, construct and use Good Map keys and Set members Code is easier to read and understand
"Classes should be immutable unless there's a very good reason to make them mutable...” (Joshua Bloch)
"When you create immutable classes, entire categories of problems simply disappear.” (javapractices.com)
How to build an immutable data type in Java? make everything final create constructor for fields implement hashcode implement equals
writing Immutable code in Java is tedious & error-prone
Immutability in final by default val vs. var immutable collection literals everything is an expression @Pure @Data
First Exercise Immutable Data Types
2 Important Principles
#2 : First-Class Functions
#2 : First-Class Functions passing functions as arguments higher-order functions lambdas
An Example
@Data class Movie { String title int year double rating long numberOfVotes ImmutableSet<String> categories }
What’s the best drama of the 70ies?
The imperative way
The functional way
Java 8 Lambdas
Lambdas
Second Exercise In The Movies
Use the force, Duke! www.xtend-lang.org

Functional programming with Xtend