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
![Page 1: 3 FP Concepts: Recursion, List Comprehensions and Monads](https://reader034.fdocuments.us/reader034/viewer/2022052523/5550f593b4c90572478b45e9/html5/thumbnails/1.jpg)
Recursion, List comprehensions and Monads
@diego_pacheco about.me/diegopacheco
3 FP Concepts
![Page 2: 3 FP Concepts: Recursion, List Comprehensions and Monads](https://reader034.fdocuments.us/reader034/viewer/2022052523/5550f593b4c90572478b45e9/html5/thumbnails/2.jpg)
List Comprehensions
Monads
3 FP Concepts
Recursion
![Page 3: 3 FP Concepts: Recursion, List Comprehensions and Monads](https://reader034.fdocuments.us/reader034/viewer/2022052523/5550f593b4c90572478b45e9/html5/thumbnails/3.jpg)
Recursion
![Page 4: 3 FP Concepts: Recursion, List Comprehensions and Monads](https://reader034.fdocuments.us/reader034/viewer/2022052523/5550f593b4c90572478b45e9/html5/thumbnails/4.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022052523/5550f593b4c90572478b45e9/html5/thumbnails/5.jpg)
Recursion
![Page 6: 3 FP Concepts: Recursion, List Comprehensions and Monads](https://reader034.fdocuments.us/reader034/viewer/2022052523/5550f593b4c90572478b45e9/html5/thumbnails/6.jpg)
List Comprehensions
![Page 7: 3 FP Concepts: Recursion, List Comprehensions and Monads](https://reader034.fdocuments.us/reader034/viewer/2022052523/5550f593b4c90572478b45e9/html5/thumbnails/7.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022052523/5550f593b4c90572478b45e9/html5/thumbnails/8.jpg)
List Comprehensions
![Page 9: 3 FP Concepts: Recursion, List Comprehensions and Monads](https://reader034.fdocuments.us/reader034/viewer/2022052523/5550f593b4c90572478b45e9/html5/thumbnails/9.jpg)
Monads
![Page 10: 3 FP Concepts: Recursion, List Comprehensions and Monads](https://reader034.fdocuments.us/reader034/viewer/2022052523/5550f593b4c90572478b45e9/html5/thumbnails/10.jpg)
Monads => NO State on a world FULL of
State, Looks nonsense!? WTF?
![Page 11: 3 FP Concepts: Recursion, List Comprehensions and Monads](https://reader034.fdocuments.us/reader034/viewer/2022052523/5550f593b4c90572478b45e9/html5/thumbnails/11.jpg)
FP/Haskell are against state? Hell No!
It’s all about discipline state…
![Page 12: 3 FP Concepts: Recursion, List Comprehensions and Monads](https://reader034.fdocuments.us/reader034/viewer/2022052523/5550f593b4c90572478b45e9/html5/thumbnails/12.jpg)
How to Deal with state without global
shared state variables ?
![Page 13: 3 FP Concepts: Recursion, List Comprehensions and Monads](https://reader034.fdocuments.us/reader034/viewer/2022052523/5550f593b4c90572478b45e9/html5/thumbnails/13.jpg)
Workaround => Truck Food as Monad !!!
(Abstraction)
![Page 14: 3 FP Concepts: Recursion, List Comprehensions and Monads](https://reader034.fdocuments.us/reader034/viewer/2022052523/5550f593b4c90572478b45e9/html5/thumbnails/14.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022052523/5550f593b4c90572478b45e9/html5/thumbnails/15.jpg)
Monads as types/class (>>=), OOP!?
![Page 16: 3 FP Concepts: Recursion, List Comprehensions and Monads](https://reader034.fdocuments.us/reader034/viewer/2022052523/5550f593b4c90572478b45e9/html5/thumbnails/16.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022052523/5550f593b4c90572478b45e9/html5/thumbnails/17.jpg)
Monads
>>= (bind)
![Page 18: 3 FP Concepts: Recursion, List Comprehensions and Monads](https://reader034.fdocuments.us/reader034/viewer/2022052523/5550f593b4c90572478b45e9/html5/thumbnails/18.jpg)
Monads
![Page 19: 3 FP Concepts: Recursion, List Comprehensions and Monads](https://reader034.fdocuments.us/reader034/viewer/2022052523/5550f593b4c90572478b45e9/html5/thumbnails/19.jpg)
https://github.com/diegopacheco/functional-society
![Page 20: 3 FP Concepts: Recursion, List Comprehensions and Monads](https://reader034.fdocuments.us/reader034/viewer/2022052523/5550f593b4c90572478b45e9/html5/thumbnails/20.jpg)
Higher Order Functions, Lambda and Currying
@diego_pacheco about.me/diegopacheco
3 FP Concepts
Thank You! Obrigado!