Clojure - 可变参数函数
可变参数函数是采用不同数量参数的函数(某些参数是可选的)。 函数还可以指定 '&' 与符号来接受任意数量的参数。
以下示例展示了如何实现这一点。
(defn demo [message & others] (str message (clojure.string/join " " others)))
上面的函数声明在其他参数旁边有一个"&"符号,这意味着它可以接受任意数量的参数。
如果您调用上述函数
示例
(demo "Hello" "This" "is" "the" "message")
输出
以下是输出。
“HelloThis is the message”
'clojure.string/join' 用于组合每个单独的字符串参数,并将其传递给函数。