Created
August 30, 2012 16:29
-
-
Save laurentpetit/3532443 to your computer and use it in GitHub Desktop.
adapt-args
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
=> (defn adapt-args | |
"Delegate calls to f by first applying args-fns to its arguments | |
(applies identity if less args-fns than actual arguments)." | |
([f arg1-fn] | |
(fn | |
([arg1] (f (arg1-fn arg1))) | |
([arg1 arg2] (f (arg1-fn arg1) arg2)) | |
([arg1 arg2 arg3] (f (arg1-fn arg1) arg2 arg3)) | |
([arg1 arg2 arg3 & more] (apply f (arg1-fn arg1) arg2 arg3 more)))) | |
([f arg1-fn arg2-fn] | |
(fn | |
([arg1] (f (arg1-fn arg1))) | |
([arg1 arg2] (f (arg1-fn arg1) (arg2-fn arg2))) | |
([arg1 arg2 arg3] (f (arg1-fn arg1) (arg2-fn arg2) arg3)) | |
([arg1 arg2 arg3 & more] (apply f (arg1-fn arg1) (arg2-fn arg2) arg3 more)))) | |
([f arg1-fn arg2-fn & args-fn] | |
(fn [& args] | |
(apply f (map #(%1 %2) (cons arg1-fn (cons arg2-fn (concat args-fn (repeat identity)))) args))))) | |
(def log-addition (adapt-args + #(do (println %) %))) | |
(log-addition 1 2) | |
#'ll.core/adapt-args | |
#'ll.core/log-addition | |
1 | |
3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment