gpt4 book ai didi

clojure - 教育与换能器组成

转载 作者:行者123 更新时间:2023-12-01 09:21:06 25 4
gpt4 key购买 nike

有什么区别:

(transduce (comp fn-1 fn-2 fn-3) conj vector-collection)


(eduction fn-1 fn-2 fn-3 vector-collection)

我已阅读 eduction docs但不明白教育的目的。

最佳答案

transduce通过将归约函数应用于集合来归约转换器。计算结果。
eduction只是记住您想将传感器应用于集合的东西。 Eduction 本身并不是“常规意义上的”集合,而是实现了它的接口(interface)。因此,当您尝试打印它时,它会像顺序一样打印自己。

看这里:

(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 .所以当你 transducereduce超过 eduction它与调用 transduce 是相同的(在此时完成相同的工作的意义上,而不是在结果的意义上)。对原始集合具有缩减功能。

看到这个:
Clojure transducers behavior ,
那里有一个特殊的答案,涵盖了关于这个想法的许多问题。

关于clojure - 教育与换能器组成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32822207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com