Skip to content

Instantly share code, notes, and snippets.

@shaunxcode
Created September 21, 2013 01:35
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 shaunxcode/6646155 to your computer and use it in GitHub Desktop.
Save shaunxcode/6646155 to your computer and use it in GitHub Desktop.
idiomatic way to apply a function with various arities
;;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
@xpe
Copy link

xpe commented Sep 21, 2013

Yes. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment