Created
January 16, 2014 20:15
-
-
Save anonymous/8462610 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; monad | |
(defprotocol IBindable | |
(mbind [mv func])) | |
;; error monad | |
(defprotocol Error | |
(from-just [this]) | |
(get-error-text [this]) | |
(error? [this])) | |
(defprotocol Functor | |
(fmap [this func])) | |
(defn just [value] | |
(reify | |
Error | |
(from-just [this] | |
value) | |
(get-error-text [this] | |
(throw (js/Error (str "Trying to get-error-text from the just value \"" value "\"")))) | |
(error? [this] false) | |
IBindable | |
(mbind [this func] | |
(func value)) | |
Functor | |
(fmap [this func] | |
(just (func value))))) | |
(defn error [text] | |
(reify | |
Error | |
(from-just [this] | |
(throw (js/Error (str "Trying to from-just the error \"" text "\"")))) | |
(get-error-text [this] | |
text) | |
(error? [this] | |
true) | |
IBindable | |
(mbind [this func] | |
this) | |
Functor | |
(fmap [this func] | |
this))) | |
(defn mseq [col monad-constructor] | |
"Converts a seq of monads to a monad containing a seq of items | |
[m a] -> m [a] " | |
(reduce (fn [r i] | |
(mbind i | |
(fn [i] | |
(mbind r | |
(fn [r] | |
(monad-constructor (conj r i))))))) | |
(monad-constructor []) | |
col)) | |
(defn thrush [value & funcs] | |
(reduce (fn [result func] | |
(func result)) | |
value funcs)) | |
;;(js/console.log (clj->js (from-just (mseq [(just :a)] just)))) | |
;;(js/console.log (clj->js (from-just (mseq [(just :a) (error :b)] just)))) | |
#_(defn error-test [] | |
(log "error-test") | |
(let [a (fn [mv] (error "wahhhh")) | |
b (fn [v] (just (inc v)))] | |
(log (from-just (-> (just 1) | |
(mbind b) | |
(mbind b)))))) | |
#_(defn trush-test [] | |
(log (trush 1 | |
#(inc %) | |
#(inc %)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment