大家好,
我听说如果在语言中加入transducers之前,它们将成为所有序列懒操作的基本构建块。由于它们是后来加入的,所以您需要稍微调整您的代码以使用transducers。而且我在想,为什么使用eduction
不是一个选择?我正在试图理解在何种情况下使用这种方法定义序列懒操作(当然假设具有相同的行为)是不具有性能优势的。
(defn map
([f] ;; the standard transducer definition
,,,)
([f coll]
(eduction (map f) coll))
我试图理解以下假设(可能是一个错误的假设):
(->> (range 5000000)
(eduction (map inc))
(eduction (filter odd?))
(eduction (map dec))
(eduction (filter even?))
(eduction (map (fn [n] (+ 3 n))))
(eduction (filter odd?))
(eduction (map inc))
(eduction (filter odd?))
(eduction (map dec))
(eduction (filter even?))
(eduction (map (fn [n] (+ 3 n))))
(eduction (filter odd?))
(into []))
这是相同的,
(->> (range 5000000)
(map inc)
(filter odd?)
(map dec)
(filter even?)
(map (fn [n] (+ 3 n)))
(filter odd?)
(map inc)
(filter odd?)
(map dec)
(filter even?)
(map (fn [n] (+ 3 n)))
(filter odd?)
(into []))