Last active
October 22, 2018 15:46
-
-
Save bostonaholic/e171edf09cc3aa694fba0cb14ddb8900 to your computer and use it in GitHub Desktop.
Clojure and-> and or-> macros
This file contains 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 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