Last active
May 17, 2020 02:37
-
-
Save lispyclouds/17689e27ebd8a7f59b0ceefbcc18fdf2 to your computer and use it in GitHub Desktop.
Monads, simple made easier
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
; 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