Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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])
like image 795
Wei Qiu Avatar asked Apr 02 '13 16:04

Wei Qiu


1 Answers

(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])
like image 191
amalloy Avatar answered Dec 04 '22 06:12

amalloy