Conversation
|
I don't think you need a volatile or anything as complex. We can take the normal (defn map-padded [f val c1 c2 & colls]
(let [step (fn step [cs]
(lazy-seq
(let [ss (map seq cs)]
(when (some identity ss)
(cons (map #(if % (first %) val) ss)
(step (map rest ss)))))))]
(map #(apply f %) (step (conj colls c2 c1)))))Then we just need some more arities for performance in common cases. I've factored out the padded "first" function into an outer (letfn [(first* [s v] (if s (first s) v))]
(defn map-padded
([f val c1 c2]
(lazy-seq
(let [s1 (seq c1) s2 (seq c2)]
(when (or s1 s2)
(cons (f (first* s1 val) (first* s2 val))
(map-padded f val (rest s1) (rest s2)))))))
([f val c1 c2 c3]
(lazy-seq
(let [s1 (seq c1) s2 (seq c2) s3 (seq c3)]
(when (or s1 s2 s3)
(cons (f (first* s1 val) (first* s2 val) (first* s3 val))
(map-padded f val (rest s1) (rest s2) (rest s3)))))))
([f val c1 c2 c3 & colls]
(let [step (fn step [cs]
(lazy-seq
(let [ss (map seq cs)]
(when (some identity ss)
(cons (map #(first* % val) ss)
(step (map rest ss)))))))]
(map #(apply f %) (step (conj colls c3 c2 c1)))))))It looks like the multi-coll version of |
3a6fd9c to
069f24c
Compare
|
I used your implementation except for some small changes, which I left as comments. |
|
I did some performance testing between my initial version and your new version (using |
|
As I said in the issue, |
|
Thanks for doing the benchmarks, and for the performance improvements. I initially thought |
|
I found a tidy solution for |
ecebaa9 to
6f80975
Compare
|
looking at the code again, i should write a test verifying |
src/medley/core.cljc
Outdated
| (lazy-seq | ||
| (let [ss (mapv seq cs)] | ||
| (if (some identity ss) | ||
| (let [res (apply f nil (mapv #(first* % val) ss))] |
There was a problem hiding this comment.
Let's not wrap both functions in the letfn. Instead, we can inline first*.
(mapv #(if % (first %) val) ss)It might also be worth doing that for map-padded... on reflection, the first* function doesn't save much space...
There was a problem hiding this comment.
first* is used a ton in map-padded because we have 3 arities. It's only used once in sequence-padded, so I think removing it from that probably makes the most sense.
6f80975 to
6b27cb1
Compare
A demonstration for #101.