Skip to content

Instantly share code, notes, and snippets.

@francoisdevlin
Created November 23, 2009 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save francoisdevlin/241144 to your computer and use it in GitHub Desktop.
Save francoisdevlin/241144 to your computer and use it in GitHub Desktop.
(defn visitor
"Used to implement visitor patterns. (first (args)) is modified by the visitor function"
[visit-fn return-fn]
(fn [f & args]
(return-fn
(apply f (visit-fn (first args)) (rest args)))))
(defn visitor*
[visit-fn return-fn]
(fn [f & args]
(let [r-args (reverse args)
mod-args (conj (rest r-args) (visit-fn (first r-args)))]
(return-fn
(apply f (reverse mod-args))))))
(def visit-keyword (visitor name keyword))
(def visit-symbol (visitor name symbol))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment