Skip to content

Instantly share code, notes, and snippets.

@bostonaholic
Last active October 22, 2018 15:46
Show Gist options
  • Save bostonaholic/e171edf09cc3aa694fba0cb14ddb8900 to your computer and use it in GitHub Desktop.
Save bostonaholic/e171edf09cc3aa694fba0cb14ddb8900 to your computer and use it in GitHub Desktop.
Clojure and-> and or-> macros
(defmacro and->
"(and-> 1 int? odd?)
;;=> (and (int? 1) (odd? 1))"
[expr & tests]
`(and ~@(for [t# tests]
`(~t# ~expr))))
(defmacro and-> [x & forms] ;; 1, (int? odd?)
`(and ~(loop [x x
forms forms]
(if forms
(let [form (first forms)
threaded (if (seq? form)
(with-meta `(~(first form) ~x ~@(next form)) (meta form))
(list form x))]
(recur threaded (next forms)))
x))))
(defmacro and->>
""
[expr & tests]
`(and ~@(for [t# tests]
`(~t# ~expr))))
(defmacro or->
"(or-> 1 string? odd?)
;;=> (or (string? 1) (odd? 1))"
[expr & tests]
`(or ~@(for [t# tests]
`(~t# ~expr))))
(or-> 1 seq? odd?)
(defmacro or->> [expr & tests])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment