Advertisement
Guest User

Untitled

a guest
Dec 24th, 2015
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;This macro was intended to create a looping function
  2. ;It defines a function "repeat-once" which may be called by itself
  3. ;But it fails "Unable to resolve symbol: repeat-once"
  4.  
  5. (defmacro do-once [expr]
  6.   "expr is of the form {:bind [bindings] :body body}. apply bindings once to body.
  7.  
  8. success example:
  9. (do-once {:bind [x 1 y 2] :body (+ x y)})
  10. ;;=> 3
  11. fail example:
  12. (do-once {:bind [x 5 y 2] :body (if (> x y) (repeat-once (dec x) y) true)})
  13. ;;=> CompilerException java.lang.RuntimeException: Unable to resolve symbol: repeat-once in this context, compiling:
  14. "
  15.   (let [
  16.         body (expr :body)
  17.         bind-symbols (take-nth 2 (expr :bind))
  18.         bind-values (take-nth 2 (rest (expr :bind)))
  19.         ]
  20.     `(let [
  21.            ~'repeat-once (fn ~(vec  bind-symbols) ~body)
  22.            ]
  23.      (apply ~'repeat-once ~(vec bind-values)))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement