Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expand an seq into individual scalars

I want 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?

like image 219
kliron Avatar asked Nov 29 '25 22:11

kliron


1 Answers

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.

like image 68
Leonel Avatar answered Dec 04 '25 22:12

Leonel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!