Transduce:为什么这个 transduce 不打印任何东西
Transduce: Why this transduce doesn't print anything
我有以下代码,我希望它在控制台上打印 1,2,3,4
和 return [true,true,true,true]
。
但它 return 是空的,不会在控制台上打印任何内容。
逻辑是:它循环 (0..3)
,inc
每个元素乘 1,所以我得到 (1..4)
,然后 运行 (map (fn[x] (println x) true))
它需要1从惰性序列中,打印1,并且return为真,而take-while
将结果输出到[true]
,然后取2的下一个元素,打印2,return [true,true] 等所以结果应该打印 1,2,3,4 到控制台,和 return [true,true,true,true]
。但实际上,它什么都不打印,而且 return []
。这个怎么理解?
(transduce (comp (take-while true?)
(map (fn[x] (println x) true))
conj
[]
(map inc (range 4)))
那是因为
Composition of the transformer runs right-to-left but builds a transformation stack that is applied left-to-right (filtering happens before mapping in this example).
查看此处了解更多信息:http://clojure.org/transducers
我有以下代码,我希望它在控制台上打印 1,2,3,4
和 return [true,true,true,true]
。
但它 return 是空的,不会在控制台上打印任何内容。
逻辑是:它循环 (0..3)
,inc
每个元素乘 1,所以我得到 (1..4)
,然后 运行 (map (fn[x] (println x) true))
它需要1从惰性序列中,打印1,并且return为真,而take-while
将结果输出到[true]
,然后取2的下一个元素,打印2,return [true,true] 等所以结果应该打印 1,2,3,4 到控制台,和 return [true,true,true,true]
。但实际上,它什么都不打印,而且 return []
。这个怎么理解?
(transduce (comp (take-while true?)
(map (fn[x] (println x) true))
conj
[]
(map inc (range 4)))
那是因为
Composition of the transformer runs right-to-left but builds a transformation stack that is applied left-to-right (filtering happens before mapping in this example).
查看此处了解更多信息:http://clojure.org/transducers