Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

hold less in partition and partition-all

  • Loading branch information...
commit 82c72549f455e68a314593c67436e7a14c64a931 1 parent da41290
Rich Hickey richhickey authored
Showing with 57 additions and 47 deletions.
  1. +57 −47 src/clj/clojure/core.clj
104 src/clj/clojure/core.clj
View
@@ -2712,6 +2712,57 @@
([keyfn ^java.util.Comparator comp coll]
(sort (fn [x y] (. comp (compare (keyfn x) (keyfn y)))) coll)))
+(defn dorun
+ "When lazy sequences are produced via functions that have side
+ effects, any effects other than those needed to produce the first
+ element in the seq do not occur until the seq is consumed. dorun can
+ be used to force any effects. Walks through the successive nexts of
+ the seq, does not retain the head and returns nil."
+ {:added "1.0"
+ :static true}
+ ([coll]
+ (when (seq coll)
+ (recur (next coll))))
+ ([n coll]
+ (when (and (seq coll) (pos? n))
+ (recur (dec n) (next coll)))))
+
+(defn doall
+ "When lazy sequences are produced via functions that have side
+ effects, any effects other than those needed to produce the first
+ element in the seq do not occur until the seq is consumed. doall can
+ be used to force any effects. Walks through the successive nexts of
+ the seq, retains the head and returns it, thus causing the entire
+ seq to reside in memory at one time."
+ {:added "1.0"
+ :static true}
+ ([coll]
+ (dorun coll)
+ coll)
+ ([n coll]
+ (dorun n coll)
+ coll))
+
+(defn nthnext
+ "Returns the nth next of coll, (seq coll) when n is 0."
+ {:added "1.0"
+ :static true}
+ [coll n]
+ (loop [n n xs (seq coll)]
+ (if (and xs (pos? n))
+ (recur (dec n) (next xs))
+ xs)))
+
+(defn nthrest
+ "Returns the nth rest of coll, coll when n is 0."
+ {:added "1.3"
+ :static true}
+ [coll n]
+ (loop [n n xs coll]
+ (if (and (pos? n) (seq xs))
+ (recur (dec n) (rest xs))
+ xs)))
+
(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
@@ -2725,15 +2776,15 @@
([n step coll]
(lazy-seq
(when-let [s (seq coll)]
- (let [p (take n s)]
+ (let [p (doall (take n s))]
(when (= n (count p))
- (cons p (partition n step (drop step s))))))))
+ (cons p (partition n step (nthrest s step))))))))
([n step pad coll]
(lazy-seq
(when-let [s (seq coll)]
- (let [p (take n s)]
+ (let [p (doall (take n s))]
(if (= n (count p))
- (cons p (partition n step pad (drop step s)))
+ (cons p (partition n step pad (nthrest s step)))
(list (take n (concat p pad)))))))))
;; evaluation
@@ -2802,37 +2853,6 @@
~@(when needrec [recform]))))))])))))]
(nth (step nil (seq seq-exprs)) 1)))
-(defn dorun
- "When lazy sequences are produced via functions that have side
- effects, any effects other than those needed to produce the first
- element in the seq do not occur until the seq is consumed. dorun can
- be used to force any effects. Walks through the successive nexts of
- the seq, does not retain the head and returns nil."
- {:added "1.0"
- :static true}
- ([coll]
- (when (seq coll)
- (recur (next coll))))
- ([n coll]
- (when (and (seq coll) (pos? n))
- (recur (dec n) (next coll)))))
-
-(defn doall
- "When lazy sequences are produced via functions that have side
- effects, any effects other than those needed to produce the first
- element in the seq do not occur until the seq is consumed. doall can
- be used to force any effects. Walks through the successive nexts of
- the seq, retains the head and returns it, thus causing the entire
- seq to reside in memory at one time."
- {:added "1.0"
- :static true}
- ([coll]
- (dorun coll)
- coll)
- ([n coll]
- (dorun n coll)
- coll))
-
(defn await
"Blocks the current thread (indefinitely!) until all actions
dispatched thus far, from this thread or agent, to the agent(s) have
@@ -3872,17 +3892,6 @@
([] (. clojure.lang.PersistentArrayMap EMPTY))
([& keyvals] (clojure.lang.PersistentArrayMap/createWithCheck (to-array keyvals))))
-(defn nthnext
- "Returns the nth next of coll, (seq coll) when n is 0."
- {:added "1.0"
- :static true}
- [coll n]
- (loop [n n xs (seq coll)]
- (if (and xs (pos? n))
- (recur (dec n) (next xs))
- xs)))
-
-
;redefine let and loop with destructuring
(defn destructure [bindings]
(let [bents (partition 2 bindings)
@@ -6288,7 +6297,8 @@
([n step coll]
(lazy-seq
(when-let [s (seq coll)]
- (cons (take n s) (partition-all n step (drop step s)))))))
+ (let [seg (doall (take n s))]
+ (cons seg (partition-all n step (nthrest s step))))))))
(defn shuffle
"Return a random permutation of coll"
Please sign in to comment.
Something went wrong with that request. Please try again.