我需要修改map函数行为,以提供不是最小集合大小但最大化的映射,并且对于缺少的元素使用零.
标准行为:
(map + [1 2 3] [4 5 6 7 8]) => [5 7 9]
Run Code Online (Sandbox Code Playgroud)
需要的行为:
(map + [1 2 3] [4 5 6 7 8]) => [5 7 9 7 8]
Run Code Online (Sandbox Code Playgroud)
我编写了函数来执行此操作,但它似乎不能用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)))))
Run Code Online (Sandbox Code Playgroud)
有一个更好的方法吗?
你的方法简洁,但效率低下(它调用count).一个更有效的解决方案,它不需要将整个输入序列存储在内存中,如下所示:
(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)))))))
Run Code Online (Sandbox Code Playgroud)
像这样使用:
user=> (map-pad + 0 [] [1] [1 1] (range 1 10))
(3 3 3 4 5 6 7 8 9)
Run Code Online (Sandbox Code Playgroud)
编辑:广义map-pad到任意arity.
另一个惰性变体,可与任意数量的输入序列一起使用:
(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)))
())))
Run Code Online (Sandbox Code Playgroud)
用法:
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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
358 次 |
| 最近记录: |