是否有可能实施;分区;使用clojure.core.reducer的函数?

是否有可能实施;分区;使用clojure.core.reducer的函数?,clojure,reducers,Clojure,Reducers,我有以下功能: (defn map-pairs [f coll] (map f (partition 2 1 coll))) 是否可以避免使用clojure.core.reducer创建中间2元素集合?不是现成的。但你可以: => (into [] (map2 + (range 10))) [1 3 5 7 9 11 13 15 17] 见: 不可以。您的f接受两元素集合作为参数;必须以某种方式创建,否则无法调用f。您可以避免在不使用减缩器的情况下创建它(映射f个数(删除1个数))

我有以下功能:

(defn map-pairs [f coll]
  (map f (partition 2 1 coll)))

是否可以避免使用clojure.core.reducer创建中间2元素集合?

不是现成的。但你可以:

=> (into [] (map2 + (range 10)))
[1 3 5 7 9 11 13 15 17]
见:


不可以。您的
f
接受两元素集合作为参数;必须以某种方式创建,否则无法调用
f
。您可以避免在不使用减缩器的情况下创建它
(映射f个数(删除1个数))
。这里f必须是2的算术式。如果f需要一个大小为2的列表,那么您可以
(comp f list)
将数字打包备份。感谢各位的回答。我想,当我避免使用2元素列表分配时,我会得到与循环/重现解决方案相当的速度,但循环/重现(coll是一个向量)似乎是最快的。
(deftype Fn2 [f1 f ^:volatile-mutable a ^:volatile-mutable b]
  clojure.lang.IFn
  (invoke [this] (f1))
  (invoke [this _] (set! b this)) ; hack to init the sentinel
  (invoke [this ret v] 
    (set! a b)
    (set! b v)
    (if (identical? a this)
      ret
      (f1 ret (f a b))))
  #_(invoke [this ret k v] to do))

(defn fn2 [f1 f] 1
  (let [f2 (Fn2. f1 f nil nil)]
    (f2 nil)
    f2))

(defn map2 [f coll]
  (r/reducer coll
    (fn [f1]
      (fn2 f1 f))))