Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up
for free
Lightning - Monads you already use (without knowing it)
Tejas Dinkar
July 19, 2014
Technology
1
120
Lightning - Monads you already use (without knowing it)
The lightning talk version of this talk.
Tejas Dinkar
July 19, 2014
Tweet
Share
More Decks by Tejas Dinkar
See All by Tejas Dinkar
gja
0
54
gja
3
1.7k
gja
1
810
gja
0
280
Other Decks in Technology
See All in Technology
ayatokura
0
100
eller86
1
240
kanaugust
PRO
0
160
uzabase_saas_product
0
110
s_uryu
0
190
piazza
0
190
110y
2
11k
dena_tech
15
3.6k
tenjuu99
1
310
iwashi
1
210
dena_tech
3
690
armaniacs
0
410
Featured
See All Featured
bkeepers
52
4.2k
dotmariusz
94
5.5k
marcelosomers
221
15k
paulrobertlloyd
72
1.4k
smashingmag
283
47k
ddemaree
273
31k
skipperchong
8
720
colly
66
3k
holman
447
130k
addyosmani
311
21k
tenderlove
53
3.5k
smashingmag
230
18k
Transcript
Monads you’ve already put in prod Tejas Dinkar Nilenso Software
about.me • Hi, I’m Tejas • Nilenso: Partner • twitter:
tdinkar • github: gja
If you think you understand Monads, you don't understand Monads.
This talk is inaccurate and will make a mathematician cry
Monads • Programmable Semicolons • Used to hide plumbing away
from you • Monads are just monoids in the category of endofunctors
None
Values Value
Monads Value Box
Monads • Monads define two functions • return takes a
value and puts it in a box • bind takes a box & function, and returns a box with f(value)
Some math (√4) + 5
Some math (√4) + 5 3 or 7!
Value 4
Monad [4]
return def m_return(x) [x] end # m_r(4) => [4]
Square Root fn def sqrt(x) s = Math.sqrt(x) [s, -s]
end # sqrt(4) => [2, -2]
Bind Function x = m_return(4) y = ?????(x) { |p|
sqrt(p) } # I want [-2, 2]
Bind Function x = m_return(4) y = x.flat_map {|p| sqrt(p)
} # y => [2, -2]
Increment Fn def inc_5(x) [x + 5] end # inc_5(1)
=> [6]
Putting it together m_return(4). flat_map {|p| sqrt(p)}. flat_map {|p| inc_5(p)}
# => [3, 7]
You have invented the List Monad, used to model non-determinism
Congrats