Ari*_*Ari 8 clojure clojure-contrib
我怎么能/我应该将单个序列作为参数传递给需要多个参数的函数?具体来说,我正在尝试使用笛卡尔积并将其传递给序列(见下文); 但是,当我这样做时,结果不是理想的结果.如果我不能传递单个序列作为参数,我怎么能/我应该将序列分解为多个参数?谢谢.
(use '[clojure.contrib.combinatorics :only (cartesian-product)])
(cartesian-product (["a" "b" "c"] [1 2 3]))
Run Code Online (Sandbox Code Playgroud)
结果是:
((["a" "b"]) ([1 2]))
Run Code Online (Sandbox Code Playgroud)
期望的结果
(("a" 1) ("a" 2) ("b" 1) ("b" 2))
Run Code Online (Sandbox Code Playgroud)
该apply函数从函数和包含函数参数的序列构建函数调用.
(apply cartesian-product '(["a" "b" "c"] [1 2 3]))
(("a" 1) ("a" 2) ("a" 3) ("b" 1) ("b" 2) ("b" 3) ("c" 1) ("c" 2) ("c" 3))
Run Code Online (Sandbox Code Playgroud)
另一个例子:
(apply + (range 10))
Run Code Online (Sandbox Code Playgroud)
计算(range 10)成一个序列(0 1 2 3 4 5 6 7 8 9)然后构建这个函数调用
(+ 0 1 2 3 4 5 6 7 8 9)
Run Code Online (Sandbox Code Playgroud)
for tunatly for for function很好地完成了.
(for [x ["a" "b"] y [1 2]] [x y])
(["a" 1] ["a" 2] ["b" 1] ["b" 2])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1599 次 |
| 最近记录: |