functional programming with xtend
DESCRIPTION
At least since the release of Java 8, functional programming has become mainstream in the Java community. Things like collection processing, lazy evaluation and concurrent programming are much easier to describe in a functional style than in the traditional procedural or object-oriented way. Xtend is a Java dialect hosted at Eclipse. Designed to remove the syntactic noise, it offers a superior syntax and additional abstractions to provide the full power of functional programming to Java developers. As it compiles to Java 5 code, it will enable functional programming even for Android and GWT developers. In this tutorial, you will learn the basic principles of functional programming and the Xtend idioms to write code the functional way: concise, easy to understand, and yet powerful. No prior knowledge of functional programming or Xtend required.TRANSCRIPT
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
How? vs What?State Changes vs No State Change
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 infinal by default
val vs. var
immutable collection literals
everything is an expression
@Pure
@Data
First ExerciseImmutable 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 titleint yeardouble ratinglong numberOfVotesImmutableSet<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