嗨,
我听说如果早在语言中加入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 []))