Vin*_*tin 7 clojure transducer
我正在编写一个自定义传感器作为练习,但我很惊讶地看到它的0-arity init函数没有被调用.
为什么?
它与我使用的聚合函数有关吗?如果是,哪些会调用init函数,为什么其他函数不调用?
(defn inc-xf [xf]
"inc-xf should be equivalent to (map inc)"
(fn
;; init
([]
(println "init") ;; <- this is not called (???)
(xf))
;; step
([result input]
(println "step" result input)
(xf result (inc input)))
;; completion
([result]
(println "completion" result)
(xf result))))
(transduce inc-xf
+
100
[5 5 5])
Run Code Online (Sandbox Code Playgroud)
如果你看一下你的实现,transduce可以看看会发生什么.
(defn transduce
;; To get the init value, (f) is used instead of ((xform f))
([xform f coll] (transduce xform f (f) coll))
([xform f init coll]
,,,))
Run Code Online (Sandbox Code Playgroud)
然而,为什么更难以回答.
实现零点的传感器是传感器要求的一部分,但它实际上从未在clojure.core的任何传感环境中使用.在邮件列表上有一个帖子询问与您相同的问题,并且实施的提议transduce实际上使用了init arity.然而,jira票据被拒绝了解释:
Rich要求我拒绝票证,因为xform的初始值不应该参与减少函数的累积. - 亚历克斯米勒
那么,如果传感器没有在任何地方使用,为什么它是传感器合同的初始部分呢?¯\ _(ツ)_ /¯
| 归档时间: |
|
| 查看次数: |
286 次 |
| 最近记录: |