Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Monads, simple made easier
; A simple demo of monadic composition of side effects
; Program to take 3 nubers as input and print their sum.
(defn read-and-add!
[prev]
(print "Enter a number: ")
(+ prev (do (flush)
(Integer/parseInt (read-line)))))
(defn bind
[m next-fn]
(try
(next-fn m)
(catch Exception e (reduced (str e)))))
(defn chain
[init & effects]
(reduce bind init effects))
(println (chain 0
read-and-add!
read-and-add!
read-and-add!))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment