Slide 1

Slide 1 text

A GENTLE INTRODUCTION TO FUNCTIONAL PROGRAMMING ENGINEERING MEETUP - 20TH APRIL 2020 Luca Di Stefano, Luca Ruzzola, Stefano Armenes

Slide 2

Slide 2 text

A GENTLE INTRODUCTION TO FUNCTIONAL PROGRAMMING WHY?

Slide 3

Slide 3 text

A GENTLE INTRODUCTION TO FUNCTIONAL PROGRAMMING Pillars - referential transparency - function composition - no side-effects

Slide 4

Slide 4 text

A GENTLE INTRODUCTION TO FUNCTIONAL PROGRAMMING No side-effects, really? - any operation that interacts with an external system - out-of-scope resources mutations - exceptions

Slide 5

Slide 5 text

A GENTLE INTRODUCTION TO FUNCTIONAL PROGRAMMING ANYTHING IS A FUNCTION …or better, an expression!

Slide 6

Slide 6 text

A GENTLE INTRODUCTION TO FUNCTIONAL PROGRAMMING Referential transparency is all about expression Is this function referential transparent?

Slide 7

Slide 7 text

A GENTLE INTRODUCTION TO FUNCTIONAL PROGRAMMING Referential transparency is all about expression and what about this?

Slide 8

Slide 8 text

A GENTLE INTRODUCTION TO FUNCTIONAL PROGRAMMING Referential transparency is all about expression and what about this?

Slide 9

Slide 9 text

A GENTLE INTRODUCTION TO FUNCTIONAL PROGRAMMING Pure function A pure function is a function that, given the same input, always returns the same output without producing any side-effect.

Slide 10

Slide 10 text

A GENTLE INTRODUCTION TO FUNCTIONAL PROGRAMMING Pure function A pure function is a function that, given the same input, always returns the same output without producing any side-effect. Benefits - readability - parallelisation - memoization - more testable

Slide 11

Slide 11 text

A GENTLE INTRODUCTION TO FUNCTIONAL PROGRAMMING We can compose two functions f and g when f domain is a subset of g domain. f ∘ g === f(g(x)) Composing functions

Slide 12

Slide 12 text

A GENTLE INTRODUCTION TO FUNCTIONAL PROGRAMMING Composing functions …but, we are missing somethings! We can compose two functions f and g when f domain is a subset of g domain. f ∘ g === f(g(x))

Slide 13

Slide 13 text

A GENTLE INTRODUCTION TO FUNCTIONAL PROGRAMMING Partial functions We want to live a world where every function is a total function Is this function total?

Slide 14

Slide 14 text

A GENTLE INTRODUCTION TO FUNCTIONAL PROGRAMMING Cool but, why my computation fails? Option[T]

Slide 15

Slide 15 text

A GENTLE INTRODUCTION TO FUNCTIONAL PROGRAMMING Either[L, R]

Slide 16

Slide 16 text

A GENTLE INTRODUCTION TO FUNCTIONAL PROGRAMMING Monads are your functional design patterns! Choose what you need IO List Option Either Set Future … Try

Slide 17

Slide 17 text

A GENTLE INTRODUCTION TO FUNCTIONAL PROGRAMMING DEMO

Slide 18

Slide 18 text

A GENTLE INTRODUCTION TO FUNCTIONAL PROGRAMMING Questions?

Slide 19

Slide 19 text

A GENTLE INTRODUCTION TO FUNCTIONAL PROGRAMMING Thanks!