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

98 views Asked by At

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.

1

There are 1 answers

0
Taylor Wood On BEST ANSWER

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 e.g. (+ 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.