Expand an seq into individual scalars
I wan开发者_JAVA技巧t to feed the members of a lazy seq produced by map as individual arguments to another function. Is there a function that splices a (lazy) seq?
Use apply.
(defn f [a b c d e]
(str "a = " a " b = " b " c = " c " d = " d " e = " e))
(println (apply f (range 5)))
;; prints: a = 0 b = 1 c = 2 d = 3 e = 4
As you can see, function f takes 5 arguments and (range 5) returns a lazy seq of 5 arguments.
Just make sure the size of the seq is the same as the amount of arguments expected by the function, or you will get an exception at runtime.
精彩评论