20
Recursion, List comprehensions and Monads @diego_pacheco about.me/diegopacheco 3 FP Concepts

3 FP Concepts: Recursion, List Comprehensions and Monads

Embed Size (px)

DESCRIPTION

3 FP Concepts: Recursion, List Comprehensions and Monads

Citation preview

Page 1: 3 FP Concepts: Recursion, List Comprehensions and Monads

Recursion, List comprehensions and Monads

@diego_pacheco about.me/diegopacheco

3 FP Concepts

Page 2: 3 FP Concepts: Recursion, List Comprehensions and Monads

List Comprehensions

Monads

3 FP Concepts

Recursion

Page 3: 3 FP Concepts: Recursion, List Comprehensions and Monads

Recursion

Page 4: 3 FP Concepts: Recursion, List Comprehensions and Monads

Recursion

Function defined inside a function

Function calling it-self

Defining an infinite set of objects by a finite

statement (Recursion Power by Wikipedia)

Same behavior as loop, for, while, etc…

Page 5: 3 FP Concepts: Recursion, List Comprehensions and Monads

Recursion

Page 6: 3 FP Concepts: Recursion, List Comprehensions and Monads

List Comprehensions

Page 7: 3 FP Concepts: Recursion, List Comprehensions and Monads

List Comprehensions

Syntactic construct based on lists

Inspired by math: set comprehensions

Expressions on lists, powerful stuff!

Page 8: 3 FP Concepts: Recursion, List Comprehensions and Monads

List Comprehensions

Page 9: 3 FP Concepts: Recursion, List Comprehensions and Monads

Monads

Page 10: 3 FP Concepts: Recursion, List Comprehensions and Monads

Monads => NO State on a world FULL of

State, Looks nonsense!? WTF?

Page 11: 3 FP Concepts: Recursion, List Comprehensions and Monads

FP/Haskell are against state? Hell No!

It’s all about discipline state…

Page 12: 3 FP Concepts: Recursion, List Comprehensions and Monads

How to Deal with state without global

shared state variables ?

Page 13: 3 FP Concepts: Recursion, List Comprehensions and Monads

Workaround => Truck Food as Monad !!!

(Abstraction)

Page 14: 3 FP Concepts: Recursion, List Comprehensions and Monads

Monads

Deal with side-effects in a functional way

Mathematical construct

Without monads PURE FP would be nuts

Functional able todo IO

Good to Control tracking of things

Similar to AOP Interceptors

Function Composition: LINQ, Unix Pipes

You can build environments that support exactly

the features that you want (Scala Option[A])

Encapsulating two things: control flow (Maybe, Error, List, Continuation, parser monads)

state propagation (State, Reader, Writer, IO)

Page 15: 3 FP Concepts: Recursion, List Comprehensions and Monads

Monads as types/class (>>=), OOP!?

Page 16: 3 FP Concepts: Recursion, List Comprehensions and Monads

Monads have laws…

its not just type/classes

Make possible assumptions about the

type/class and his behavior.

1. Left Identity (apply function to value)

return x >>= f (the same as) f x

sample: return 3 >>= (\x -> Just (x+100000))

2. Right Identity (value to feed return)

m >>= return (the same as) m sample: [1,2,3,4] >>= (\x -> return x)

3. Associativity (no matter how nested chain)

(m >>= f) >>= g (the same as) m >>= (\x -> f x >>= g)

sample: return (0,0) >>= landRight 2 >>= landLeft 2 >>= landRight 2

Page 17: 3 FP Concepts: Recursion, List Comprehensions and Monads

Monads

>>= (bind)

Page 18: 3 FP Concepts: Recursion, List Comprehensions and Monads

Monads

Page 20: 3 FP Concepts: Recursion, List Comprehensions and Monads

Higher Order Functions, Lambda and Currying

@diego_pacheco about.me/diegopacheco

3 FP Concepts

Thank You! Obrigado!