简体   繁体   中英

Why does the result of a mapping transducer include greater-than-2 arities?

The question is in the title. Below I copy the transducer-part of the source of map :

([f]
  (fn [rf]
    (fn
      ([] (rf))
      ([result] (rf result))
      ([result input]
         (rf result (f input)))
      ([result input & inputs] ;why?
         (rf result (apply f input inputs))))))

Here is a link to the source of clojure.core which contains the definition of map .

map can work on multiple collections at once, calling the mapping function with an argument for each collection item:

(map + [1 2 3] [4 5 6])
=> (5 7 9)

The + function is invoked once for each pair of values in those collections eg (+ 1 4) , (+ 2 5) , (+ 3 6) . The non-transducer version looks like this .

The map transducer works the same way:

(sequence (map +) [1 2 3] [4 5 6])
=> (5 7 9)

[ map ] Returns a lazy sequence consisting of the result of applying f to the set of first items of each coll , followed by applying f to the set of second items in each coll , until any one of the colls is exhausted . Any remaining items in other colls are ignored. Function f should accept number-of-colls arguments. Returns a transducer when no collection is provided.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM