sum*_*mek 3 clojure transducer
是什么区别:
(transduce (comp fn-1 fn-2 fn-3) conj vector-collection)
Run Code Online (Sandbox Code Playgroud)
和
(eduction fn-1 fn-2 fn-3 vector-collection)
Run Code Online (Sandbox Code Playgroud)
我读过教育文档,但不明白教育的目的.
transduce
通过将减少功能应用于集合来减少换能器.计算结果.
eduction
只是记得你想要将传感器应用于集合的东西.Eduction本身并不是"常规意义"的集合,而是实现它的界面.因此,当您尝试打印时,它会像顺序打印一样.
看这里:
(defn my-tran [rf]
(fn
([]
(println "Arity 0!")
(rf))
([res]
(println "Arity 1!")
(rf res))
([result input]
(println "Arity 2!")
(rf result input))))
> (def a (transduce my-tran conj '(1 2 3)))
Arity 2!
Arity 2!
Arity 2!
Arity 1!
#'test.core/a ;a is already finished
> (def r (eduction my-tran '(1 2 3)))
#'test.core/r ;nothing was done
> r
Arity 2!
Arity 2!
Arity 2!
Arity 1!
(1 2 3) ;now it's done. Next call with calculate it again. Check it.
> (sequential? r)
true
Run Code Online (Sandbox Code Playgroud)
因此eduction
,将换能器部分应用于没有减少功能的集合.但事实并非如此lazy-seq
.因此,当你transduce
还是reduce
在eduction
它是相同的(在这个意义上,同样的工作是在这一刻完成,而不是在结果的意义上),如果你叫transduce
与还原功能,原来的集合.
看到这个: Clojure传感器的行为,其中有特殊的答案,涵盖了许多关于这个想法的问题.