Changing map behaviour in Clojure

后端 未结 6 670
青春惊慌失措
青春惊慌失措 2020-12-20 18:50

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:

6条回答
  •  死守一世寂寞
    2020-12-20 19:41

    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)
    

提交回复
热议问题