Skip to content

Instantly share code, notes, and snippets.

@jrsa
Last active August 5, 2018 23:05
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 jrsa/b742ca4348d01cc97954f2627f0c4594 to your computer and use it in GitHub Desktop.
Save jrsa/b742ca4348d01cc97954f2627f0c4594 to your computer and use it in GitHub Desktop.
;; 'wrap' any function to print the arguments
;; with the intention of visualizing what happens
;; when you pass a function to reduce
;; first version
(defn wrap [f & args]
(println args)
(apply f args))
(wrap + 4 5)
(reduce (fn [a b] (wrap + a b)) numbers)
;; how to rewrite so i can call like this?
(reduce (wrap +) numbers)
;; hmm...
(defn wrap [f]
(fn [& args]
(println args)
(apply f args)))
;;accept arbitrary wrapper fn?
(defn wrap [f w]
(fn [& args]
(apply w args)
(apply f args)))
;; accept collection of wrappers using comp
(defn wrap [f w]
(fn [& args]
((apply comp w) args)
(apply f args)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment