gpt4 book ai didi

dictionary - Clojure reducer /映射不工作

转载 作者:行者123 更新时间:2023-12-03 18:18:34 25 4
gpt4 key购买 nike

我有一个算法如下 -

(defn max-of
[args]
(into [] (apply map #(apply max %&) args)))

效果很好。

(max-of [[1 7] [3 5] [7 9] [2 2]]) 返回 [7 9]

它基本上找到每个位置上的最大元素。 7 是集合中最大的第一个元素,9 是最大的第二个元素。但是,当尝试使用 core.reducers 中的 reducer/map 时,我得到

CompilerException clojure.lang.ArityException:传递给:reducers/map 的参数数量错误 (21)

所以这不起作用 -

(defn max-of
[args]
(into [] (apply r/map #(apply max %&) args)))

为什么?

更新

我的最终代码是

(defn max-of [[tuple & tuples]]
(into [] (r/fold (fn
([] tuple)
([t1 t2] (map max t1 t2)))
(vec tuples))))

在其上运行快速工作台给出平均执行时间:626.125215 ms

我之前写过另一个算法 -

(defn max-fold
[seq-arg]
(loop [acc (transient []) t seq-arg]
(if (empty? (first t))
(rseq (persistent! acc))
(recur (conj! acc (apply max (map peek t))) (map pop t)))))

它做同样的事情。为此,我得到了 - 执行时间平均值:308.200310 ms,它比 r/fold 并行的东西快两倍。有什么想法吗?

顺便说一句,如果我从 r/fold 中删除 into [] ,那么我会得到执行时间平均值:13.101313 ms

最佳答案

r/map 需要 [f][f coll] - 所以你的 apply 方法不会'在这里工作

user=> (doc r/map)
-------------------------
clojure.core.reducers/map
([f] [f coll])

对比

user=> (doc map)
-------------------------
clojure.core/map
([f] [f coll] [f c1 c2] [f c1 c2 c3] [f c1 c2 c3 & colls])

关于dictionary - Clojure reducer /映射不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33218783/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com