Skip to content

Instantly share code, notes, and snippets.

@mnzk
Last active December 17, 2015 17:49
Show Gist options
  • Save mnzk/5648536 to your computer and use it in GitHub Desktop.
Save mnzk/5648536 to your computer and use it in GitHub Desktop.
clojure.algo.monads/maybe-t sample
(use 'clojure.algo.monads)
(def maybes-t (partial reduce maybe-t))
(with-monad (maybes-t maybe-m [0 :err1 :err2 :err3])
(defn div [x y z]
(domonad [a x b y c z] (/ a b c))))
(div 10 0 20) ;=> 0
(div nil 0 20) ;=> nil
(div :err2 0 20) ;=> :err2

algo.monadsの Maybeモナド変換子 (maybe-t) はnothing値を任意に指定できる。 でもnothingの判定は identical? で行われるので実行時情報を持たせたオブジェクトをnothingにしてeitherのように使うことは出来ない。 予め決められた選択肢から選択することはできる。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment