Skip to content

Instantly share code, notes, and snippets.

@noisesmith
Created February 22, 2017 00:30
Show Gist options
  • Select an option

  • Save noisesmith/ad14d0b091626de380ad093780ff9915 to your computer and use it in GitHub Desktop.

Select an option

Save noisesmith/ad14d0b091626de380ad093780ff9915 to your computer and use it in GitHub Desktop.
r-> like -> but it respects reduced
(defmacro r->
[x & forms]
(loop [x x, forms forms]
(if forms
(let [form (first forms)
threaded (with-meta `(let [x# ~x]
(if (reduced? x#)
x#
(~(first form) x# ~@(next form))))
(meta form))]
(recur threaded (next forms)))
`(let [x# ~x]
(if (reduced? x#)
@x#
x#)))))
(comment
=> (r-> 1 (+ 2) (+ 3) (reduced) (+ 300))
6
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment