Last active
June 16, 2020 15:29
-
-
Save bsless/3e108823c58ba5e051ebb37bfda7b230 to your computer and use it in GitHub Desktop.
Either / ROP threading macro. Clojury
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 either-> | |
| [expr & clauses] | |
| (assert (zero? (rem (count clauses) 3))) | |
| (let [g (gensym) | |
| steps (map (fn [[test then else]] `(if (reduced? ~g) | |
| ~g | |
| (if (-> ~g ~test) | |
| (-> ~g ~then) | |
| (reduced (-> ~g ~else))))) | |
| (partition 3 clauses))] | |
| `(let [~g ~expr | |
| ~@(interleave (repeat g) (butlast steps))] | |
| ~(if (empty? steps) | |
| g | |
| (last steps))))) | |
| (either-> | |
| x | |
| pred1 then1 else1 | |
| pred2 then2 else2) | |
| ;;; expands to | |
| (let [G__7782 x | |
| G__7782 (if (reduced? G__7782) | |
| G__7782 | |
| (if (-> G__7782 pred1) | |
| (-> G__7782 then1) | |
| (reduced (-> G__7782 else1))))] | |
| (if (reduced? G__7782) | |
| G__7782 | |
| (if (-> G__7782 pred2) | |
| (-> G__7782 then2) | |
| (reduced (-> G__7782 else2))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment