Created
November 16, 2012 12:16
-
-
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
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
(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