33
Functional Programming with Jan Köhnlein & Sven Efftinge

Functional programming with Xtend

Embed Size (px)

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.

Citation preview

Page 1: Functional programming with Xtend

Functional Programming with

Jan Köhnlein & Sven Efftinge

Page 2: Functional programming with Xtend

Functional programming is like describing your problem to a mathematician.

Imperative programming is like

giving instructions to an idiot.

Page 3: Functional programming with Xtend

Imperative vs Functional

How? vs What?State Changes vs No State Change

Page 4: Functional programming with Xtend

2 Important Principles

Page 5: Functional programming with Xtend

#1 : No Side-Effects

Page 6: Functional programming with Xtend

Haskell is a purely-functional programming language

Page 7: Functional programming with Xtend

Most other functional programming languages are more relaxed, i.e. non-pure.

Page 8: Functional programming with Xtend

Most other functional programming languages are more relaxed, i.e. non-pure.

But still embrace immutability!

Page 9: Functional programming with Xtend

Why Immutability?

Thread-Safety

Easy to test, construct and use

Good Map keys and Set members

Code is easier to read and understand

Page 10: Functional programming with Xtend

Java and Immutability

Page 11: Functional programming with Xtend

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

Page 12: Functional programming with Xtend

"Classes should be immutable unless there's a very good reason to make them mutable...” (Joshua Bloch)

Page 13: Functional programming with Xtend

"When you create immutable classes, entire categories of problems simply disappear.” (javapractices.com)

Page 14: Functional programming with Xtend

How to build an immutable data type in Java?

make everything final

create constructor for fields

implement hashcode

implement equals

Page 15: Functional programming with Xtend

writing Immutable code in Java is tedious & error-prone

Page 16: Functional programming with Xtend

Immutability infinal by default

val vs. var

immutable collection literals

everything is an expression

@Pure

@Data

Page 17: Functional programming with Xtend

First ExerciseImmutable Data Types

Page 18: Functional programming with Xtend

2 Important Principles

Page 19: Functional programming with Xtend

#2 : First-Class Functions

Page 20: Functional programming with Xtend

#2 : First-Class Functions

passing functions as arguments higher-order functions

lambdas

Page 21: Functional programming with Xtend

An Example

Page 22: Functional programming with Xtend
Page 23: Functional programming with Xtend

@Data class Movie {String titleint yeardouble ratinglong numberOfVotesImmutableSet<String> categories

}

Page 24: Functional programming with Xtend

What’s the best drama of the 70ies?

Page 25: Functional programming with Xtend

The imperative way

Page 26: Functional programming with Xtend
Page 27: Functional programming with Xtend
Page 28: Functional programming with Xtend

The functional way

Page 29: Functional programming with Xtend

Java 8 Lambdas

Page 30: Functional programming with Xtend

Lambdas

Page 31: Functional programming with Xtend
Page 32: Functional programming with Xtend

Second Exercise In The Movies

Page 33: Functional programming with Xtend

Use the force, Duke!

www.xtend-lang.org