Monad transformer that transforms a monad m into a monad of stateful
computations that have the base monad type as their result.
(defn state-t
"Monad transformer that transforms a monad m into a monad of stateful
computations that have the base monad type as their result."
[m]
(monad [m-result (with-monad m
(fn m-result-state-t [v]
(fn [s]
(m-result [v s]))))
m-bind (with-monad m
(fn m-bind-state-t [stm f]
(fn [s]
(m-bind (stm s)
(fn [[v ss]]
((f v) ss))))))
m-zero (with-monad m
(if (= ::undefined m-zero)
::undefined
(fn [s]
m-zero)))
m-plus (with-monad m
(if (= ::undefined m-plus)
::undefined
(fn [& stms]
(fn [s]
(apply m-plus (map #(% s) stms))))))
]))
Comments top
No comments for state-t. Log in to add a comment.