Wei Qiu
Wei Qiu

Reputation: 945

What's the idiomatic way to map vector according to the longest seq in clojure?

(map vector [1 2 3] [4 5])

will give:

([1 4] [2 5])

Here 3 is discarded.

What if I want to pad those too short seqs to the largest length automatically?

e.g. What's the idiomatic way if I want to get

([1 4] [2 5] [3 nil])

Upvotes: 11

Views: 316

Answers (1)

amalloy
amalloy

Reputation: 92067

(defn map-all [f & colls]
  (lazy-seq
   (when (some seq colls)
     (cons (apply f (map first colls))
           (apply map-all f (map rest colls))))))

(map-all vector [1 2 3] [4 5])
;=> ([1 4] [2 5] [3 nil])

Upvotes: 14

Related Questions