Created
September 21, 2013 01:35
-
-
Save shaunxcode/6646155 to your computer and use it in GitHub Desktop.
idiomatic way to apply a function with various arities
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
;;say you have something that looks like | |
(fnx a b) | |
(fnx a b2) | |
(fnx a b c) | |
(fnx a1 b2 c) | |
(fnx a2 b2 c2 d) | |
;;my goal is to avoid the repetition of fnx | |
(for [args [[a b] | |
[a b2] | |
[a b c] | |
[a1 b2 c] | |
[a2 b2 c2 d]]] | |
(apply fnx args)) | |
;;or | |
(doall (map (partial apply fnx) | |
[[a b] | |
[a b2] | |
[a b c] | |
[a1 b2 c] | |
[a2 b2 c2 d]])) | |
;;and finally what I call "applies" which is defined simply as | |
(defn applies | |
[f & args] | |
(doall | |
(map (partial apply f) | |
args))) | |
;;and used like | |
(applies fnx [a b] | |
[a b2] | |
[a b c] | |
[a1 b2 c] | |
[a2 b2 c2 d]) | |
;;it could easily be a macro though |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes. :)