目录

变量函数(Variadic Functions)

变量函数是使用不同数量的参数的函数(一些参数是可选的)。 函数还可以指定'&'符号,以接受任意数量的参数。

以下示例显示了如何实现这一目标。

(defn demo 
   [message & others]
   (str message (clojure.string/join " " others)))

上面的函数声明在参数other旁边有'&'符号,这意味着它可以使用任意数量的参数。

如果你调用上面的函数

例子 (Example)

(demo "Hello" "This" "is" "the" "message")

输出 (Output)

以下是输出。

“HelloThis is the message”

'clojure.string/join'用于组合传递给函数的每个单独的字符串参数。

↑回到顶部↑
WIKI教程 @2018