diff options
author | Stuart Sierra <mail@stuartsierra.com> | 2009-05-09 18:15:30 +0000 |
---|---|---|
committer | Stuart Sierra <mail@stuartsierra.com> | 2009-05-09 18:15:30 +0000 |
commit | 3d662f2faebe4757128e236f5238f68878806a6f (patch) | |
tree | bb9c8f18326bc8629f74c0465d30cd7be29ede6c /src/clojure | |
parent | c7bb828e73017a98eaa1a69edda9ea26bb71e4db (diff) |
seq_utils.clj: Issue 35: fixed partition-by to only call f once on each input
This allows partition-by to work even if f has side-effects.
Diffstat (limited to 'src/clojure')
-rw-r--r-- | src/clojure/contrib/seq_utils.clj | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/src/clojure/contrib/seq_utils.clj b/src/clojure/contrib/seq_utils.clj index b1eb83e5..969cce23 100644 --- a/src/clojure/contrib/seq_utils.clj +++ b/src/clojure/contrib/seq_utils.clj @@ -71,17 +71,18 @@ (assoc ret k (conj (get ret k []) x)))) (sorted-map) coll)) -;; partition-by written by Rich Hickey; -;; see http://paste.lisp.org/display/64190 +;; partition-by originally written by Rich Hickey; +;; modified by Stuart Sierra (defn partition-by "Applies f to each value in coll, splitting it each time f returns a new value. Returns a lazy seq of lazy seqs." [f coll] (when-let [s (seq coll)] - (let [fv (f (first s)) - run (take-while #(= fv (f %)) s)] + (let [fst (first s) + fv (f fst) + run (cons fst (take-while #(= fv (f %)) (rest s)))] (lazy-seq - (cons run (partition-by f (drop (count run) s))))))) + (cons run (new-partition-by f (drop (count run) s))))))) (defn frequencies "Returns a map from distinct items in coll to the number of times |