Groovy Monads

30
Marcin Gryszko @mgryszko Yet another presentation on MONADS

Transcript of Groovy Monads

Page 1: Groovy Monads

Marcin Gryszko@mgryszko

Yet another presentation on MONADS

Page 2: Groovy Monads

why am I doing this?

Page 3: Groovy Monads

according to Brian Marick…http://www.vimeo.com/20717301

Page 4: Groovy Monads

There is an ancient Mayan profecy that states that some day every single human on earth will have done a tutorial about monads and on that point the purpose of the universe will have been achieved and all humans will ascent to paradise

Page 5: Groovy Monads

I’ll try to do my bit so that humanity can reach

nirvana

Page 6: Groovy Monads

origins

Page 7: Groovy Monads

XVIII century

Page 8: Groovy Monads

Gottfried

1714 Monadologie

Page 9: Groovy Monads

eternal indecomposable

individualhierarchical and aggregatedautonomic and independent

can change itself according to some intrinsic lawsinfinite

space, time matter, and motion are phenomenal

atomhuman being

soulgod – first monad

ultimate elements of the universe

Page 10: Groovy Monads

XX century

Page 11: Groovy Monads

category theory

Page 12: Groovy Monads

abstract view of mathematical concepts

(which are already quite abstract )

Page 13: Groovy Monads

categories have:objects

morphisms or maps or arrows

see a relationship withclasses, objects and

methods?

Page 14: Groovy Monads

defines monad

how it is related to functional programming?

Page 15: Groovy Monads

functional programmin

g

Page 16: Groovy Monads

what are monads?

Page 17: Groovy Monads

containers

generalized interface for sequential computations

pattern for transmitting state using functions

without mutation

Page 18: Groovy Monads

what defines a monad?

Page 19: Groovy Monads

monadic type Mjava.util.List

unit operationvalue -> monadconstructor/factory method[1, 2, 3] (Integer -> List<Integer>)

bind operationmonad -> next monad, exposing its internal value for a transformation functionany method in Groovy taking a closure as param[1, 2, 3].bind({ x -> [x, x + 1] }) == [1, 2, 2, 3, 3, 4]

Page 20: Groovy Monads

what does bind?(typically)

Page 21: Groovy Monads

Take a function and apply it to all values inside the monadEach invocation returns monad M<U>def intermediateMonads = collect { x -> f(x) }Extract U values from all intermediate M<U> monads and assemble the final M<U> monadintermediateMonads.flatten()

Page 22: Groovy Monads

monadic laws

(or when a monad is a monad)

Page 23: Groovy Monads

1. identity m.bind { x -> unit(x) } ≡ m

transforming to unit doesn’t change a monad

Page 24: Groovy Monads

2. unitunit(x).bind(f) ≡ f(x)

unit must preserve the value inside the monad

Page 25: Groovy Monads

3. associativity

m.bind(f).bind(g) ≡ m.bind{ x -> f(x).bind(g) }

order of monad composition doesn’t matter

Page 26: Groovy Monads

monadic zeros

(optional, empty monad)[]

Page 27: Groovy Monads

1. binding with zerom.flatMap({ x -> mzero })≡ mzero

2. unitmzero.bind(f) ≡ mzero

3. plusmzero.plus(m) ≡ mm.plus(mzero) ≡ m

Page 28: Groovy Monads

examplesListAsMonad

IOStateResult

Page 30: Groovy Monads

Thank you!