我正在编写一个自定义转换器作为练习,但我惊讶地发现它的 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])