Monad Monad Monad …
Friday, 31 August 2012
아 어렵다. 일단 문서들 모으고 파편적인 지식들만 끄적이기.
Articles
- Wikipedia – Monad (functional programming)
- You Could Have Invented Monads! (And Maybe You Already Have.)
Series
- A monad tutorial for Clojure programmers Part 1, Part 2, Part 3, Part 4
- Monads in Clojure Part 1, Part 2
- A Clojure Story – Monads Tags – 맨 아래부터 읽어보기
Monad in JavaScript
- Understanding Monads With JavaScript
- Monad syntax for JavaScript –
You Could Have Invented Monads! (And Maybe You Already Have.)를 JavaScript로 구현한 것.
Vidoes
- Monad Tutorial Part 1, Part 2, Part 3, Part 4
- Why is a Monad Like a Writing Desk?
- Introduction to Monads – Adam Smyczek
Papers
끄적끄적
A monad is nothing more than a way to compose functions. 함수를 조합하는 방법이다.
comp 쓰면 되지만? (comp f1 f2 f3) f1의 return이 f2에 들어갈 수 없다면? 중간에 f1의 return을 f2의 인자로 넘기기 위한 코드가 필요하다. f1, f2, f3의 signature가 동일하다면 monad로 만들 수 있고, 이러면 중간 코드를 숨기고 개별 함수들을 모듈화 할 수 있다. sequence monad가 좋은 예.
monadic function – plain value를 인자로 받아 monadic value를 return 한다.
m-bind, m-result
Set Theory
A monad is composed of two functions, m-result and m-bind, that obey the 3 monad laws.
3 monad laws
1. (m-bind (m-result x) f) === (f x)
2. (m-bind mv m-result) === mv
3. (m-bind (m-bind mv f) g) === (m-bind mv (fn [x] (m-bind (f x) g)))
sequence monad(sequence-m), identity monad(identity-m), maybe monad(maybe-m) 등 여러가지 monad가 존재한다.
monad는 일반적으로 많이 사용되는 패턴이라 clojure 내부에 구현되어 있다.
identity monad는 let form 과 동일하다.
;; let form
(let [a 1
b (inc a)]
(* a b))
;; => 2
;; identity monad
(domonad identity-m
[a 1
b (inc a)]
(* a b))
;; => 2
sequence monad는 for form과 동일하다.
;; for form
(for [a (range 5)
b (range a)]
(* a b))
;; => (0 0 2 0 3 6 0 4 8 12)
;; sequence monad
(domonad sequence-m
[a (range 5)
b (range a)]
(* a b))
;; => (0 0 2 0 3 6 0 4 8 12)