Skip to content

Instantly share code, notes, and snippets.

@lispyclouds
Last active May 17, 2020 02:37
Show Gist options
  • Save lispyclouds/17689e27ebd8a7f59b0ceefbcc18fdf2 to your computer and use it in GitHub Desktop.
Save lispyclouds/17689e27ebd8a7f59b0ceefbcc18fdf2 to your computer and use it in GitHub Desktop.
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