Clojure - 可變引數函式



可變引數函式是可以接受可變數量引數的函式(某些引數是可選的)。函式也可以指定“&”符號來接收任意數量的引數。

以下示例展示瞭如何實現這一點。

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

上述函式宣告在引數 `others` 旁邊使用了“&”符號,這意味著它可以接受任意數量的引數。

如果像下面這樣呼叫上述函式:

示例

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

輸出

輸出結果如下:

“HelloThis is the message”

`clojure.string/join` 用於組合傳遞給函式的每個單獨的字串引數。

clojure_functions.htm
廣告
© . All rights reserved.