我認爲有一種方法可以做到這一點 - 我找不到確切的語法。將函數參數映射到Clojure的嵌套列表中
我想映射一個函數,它在大小爲三的元組列表上有三個參數。例如:
(def mylist '((1 2 3)(3 4 5)))
(defn myfunc [a b c] (println "this is the first value in this tuple: " a))
(map myfunc mylist)
有人可以給我精確的語法嗎?
我認爲有一種方法可以做到這一點 - 我找不到確切的語法。將函數參數映射到Clojure的嵌套列表中
我想映射一個函數,它在大小爲三的元組列表上有三個參數。例如:
(def mylist '((1 2 3)(3 4 5)))
(defn myfunc [a b c] (println "this is the first value in this tuple: " a))
(map myfunc mylist)
有人可以給我精確的語法嗎?
d11wtq's answer是一個非常好的一個,這是一條正確的方式,除非你已經有要映射的功能。
所以,如果myfunc
是外部函數,它是更好地使用apply
,而不是寫另外的包裝:
(map (partial apply myfunc) mylist)
你只需要一對方括號來解構嵌套的列表元素。
(defn myfunc
[[a b c]]
(println "this is the first value in this tuple: " a))
注意,但是,因爲map
返回一個懶惰-seq的,你可能不會得到副作用你這裏經過的時候,除非你強制序列的評價與doall
,或檢查序列中REPL。
文檔:http://clojure.org/special_forms#Special Forms--Binding Forms (Destructuring)