summaryrefslogtreecommitdiff
path: root/src/clj
diff options
context:
space:
mode:
authorDimitry Gashinsky <dimi...@gashinsky.com>2009-06-20 09:38:31 -0400
committerChouser <chouser@n01se.net>2009-06-20 10:45:47 -0400
commite0e8326871983be5615f5c0bc9dbf66140c7017f (patch)
treef5e16a16322a1389c1405fb855af097fc99c7037 /src/clj
parent31825fe1991baa3fc9375556ec6f6c595c175f4e (diff)
add optional pad argument to partition. Fixes #120
Signed-off-by: Chouser <chouser@n01se.net>
Diffstat (limited to 'src/clj')
-rw-r--r--src/clj/clojure/core.clj21
1 files changed, 15 insertions, 6 deletions
diff --git a/src/clj/clojure/core.clj b/src/clj/clojure/core.clj
index 86f4f4cf..7859ba44 100644
--- a/src/clj/clojure/core.clj
+++ b/src/clj/clojure/core.clj
@@ -1713,15 +1713,24 @@
(defn partition
"Returns a lazy sequence of lists of n items each, at offsets step
apart. If step is not supplied, defaults to n, i.e. the partitions
- do not overlap."
+ do not overlap. If a pad collection is supplied, use its elements as
+ necessary to complete last partition upto n items. In case there are
+ not enough padding elements, return a partition with less than n items."
([n coll]
(partition n n coll))
([n step coll]
- (lazy-seq
- (when-let [s (seq coll)]
- (let [p (take n s)]
- (when (= n (count p))
- (cons p (partition n step (drop step s)))))))))
+ (lazy-seq
+ (when-let [s (seq coll)]
+ (let [p (take n s)]
+ (when (= n (count p))
+ (cons p (partition n step (drop step s))))))))
+ ([n step pad coll]
+ (lazy-seq
+ (when-let [s (seq coll)]
+ (let [p (take n s)]
+ (if (= n (count p))
+ (cons p (partition n step pad (drop step s)))
+ (list (take n (concat p pad)))))))))
;; evaluation