Welcome! Please see the About page for a little more info on how this works.

0 votes
in Clojure by
edited by

If we use the cat transducer instead of calling (concat p pad), the code is much faster:

(defn partitionv [n step pad coll]
  (lazy-seq
    (when-let [s (seq coll)]
      (let [p (into [] (take n) s)]
        (if (= n (count p))
          (cons p (partitionv n step pad (nthrest s step)))
          (list (into [] (take n) (concat p pad))))))))

can be changed to

(defn partitionv2 [n step pad coll]
  (lazy-seq
    (when-let [s (seq coll)]
      (let [p (into [] (take n) s)]
        (if (= n (count p))
          (cons p (partitionv n step pad (nthrest s step)))
          (list (into [] (comp cat (take n)) [p pad])))))))
by
To answer the question in the edit - it's actually the standard markdown syntax. :) https://www.markdownguide.org/basic-syntax#code-blocks-1 It's easier to use the editor buttons than to manually indent stuff.

1 Answer

0 votes
by

You're only changing the construction of the last possibly padded value, right?

It seems hard to believe that is "much faster". What are you testing?

by
Well, that's interesting, it seemed much faster on a few benchmarks in a row... But sometimes it's the other way...
Welcome to Clojure Q&A, where you can ask questions and receive answers from members of the Clojure community.
...