Skip to content

Instantly share code, notes, and snippets.

@duckyuck
Created November 16, 2012 12:16
Show Gist options
  • Save duckyuck/4086841 to your computer and use it in GitHub Desktop.
Save duckyuck/4086841 to your computer and use it in GitHub Desktop.
rudimentær implementasjon av 'let' og 'nil-safe let' vha identity og maybe monad
(defmacro domonad [monad [sym expr & more] & body]
(if more
`(~monad ~expr
(fn [~sym] (domonad ~monad ~more ~@body)))
`(~monad ~expr
(fn [~sym] ~@body))))
(defn identity-m [v f] (f v))
(defn maybe-m [v f]
(if (nil? v)
nil
(f v)))
(defmacro lett [bindings & body]
`(domonad identity-m ~bindings ~@body))
(defmacro safe-lett [bindings & body]
`(domonad maybe-m ~bindings ~@body))
(lett [a 1
b (inc a)
c (+ a b)]
(* a b c))
; => 6
(lett [a 1
b nil ; Denne er litt kjip
c (+ a b)]
(* a b c))
; => Æsj! NPE-trollet. Obviously!
(safe-lett [a 1
b nil ; Denne er fortsatt kjip, men ikke like kjip
c (+ a b)]
(* a b c))
; => nil (pweh, ingen NPE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment