Last active
August 5, 2018 23:05
-
-
Save jrsa/b742ca4348d01cc97954f2627f0c4594 to your computer and use it in GitHub Desktop.
This file contains 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
;; '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