Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing map behaviour in Clojure

Tags:

clojure

I need to modify map function behavior to provide mapping not with minimum collection size but with maximum and use zero for missing elements.

Standard behavior:

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

Needed behavior:

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

I wrote function to do this, but it seems not very extensible with varargs.

(defn map-ext [f coll1 coll2]
  (let [mx (max (count coll1) (count coll2))]
    (map f
     (concat coll1 (repeat (- mx (count coll1)) 0))
     (concat coll2 (repeat (- mx (count coll2)) 0)))))

Is there a better way to do this?

like image 688
mishadoff Avatar asked Jan 27 '12 13:01

mishadoff


2 Answers

Another lazy variant, usable with an arbitrary number of input sequences:

(defn map-ext [f ext & seqs]
  (lazy-seq
   (if (some seq seqs)
     (cons (apply f (map #(if (seq %) (first %) ext) seqs))
           (apply map-ext f ext (map rest seqs)))
     ())))

Usage:

user> (map-ext + 0 [1 2 3] [4 5 6 7 8])
(5 7 9 7 8)

user> (map-ext + 0 [1 2 3] [4 5 6 7 8] [3 4])
(8 11 9 7 8)
like image 43
Michał Marczyk Avatar answered Sep 27 '22 22:09

Michał Marczyk


Your method is concise, but inefficient (it calls count). A more efficient solution, which does not require the entirety of its input sequences to be stored in memory follows:

(defn map-pad [f pad & colls]
  (lazy-seq
   (let [seqs (map seq colls)]
     (when (some identity seqs)
       (cons (apply f (map #(or (first %) pad) seqs))
             (apply map-pad f pad (map rest seqs)))))))

Used like this:

user=> (map-pad + 0 [] [1] [1 1] (range 1 10))
(3 3 3 4 5 6 7 8 9)

Edit: Generalized map-pad to arbitrary arity.

like image 157
Trevor Caira Avatar answered Sep 27 '22 22:09

Trevor Caira