Skip to content

Instantly share code, notes, and snippets.

@RutledgePaulV
Created November 23, 2016 17:29
Show Gist options
  • Save RutledgePaulV/45f0bebf28e79e585131cc422d945740 to your computer and use it in GitHub Desktop.
Save RutledgePaulV/45f0bebf28e79e585131cc422d945740 to your computer and use it in GitHub Desktop.
A macro for wrapping execution of a single form such that the form is not evaluated and nil is returned when given any nil arguments
(defmacro ? [form]
(let [f (first form) args (rest form)]
`(let [_# (list ~@args)]
(when (every? some? _#)
(apply ~f _#)))))
@RutledgePaulV
Copy link
Author

(? (println "stuff"))
stuff
=> nil
(? (println "stuff" nil))
=> nil
(? (str "one" "two"))
=> "onetwo"
(? (str "one" nil))
=> nil
(? (println "stuff" nil))
=> nil
(? (println "stuff" "more stuff"))
stuff more stuff
=> nil
(? (println (+ 1 2) "more stuff"))
3 more stuff
=> nil
(? (println (or nil) "more stuff"))
=> nil
(? (println (str "hello" nil) "more struff"))
hello more struff
=> nil
(? (println (? (str "hello" nil)) "more struff"))
=> nil

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment