Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Monads you've already put in production (withou...
Search
Tejas Dinkar
October 10, 2014
Technology
1
1.1k
Monads you've already put in production (without knowing it)
Tejas Dinkar
October 10, 2014
Tweet
Share
More Decks by Tejas Dinkar
See All by Tejas Dinkar
Quick Wins for Page Speed
gja
0
100
Progressive Web Apps In Clojure(Script)
gja
4
2.3k
Lightning - Monads you already use (without knowing it)
gja
1
330
Native Extensions Served 3 Ways
gja
0
330
Other Decks in Technology
See All in Technology
スクラムチームを立ち上げる〜チーム開発で得られたもの・得られなかったもの〜
ohnoeight
2
350
フルカイテン株式会社 採用資料
fullkaiten
0
40k
Shopifyアプリ開発における Shopifyの機能活用
sonatard
4
250
誰も全体を知らない ~ ロールの垣根を超えて引き上げる開発生産性 / Boosting Development Productivity Across Roles
kakehashi
1
220
Python(PYNQ)がテーマのAMD主催のFPGAコンテストに参加してきた
iotengineer22
0
470
Security-JAWS【第35回】勉強会クラウドにおけるマルウェアやコンテンツ改ざんへの対策
4su_para
0
180
複雑なState管理からの脱却
sansantech
PRO
1
140
The Role of Developer Relations in AI Product Success.
giftojabu1
0
120
AGIについてChatGPTに聞いてみた
blueb
0
130
dev 補講: プロダクトセキュリティ / Product security overview
wa6sn
1
2.3k
テストコード品質を高めるためにMutation Testingライブラリ・Strykerを実戦導入してみた話
ysknsid25
7
2.6k
rootlessコンテナのすゝめ - 研究室サーバーでもできる安全なコンテナ管理
kitsuya0828
3
380
Featured
See All Featured
Rails Girls Zürich Keynote
gr2m
94
13k
How to Think Like a Performance Engineer
csswizardry
20
1.1k
Happy Clients
brianwarren
98
6.7k
How GitHub (no longer) Works
holman
310
140k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
329
21k
A better future with KSS
kneath
238
17k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
370
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
Teambox: Starting and Learning
jrom
133
8.8k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
109
49k
Transcript
Monads you are already using in prod Tejas Dinkar nilenso
about.me • Hi, I’m Tejas • Nilenso: Partner • twitter:
tdinkar • github: gja
Serious Pony
Online Abuse
Trouble at the Koolaid Point http://seriouspony.com/trouble-at-the-koolaid-point/ https://storify.com/adriarichards/telling-my-troll-story-because- kathy-sierra-left-t
If you think you understand Monads, you don't understand Monads.
None
This talk is inaccurate and will make a mathematician cry
None
Goal of this talk For you to say “Oh yeah,
I’ve used that hack”
None
Monads • Programmable Semicolons • Used to hide plumbing away
from you • You can say Monads in almost any sentence and people will think you are smart
None
Values Value
Monads Value Box
Mysore Masala Monad M onad Value
Monads Value Box
Monads • Monads define two functions • return takes a
value and puts it in a box • bind takes a box & function f, returning f(value) • it is expected that the function returns a box
Value Value Another Value Value Function return bind
Our Function Signatures Value f(value)
Some math (√4) + 5
Some math (√4) + 5 3 or 7!
Value 4
Monad [4]
[alive, dead]
ruby! x = [1, 2, 3] y = x.map {
|x| x + 1 } # y = [2, 3, 4]
return Value Value return
return def m_return(x) [x] end # m_return(4) => [4]
The functions Value f(value)
Square Root fn def sqrt(x) s = Math.sqrt(x) [s, -s]
end # sqrt(4) => [2, -2]
Increment Fn def inc_5(x) [x + 5] end # inc_5(1)
=> [6]
Bind Functions Another Value Value Function bind
Bind Function x = m_return(4) y = x.????? { |p|
sqrt(p) } # I want [-2, 2]
Bind Function x = m_return(4) y = x.map {|p| sqrt(p)
} # y => [[2, -2]] # ^—— Box in a box?
Bind Function x = m_return(4) y = x.mapcat {|p| sqrt(p)
} # y => [2, -2]
Putting it together m_return(4) .mapcat {|p| sqrt(p)} .mapcat {|p| inc_5(p)}
# => [3, 7]
You have invented the List Monad, used to model non-determinism
Congrats
Turtles all the way down
A small constraint • Let’s do a bit of a
self imposed constraint on this • Functions must return either 0 or 1 elements • (we’ll only model positive integers here)
return - stays the same
bind - stays the same x = m_return(4) y =
x.mapcat { |p| inc_5(p) } # y => 9
Square Root Fn def sqrt(x) if (x < 0) return
[] #error else [Math.sqrt(x)] end end # sqrt(4) => [2] # sqrt(-1) => []
Describe in English There is a list passed to each
step Maybe this list has just one element, or Maybe it has none
None
The Maybe Monad • The intent is to short circuit
computation • The value of the `box’ is None, or Just(Value) • You can think of it as a type-safe nil / null
try def try(x, f) if x == nil return f(x)
else return nil end end # 4.try { |x| x + 5 } => 9 # nil.try {|x| x + 5 } => nil
None
Let’s start over • The Monad Laws • Left Identity
• Right Identity • Associativity
Left Identity m_return(a).bind(f) == f(a)
Right Identity m.bind(m_return) == m
Associativity m.bind(f).bind(g) == m.bind(x -> f(x).bind(g))
Store Computation
The State Monad • Rest of the world - State
Machine (sorta) • The value inside the box f(state) => [r new-state] • Particularly useful in pure languages like Haskell • Let’s build a stack
The functions Value f(value)
The functions (f(value) state) [new-value, new-state]
push def push(val) lambda { |state| new_state = state.push(val) [value,
new_state] } end
pop def pop() lambda { |state| val = state.pop() [val,
state] } end
def double_top() lambda { |state| top = state.pop() [2 *
top, state.push(2*top)] } end double_top
return def m_return(x) lambda { |state| [x, state] } end
bind def bind(mv, f) lambda { |state| v, temp_state =
mv(state) state_fn = f(v) state_fn(temp_state) } end
example # Not working code ! m_return(4) .bind(a -> push(a))
.bind(b -> push(b + 1)) .bind(c -> double_top()) .bind(d -> sum_top2()) .bind(e -> pop())
None
Associativity m.bind(f).bind(g) == m.bind(x => f(x).bind(g))
turn this # Not working code ! m_return(4) .bind(a ->
push(a)) .bind(b -> push(b + 1)) .bind(c -> double_top()) .bind(d -> sum_top2()) .bind(e -> pop())
into this m_return(4) .bind(a -> push(a) .bind(b -> push(b +
1) .bind(c -> double_top() .bind(d -> sum_top() .bind(e -> pop())))))
done with ruby
imagine # Not working code state_monad { a <- m_return(4)
b <- push(a) c <- push(b + 1) d <- double_top() e <- sum_top2() pop() }
Back to List m_return(4) .mapcat {|p| sqrt(p)} .mapcat {|p| inc_5(p)}
# => [3, 7]
Back to List m_return(4) .mapcat {|a| sqrt(a) .mapcat {|b| inc_5(b)}}
# => [3, 7]
Back to List list_monad { a <- m_return(4) b <-
sqrt(a) c <- inc_5(b) c }
On to Clojure • this is an example from clojure.net
• the state is a vector containing every function we’ve called so far
(defn inc-s [x] (fn [state] [(inc x) (conj state :inc)]))
in clojure (defn inc-s [x] (fn [state] [(inc x) (conj
state :inc)])) (defn do-things [x] (domonad state-m [a (inc-s x) b (double-s a) c (dec-s b) d (dec-s c)] d)) ! ((do-things 7) []) => [14 [:inc :double :dec :dec]]
state monad in Clojure (defmonad state-m "Monad describing stateful computations.
The monadic values have the structure (fn [old-state] [result new-state])." [m-result (fn m-result-state [v] (fn [s] [v s])) m-bind (fn m-bind-state [mv f] (fn [s] (let [[v ss] (mv s)] ((f v) ss)))) ])
state monad in Haskell inc = state (\st -> let
st' = st +1 in (st’,st')) inc3 = do x <- inc y <- inc z <- inc return z
Finally, IO
IOMonad • rand-int(100) is non deterministic !
ay-yo
IOMonad • rand-int(100) is non deterministic • rand-int(100, seed =
42) is deterministic • monadic value: f(world) => [value, world-after-io]
IOMonad • puts() just `appends to a buffer’ in the
real world • How does gets() return different strings? • gets() returns a fixed value based on the `world’
Image Credits http://www.myfoodarama.com/2010/11/masala- dosa.html http://www.clojure.net/2012/02/10/State/ http://www.cafepress.com/ +no_place_like_home_ruby_slippers_3x5_area_rug, 796646161 http://www.netizens-stalbans.co.uk/installs-and- upgrades.html.htm
http://www.hpcorporategroup.com/what-is-the-life- box.html
Thank You MANY QUESTIONS? VERY MONAD SO FUNCTIONAL Y NO
CLOJURE?
[email protected]
@tdinkar WOW WOW WOW MUCH EASY SUPER SIMPLE