groovy monads

Download Groovy Monads

Post on 07-May-2015

3.405 views

Category:

Technology

2 download

Embed Size (px)

TRANSCRIPT

  • 1.Yetanotherpresentationon MONADS
    MarcinGryszko
    @mgryszko

2. whyam I doingthis?
3. accordingto Brian Marick
http://www.vimeo.com/20717301
4. Thereisanancient Mayan profecy
thatstatesthatsomedayevery single human onearthwillhave done a tutorial aboutmonads
and onthatpointthepurpose of theuniversewillhavebeenachieved
and allhumanswillascenttoparadise
5. Ill try to do my bit
so thathumanity can reach nirvana
6. origins
7. XVIII century
8. Gottfried
1714
Monadologie
9. eternal
indecomposable
individual
hierarchical and aggregated
autonomic and independent
can change itself according to some intrinsic laws
infinite
space, time matter, and motion are phenomenal
atom
human being
soul
god first monad
ultimate elements of the universe
10. XX century
11. categorytheory
12. abstractview of mathematical concepts (which are already quite abstract )
13. categories have:
objects
morphisms or maps or arrows
see a relationship with
classes, objects and methods?
14. defines monad
howitisrelatedtofunctionalprogramming?
15. functionalprogramming
16. what are monads?
17. containers
generalized interface for sequential computations
pattern for transmitting state using functions without mutation
18. what defines a monad?
19. monadictypeM
java.util.List
unitoperation
value -> monad
constructor/factory method
[1, 2, 3] (Integer -> List)
bindoperation
monad -> next monad, exposing its internal value for a transformation function
any method in Groovy taking a closure as param
[1, 2, 3].bind({ x -> [x, x + 1] })
== [1, 2, 2, 3, 3, 4]
20. what does bind?
(typically)
21. Take a function and apply it to all values inside the monad
Each invocation returns monad M
defintermediateMonads =
collect { x -> f(x) }
Extract U values from all intermediate M monads and assemble the final M monad
intermediateMonads.flatten()
22. monadic laws
(or when a monad is a monad)
23. 1. identity
m.bind { x -> unit(x) } m
transforming to unit doesnt change a monad
24. 2. unit
unit(x).bind(f) f(x)
unit must preserve the value inside the monad
25. 3. associativity
m.bind(f).bind(g)
m.bind{ x -> f(x).bind(g) }
order of monad composition doesnt matter
26. monadic zeros
(optional, empty monad)
[]
27. 1. bindingwithzero
m.flatMap({ x -> mzero}) mzero
2. unit
mzero.bind(f) mzero
3. plus
mzero.plus(m) m
m.plus(mzero) m
28. examples
ListAsMonad
IO
State
Result
29. sources
https://github.com/dsrkoc/monadologie
http://james-iry.blogspot.com/2007/09/monads-are-elephants-part-1.html
http://james-iry.blogspot.com/2007/10/monads-are-elephants-part-2.html
http://james-iry.blogspot.com/2007/10/monads-are-elephants-part-3.html
http://james-iry.blogspot.com/2007/11/monads-are-elephants-part-4.html
30. Thank you!

Recommended

View more >