Skip to content

Instantly share code, notes, and snippets.

@laurentpetit
Created August 30, 2012 16:29
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 laurentpetit/3532443 to your computer and use it in GitHub Desktop.
Save laurentpetit/3532443 to your computer and use it in GitHub Desktop.
adapt-args
=> (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