Download - Functional programming with Xtend
![Page 1: Functional programming with Xtend](https://reader034.vdocuments.net/reader034/viewer/2022042510/557d5f19d8b42ae1438b4eca/html5/thumbnails/1.jpg)
Functional Programming with
Jan Köhnlein & Sven Efftinge
![Page 2: Functional programming with Xtend](https://reader034.vdocuments.net/reader034/viewer/2022042510/557d5f19d8b42ae1438b4eca/html5/thumbnails/2.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042510/557d5f19d8b42ae1438b4eca/html5/thumbnails/3.jpg)
Imperative vs Functional
How? vs What?State Changes vs No State Change
![Page 4: Functional programming with Xtend](https://reader034.vdocuments.net/reader034/viewer/2022042510/557d5f19d8b42ae1438b4eca/html5/thumbnails/4.jpg)
2 Important Principles
![Page 5: Functional programming with Xtend](https://reader034.vdocuments.net/reader034/viewer/2022042510/557d5f19d8b42ae1438b4eca/html5/thumbnails/5.jpg)
#1 : No Side-Effects
![Page 6: Functional programming with Xtend](https://reader034.vdocuments.net/reader034/viewer/2022042510/557d5f19d8b42ae1438b4eca/html5/thumbnails/6.jpg)
Haskell is a purely-functional programming language
![Page 7: Functional programming with Xtend](https://reader034.vdocuments.net/reader034/viewer/2022042510/557d5f19d8b42ae1438b4eca/html5/thumbnails/7.jpg)
Most other functional programming languages are more relaxed, i.e. non-pure.
![Page 8: Functional programming with Xtend](https://reader034.vdocuments.net/reader034/viewer/2022042510/557d5f19d8b42ae1438b4eca/html5/thumbnails/8.jpg)
Most other functional programming languages are more relaxed, i.e. non-pure.
But still embrace immutability!
![Page 9: Functional programming with Xtend](https://reader034.vdocuments.net/reader034/viewer/2022042510/557d5f19d8b42ae1438b4eca/html5/thumbnails/9.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042510/557d5f19d8b42ae1438b4eca/html5/thumbnails/10.jpg)
Java and Immutability
![Page 11: Functional programming with Xtend](https://reader034.vdocuments.net/reader034/viewer/2022042510/557d5f19d8b42ae1438b4eca/html5/thumbnails/11.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042510/557d5f19d8b42ae1438b4eca/html5/thumbnails/12.jpg)
"Classes should be immutable unless there's a very good reason to make them mutable...” (Joshua Bloch)
![Page 13: Functional programming with Xtend](https://reader034.vdocuments.net/reader034/viewer/2022042510/557d5f19d8b42ae1438b4eca/html5/thumbnails/13.jpg)
"When you create immutable classes, entire categories of problems simply disappear.” (javapractices.com)
![Page 14: Functional programming with Xtend](https://reader034.vdocuments.net/reader034/viewer/2022042510/557d5f19d8b42ae1438b4eca/html5/thumbnails/14.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042510/557d5f19d8b42ae1438b4eca/html5/thumbnails/15.jpg)
writing Immutable code in Java is tedious & error-prone
![Page 16: Functional programming with Xtend](https://reader034.vdocuments.net/reader034/viewer/2022042510/557d5f19d8b42ae1438b4eca/html5/thumbnails/16.jpg)
Immutability infinal by default
val vs. var
immutable collection literals
everything is an expression
@Pure
@Data
![Page 17: Functional programming with Xtend](https://reader034.vdocuments.net/reader034/viewer/2022042510/557d5f19d8b42ae1438b4eca/html5/thumbnails/17.jpg)
First ExerciseImmutable Data Types
![Page 18: Functional programming with Xtend](https://reader034.vdocuments.net/reader034/viewer/2022042510/557d5f19d8b42ae1438b4eca/html5/thumbnails/18.jpg)
2 Important Principles
![Page 19: Functional programming with Xtend](https://reader034.vdocuments.net/reader034/viewer/2022042510/557d5f19d8b42ae1438b4eca/html5/thumbnails/19.jpg)
#2 : First-Class Functions
![Page 20: Functional programming with Xtend](https://reader034.vdocuments.net/reader034/viewer/2022042510/557d5f19d8b42ae1438b4eca/html5/thumbnails/20.jpg)
#2 : First-Class Functions
passing functions as arguments higher-order functions
lambdas
![Page 21: Functional programming with Xtend](https://reader034.vdocuments.net/reader034/viewer/2022042510/557d5f19d8b42ae1438b4eca/html5/thumbnails/21.jpg)
An Example
![Page 22: Functional programming with Xtend](https://reader034.vdocuments.net/reader034/viewer/2022042510/557d5f19d8b42ae1438b4eca/html5/thumbnails/22.jpg)
![Page 23: Functional programming with Xtend](https://reader034.vdocuments.net/reader034/viewer/2022042510/557d5f19d8b42ae1438b4eca/html5/thumbnails/23.jpg)
@Data class Movie {String titleint yeardouble ratinglong numberOfVotesImmutableSet<String> categories
}
![Page 24: Functional programming with Xtend](https://reader034.vdocuments.net/reader034/viewer/2022042510/557d5f19d8b42ae1438b4eca/html5/thumbnails/24.jpg)
What’s the best drama of the 70ies?
![Page 25: Functional programming with Xtend](https://reader034.vdocuments.net/reader034/viewer/2022042510/557d5f19d8b42ae1438b4eca/html5/thumbnails/25.jpg)
The imperative way
![Page 26: Functional programming with Xtend](https://reader034.vdocuments.net/reader034/viewer/2022042510/557d5f19d8b42ae1438b4eca/html5/thumbnails/26.jpg)
![Page 27: Functional programming with Xtend](https://reader034.vdocuments.net/reader034/viewer/2022042510/557d5f19d8b42ae1438b4eca/html5/thumbnails/27.jpg)
![Page 28: Functional programming with Xtend](https://reader034.vdocuments.net/reader034/viewer/2022042510/557d5f19d8b42ae1438b4eca/html5/thumbnails/28.jpg)
The functional way
![Page 29: Functional programming with Xtend](https://reader034.vdocuments.net/reader034/viewer/2022042510/557d5f19d8b42ae1438b4eca/html5/thumbnails/29.jpg)
Java 8 Lambdas
![Page 30: Functional programming with Xtend](https://reader034.vdocuments.net/reader034/viewer/2022042510/557d5f19d8b42ae1438b4eca/html5/thumbnails/30.jpg)
Lambdas
![Page 31: Functional programming with Xtend](https://reader034.vdocuments.net/reader034/viewer/2022042510/557d5f19d8b42ae1438b4eca/html5/thumbnails/31.jpg)
![Page 32: Functional programming with Xtend](https://reader034.vdocuments.net/reader034/viewer/2022042510/557d5f19d8b42ae1438b4eca/html5/thumbnails/32.jpg)
Second Exercise In The Movies
![Page 33: Functional programming with Xtend](https://reader034.vdocuments.net/reader034/viewer/2022042510/557d5f19d8b42ae1438b4eca/html5/thumbnails/33.jpg)
Use the force, Duke!
www.xtend-lang.org