3 FP Concepts: Recursion, List Comprehensions and Monads
-
Upload
diego-pacheco -
Category
Technology
-
view
3.988 -
download
0
description
Transcript of 3 FP Concepts: Recursion, List Comprehensions and Monads
Recursion, List comprehensions and Monads
@diego_pacheco about.me/diegopacheco
3 FP Concepts
List Comprehensions
Monads
3 FP Concepts
Recursion
Recursion
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…
Recursion
List Comprehensions
List Comprehensions
Syntactic construct based on lists
Inspired by math: set comprehensions
Expressions on lists, powerful stuff!
List Comprehensions
Monads
Monads => NO State on a world FULL of
State, Looks nonsense!? WTF?
FP/Haskell are against state? Hell No!
It’s all about discipline state…
How to Deal with state without global
shared state variables ?
Workaround => Truck Food as Monad !!!
(Abstraction)
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)
Monads as types/class (>>=), OOP!?
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
Monads
>>= (bind)
Monads
https://github.com/diegopacheco/functional-society
Higher Order Functions, Lambda and Currying
@diego_pacheco about.me/diegopacheco
3 FP Concepts
Thank You! Obrigado!