如果我们使用cat
transducer代替调用(concat p pad)
,则代码将更快
(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))))))))
可以更改为
(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])))))))