如果我们使用cat
转换器而不是调用(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])))))))