Tejas Dinkar
October 10, 2014
1k

October 10, 2014

## Transcript

2. ### about.me • Hi, I’m Tejas • Nilenso: Partner • twitter:

tdinkar • github: gja

8. ### Goal of this talk For you to say “Oh yeah,

I’ve used that hack”
9. ### 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

14. ### Monads • Monads deﬁne 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

22. ### ruby! x = [1, 2, 3] y = x.map {

|x| x + 1 } # y = [2, 3, 4]

26. ### Square Root fn def sqrt(x) s = Math.sqrt(x) [s, -s]

end # sqrt(4) => [2, -2]

=> [6]

29. ### Bind Function x = m_return(4) y = x.????? { |p|

sqrt(p) } # I want [-2, 2]
30. ### Bind Function x = m_return(4) y = x.map {|p| sqrt(p)

} # y => [[2, -2]] # ^—— Box in a box?
31. ### Bind Function x = m_return(4) y = x.mapcat {|p| sqrt(p)

} # y => [2, -2]

# => [3, 7]

Congrats

35. ### 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)

37. ### bind - stays the same x = m_return(4) y =

x.mapcat { |p| inc_5(p) } # y => 9
38. ### Square Root Fn def sqrt(x) if (x < 0) return

[] #error else [Math.sqrt(x)] end end # sqrt(4) => [2] # sqrt(-1) => []
39. ### Describe in English There is a list passed to each

step Maybe this list has just one element, or Maybe it has none
40. ### 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
41. ### 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
42. ### Let’s start over • The Monad Laws • Left Identity

• Right Identity • Associativity

47. ### 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

50. ### push def push(val) lambda { |state| new_state = state.push(val) [value,

new_state] } end

state] } end
52. ### def double_top() lambda { |state| top = state.pop() [2 *

top, state.push(2*top)] } end double_top

54. ### bind def bind(mv, f) lambda { |state| v, temp_state =

mv(state) state_fn = f(v) state_fn(temp_state) } end
55. ### 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())

57. ### 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())
58. ### 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())))))

60. ### imagine # Not working code state_monad { a <- m_return(4)

b <- push(a) c <- push(b + 1) d <- double_top() e <- sum_top2() pop() }

# => [3, 7]

# => [3, 7]
63. ### Back to List list_monad { a <- m_return(4) b <-

sqrt(a) c <- inc_5(b) c }
64. ### On to Clojure • this is an example from clojure.net

• the state is a vector containing every function we’ve called so far

66. ### 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]]

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)))) ])
68. ### state monad in Haskell inc = state (\st -> let

st' = st +1 in (st’,st')) inc3 = do x <- inc y <- inc z <- inc return z

72. ### IOMonad • rand-int(100) is non deterministic • rand-int(100, seed =

42) is deterministic • monadic value: f(world) => [value, world-after-io]
73. ### IOMonad • puts() just `appends to a buffer’ in the

real world • How does gets() return different strings? • gets() returns a ﬁxed value based on the `world’
74. ### 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
75. ### Thank You MANY QUESTIONS? VERY MONAD SO FUNCTIONAL Y NO

CLOJURE? [email protected] @tdinkar WOW WOW WOW MUCH EASY SUPER SIMPLE