Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@xpe xpe commented Sep 21, 2013

Yes. :)

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