如果我们使用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])))))))