在以下方面有何不同:
(transduce (comp fn-1 fn-2 fn-3) conj vector-collection)和
(eduction fn-1 fn-2 fn-3 vector-collection)我读过教育博士,但不明白教育的目的。
发布于 2015-09-28 12:27:17
transduce通过将还原功能应用于集合来减少换能器。计算结果。
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因此,eduction是将传感器部分应用于一个没有还原功能的集合上。但这不是lazy-seq。因此,当您在transduce或reduce之上使用eduction时,它是相同的(在这个意义上,相同的工作是在此时完成的,而不是在结果的意义上),就好像您用一个简化函数调用transduce到原始集合一样。
参见:Clojure换能器行为,其中包含了关于这个想法的许多问题的特殊答案。
发布于 2018-04-13 01:46:39
只是为了将教育用例添加到@JustAnotherCurious‘伟大的解释中。
eduction允许您将一个或多个传感器与要被转换的集合绑定到可以转换为reduced的东西中,但eduction不执行任何转换。
相比之下,transduce实际上减少了采集,使用指定的换能器和减少功能。
--我在代码中使用创建或知道的转换程序和某些操作的集合,但是需要保持对简化函数的不可知。因此,我可以把一个教育的结果作为一个单一的单位来传递,并在已知的约简函数的某个地方使用它。
这可以帮助您保持您的方面分离(更正交的代码),并导致更干净的代码。
https://stackoverflow.com/questions/32822207
复制相似问题