-
-
Save kolharsam/c00a5395cb0b2f9176499d281290dc41 to your computer and use it in GitHub Desktop.
Monads, simple made easier
This file contains hidden or 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
; 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