Skip to content

Instantly share code, notes, and snippets.

@blancas
Created January 21, 2013 23:23
Show Gist options
  • Save blancas/4590513 to your computer and use it in GitHub Desktop.
Save blancas/4590513 to your computer and use it in GitHub Desktop.
A bit of the Writer monad.
(deftype Writer [value output]) ;; output must implement Monoid.
(extend-type Writer
Functor
(fun [this f]
(monad [x this] (return this (f x))))
Applicative
(<*> [this m]
(if (coll? m)
(monad [f this vs (seqm m)] (return this (apply f vs)))
(monad [f this x m] (return this (f x)))))
Monad
(>>= [m k]
(let [w (k (eval-writer m))]
(writer (eval-writer w)
(mappend (exec-writer m) (exec-writer w)))))
(return [this x]
(writer x (mempty (.output this)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment