Sir Robert
Sir Robert

Reputation: 4946

How can I "expand" a list to be arguments to a function?

How can I make these three snippets work?

(defn bar [a b c] (println a b c))
> (bar :a :b :c)
:a :b :c

(defn foo [a & args] (bar a args)) ;; some magic is needed here.
> (foo :a :b :c)
MoralException: You're a bad person for trying this.

I have looked all over for how to do this. I tried lots of things like (apply bar [a args]) but that's an ArityException (which makes sense). What can I do?

Upvotes: 2

Views: 1065

Answers (1)

Kyle
Kyle

Reputation: 22258

You don't need to wrap arguments to apply in a vector.

(apply bar a args)

Intervening arguments are prepended to args.

Upvotes: 2

Related Questions