如何理解这个猫的例子?
How to understand this example of cat?
clojuredocs.com
中有猫的例子
(into [] (comp cat cat (map inc)) [[[1] [2]] [[3] [4]]])
据我所知,comp
从左到右应用 fn,但上面的示例似乎是从右到左应用的。我该如何阅读?
"secret" 在 comp
函数中,它从右到左应用函数,但对于 transducers it is left to right。
既然你在转换,(comp cat cat (map inc))
有点像
(fn [xs] (map inc (sequence cat (sequence cat xs))))
(注意,cat
的版本中没有 coll)
简短回答:comp
-ing transducers 基本上运行它们 left-to-right。
来自上面的文档:
Transducers compose with ordinary function composition. A transducer performs its operation before deciding whether and how many times to call the transducer it wraps. The recommended way to compose transducers is with the existing comp function:
(def xf
(comp
(filter odd?)
(map inc)
(take 5)))
The transducer xf is a transformation stack that will be applied by a process to a series of input elements. Each function in the stack is performed before the operation it wraps. Composition of the transformer runs right-to-left but builds a transformation stack that runs left-to-right (filtering happens before mapping in this example).
As a mnemonic, remember that the ordering of transducer functions in comp is the same order as sequence transformations in ->>. The transformation above is equivalent to the sequence transformation:
(->> coll
(filter odd?)
(map inc)
(take 5))
clojuredocs.com
中有猫的例子(into [] (comp cat cat (map inc)) [[[1] [2]] [[3] [4]]])
据我所知,comp
从左到右应用 fn,但上面的示例似乎是从右到左应用的。我该如何阅读?
"secret" 在 comp
函数中,它从右到左应用函数,但对于 transducers it is left to right。
既然你在转换,(comp cat cat (map inc))
有点像
(fn [xs] (map inc (sequence cat (sequence cat xs))))
(注意,cat
的版本中没有 coll)
简短回答:comp
-ing transducers 基本上运行它们 left-to-right。
来自上面的文档:
Transducers compose with ordinary function composition. A transducer performs its operation before deciding whether and how many times to call the transducer it wraps. The recommended way to compose transducers is with the existing comp function:
(def xf (comp (filter odd?) (map inc) (take 5)))
The transducer xf is a transformation stack that will be applied by a process to a series of input elements. Each function in the stack is performed before the operation it wraps. Composition of the transformer runs right-to-left but builds a transformation stack that runs left-to-right (filtering happens before mapping in this example).
As a mnemonic, remember that the ordering of transducer functions in comp is the same order as sequence transformations in ->>. The transformation above is equivalent to the sequence transformation:
(->> coll (filter odd?) (map inc) (take 5))