Yetanotherpresentationon MONADSMarcinGryszko@mgryszko
whyam I doingthis?
accordingto Brian Marick…http://www.vimeo.com/20717301
Thereisanancient Mayan profecythatstatesthatsomedayevery single human onearthwillhave done a tutorial aboutmonadsand onthatpointthepurpose of theuniversewillhavebeenachievedand allhumanswillascenttoparadise
I’ll try to do my bit so thathumanity can reach nirvana
origins
XVIII century
Gottfried1714 Monadologie
eternal indecomposableindividualhierarchical and aggregatedautonomic and independentcan change itself according to some intrinsic lawsinfinitespace, time matter, and motion are phenomenalatomhuman beingsoulgod – first monadultimate elements of the universe
XX century
categorytheory
abstractview of mathematical concepts (which are already quite abstract )
categories have:objectsmorphisms or maps or arrowssee a relationship withclasses, objects and methods?
defines monadhowitisrelatedtofunctionalprogramming?
functionalprogramming
what are monads?
containersgeneralized interface for sequential computationspattern for transmitting state using functions without mutation
what defines a monad?
monadictypeMjava.util.Listunitoperationvalue -> monadconstructor/factory method[1, 2, 3] (Integer -> List<Integer>)bindoperationmonad -> 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]
what does bind?(typically)
Take a function and apply it to all values inside the monadEach invocation returns monad M<U>defintermediateMonads =        collect { x -> f(x) }Extract U values from all intermediate M<U> monads and assemble the final M<U> monadintermediateMonads.flatten()
monadic laws(or when a monad is a monad)
1. identitym.bind { x -> unit(x) } ≡ mtransforming to unit doesn’t change a monad
2. unitunit(x).bind(f) ≡ f(x)unit must preserve the value inside the monad
3. associativitym.bind(f).bind(g) ≡ m.bind{ x -> f(x).bind(g) }order of monad composition doesn’t matter
monadic zeros(optional, empty monad)[]
1. bindingwithzerom.flatMap({ x -> mzero})≡ mzero2. unitmzero.bind(f) ≡ mzero3. plusmzero.plus(m) ≡ mm.plus(mzero) ≡ m
examplesListAsMonadIOStateResult
sourceshttps://github.com/dsrkoc/monadologiehttp://james-iry.blogspot.com/2007/09/monads-are-elephants-part-1.htmlhttp://james-iry.blogspot.com/2007/10/monads-are-elephants-part-2.htmlhttp://james-iry.blogspot.com/2007/10/monads-are-elephants-part-3.htmlhttp://james-iry.blogspot.com/2007/11/monads-are-elephants-part-4.html
Thank you!

Groovy Monads

Editor's Notes

  • #10 Individual -each has its own identityAutonomic and independent - interactions are only apparentCan change itself according to some intrinsic laws -but the change comes from insideHierarchical and aggregated – complex monads can be composed from simple monads