diff options
author | Stuart Halloway <stu@thinkrelevance.com> | 2010-05-05 09:32:59 -0400 |
---|---|---|
committer | Stuart Halloway <stu@thinkrelevance.com> | 2010-05-05 09:32:59 -0400 |
commit | 95dddbbdd748b0cc6d9c8486b8388836e6418848 (patch) | |
tree | 5c852b3a71a45408b015c350ef5fdba66320fc4c /src/main/clojure | |
parent | 48b81e07256c697c23a1aff896596300e7d0115b (diff) |
keep deprecated version of seq fns, safe(r) now that we have "last var wins"
Diffstat (limited to 'src/main/clojure')
-rw-r--r-- | src/main/clojure/clojure/contrib/gen_html_docs.clj | 2 | ||||
-rw-r--r-- | src/main/clojure/clojure/contrib/seq.clj | 116 | ||||
-rw-r--r-- | src/main/clojure/clojure/contrib/seq_utils.clj | 114 |
3 files changed, 202 insertions, 30 deletions
diff --git a/src/main/clojure/clojure/contrib/gen_html_docs.clj b/src/main/clojure/clojure/contrib/gen_html_docs.clj index a9a66742..5a96b911 100644 --- a/src/main/clojure/clojure/contrib/gen_html_docs.clj +++ b/src/main/clojure/clojure/contrib/gen_html_docs.clj @@ -48,7 +48,7 @@ one or more Clojure libraries."} clojure.contrib.gen-html-docs (:require [clojure.contrib.io :as io] [clojure.contrib.string :as s]) - (:use [clojure.contrib seq repl-utils def prxml]) + (:use [clojure.contrib repl-utils def prxml]) (:import [java.lang Exception] [java.util.regex Pattern])) diff --git a/src/main/clojure/clojure/contrib/seq.clj b/src/main/clojure/clojure/contrib/seq.clj index abac65c0..87986f94 100644 --- a/src/main/clojure/clojure/contrib/seq.clj +++ b/src/main/clojure/clojure/contrib/seq.clj @@ -14,30 +14,33 @@ ;; Change Log ;; -;; April 28, 2010 (Stuart Halloway): -;; -;; * BREAKING CHANGE: -;; moved to clojure.core: flatten, partition-all, frequencies, -;; reductions, shuffle, partition-by -;; moved with semantic changes: -;; group-by now returns an *unsorted* map -;; moved with name changes: -;; rand-elt => clojure.core/rand-nth -;; includes? => clojure.core/seq-contains? -;; ;; January 10, 2009 (Stuart Sierra): ;; ;; * BREAKING CHANGE: "includes?" now takes collection as first ;; argument. This is more consistent with Clojure collection ;; functions; see discussion at http://groups.google.com/group/clojure/browse_thread/thread/8b2c8dc96b39ddd7/a8866d34b601ff43 + (ns #^{:author "Stuart Sierra (and others)", :doc "Sequence utilities for Clojure"} clojure.contrib.seq (:import (java.util.concurrent LinkedBlockingQueue TimeUnit) - (java.lang.ref WeakReference))) - + (java.lang.ref WeakReference)) + (:refer-clojure :exclude [frequencies shuffle partition-by reductions partition-all group-by flatten])) + + +;; 'flatten' written by Rich Hickey, +;; see http://groups.google.com/group/clojure/msg/385098fabfcaad9b +(defn flatten + "DEPRECATED. Prefer clojure.core version. + Takes any nested combination of sequential things (lists, vectors, + etc.) and returns their contents as a single, flat sequence. + (flatten nil) returns nil." + {:deprecated "1.2"} + [x] + (filter (complement sequential?) + (rest (tree-seq sequential? seq x)))) (defn separate "Returns a vector: @@ -53,6 +56,46 @@ [s] (map vector (iterate inc 0) s)) +;; group-by written by Rich Hickey; +;; see http://paste.lisp.org/display/64190 +(defn group-by + "DEPRECATED. Prefer clojure.core version. + Returns a sorted map of the elements of coll keyed by the result of + f on each element. The value at each key will be a vector of the + corresponding elements, in the order they appeared in coll." + {:deprecated "1.2"} + [f coll] + (reduce + (fn [ret x] + (let [k (f x)] + (assoc ret k (conj (get ret k []) x)))) + (sorted-map) coll)) + +;; partition-by originally written by Rich Hickey; +;; modified by Stuart Sierra +(defn partition-by + "DEPRECATED. Prefer clojure.core version. + Applies f to each value in coll, splitting it each time f returns + a new value. Returns a lazy seq of lazy seqs." + {:deprecated "1.2"} + [f coll] + (when-let [s (seq coll)] + (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))))))) + +(defn frequencies + "DEPRECATED. Prefer clojure.core version. + Returns a map from distinct items in coll to the number of times + they appear." + {:deprecated "1.2"} + [coll] + (reduce (fn [counts x] + (assoc counts x (inc (get counts x 0)))) + {} coll)) + ;; recursive sequence helpers by Christophe Grand ;; see http://clj-me.blogspot.com/2009/01/recursive-seqs.html (defmacro rec-seq @@ -68,6 +111,21 @@ [binding-name & exprs] `(rec-seq ~binding-name (lazy-cat ~@exprs))) + +;; reductions by Chris Houser +;; see http://groups.google.com/group/clojure/browse_thread/thread/3edf6e82617e18e0/58d9e319ad92aa5f?#58d9e319ad92aa5f +(defn reductions + "DEPRECATED. Prefer clojure.core version. + Returns a lazy seq of the intermediate values of the reduction (as + per reduce) of coll by f, starting with init." + {:deprecated "1.2"} + ([f coll] + (if (seq coll) + (rec-seq self (cons (first coll) (map f self (rest coll)))) + (cons (f) nil))) + ([f init coll] + (rec-seq self (cons init (map f self coll))))) + (defn rotations "Returns a lazy seq of all rotations of a seq" [x] @@ -78,6 +136,34 @@ (iterate inc 0) x) (list nil))) +(defn partition-all + "DEPRECATED. Prefer clojure.core version. + Returns a lazy sequence of lists like clojure.core/partition, but may + include lists with fewer than n items at the end." + {:deprecated "1.2"} + ([n coll] + (partition-all n n coll)) + ([n step coll] + (lazy-seq + (when-let [s (seq coll)] + (cons (take n s) (partition-all n step (drop step s))))))) + +(defn shuffle + "DEPRECATED. Prefer clojure.core version. + Return a random permutation of coll" + {:deprecated "1.2"} + [coll] + (let [l (java.util.ArrayList. coll)] + (java.util.Collections/shuffle l) + (seq l))) + +(defn rand-elt + "DEPRECATED. Prefer clojure.core/rand-nth. + Return a random element of this seq" + {:deprecated "1.2"} + [s] + (nth s (rand-int (count s)))) + ;; seq-on written by Konrad Hinsen (defmulti seq-on "Returns a seq on the object s. Works like the built-in seq but as @@ -142,9 +228,9 @@ (defn includes? "Returns true if coll contains something equal (with =) to x, - in linear time. Deprecated. prefer 'contains?' for key testing, + in linear time. Deprecated. Prefer 'contains?' for key testing, or 'some' for ad hoc linear searches." - {:deprecated true} + {:deprecated "1.2"} [coll x] (boolean (some (fn [y] (= y x)) coll))) diff --git a/src/main/clojure/clojure/contrib/seq_utils.clj b/src/main/clojure/clojure/contrib/seq_utils.clj index 018fc2e8..3a199402 100644 --- a/src/main/clojure/clojure/contrib/seq_utils.clj +++ b/src/main/clojure/clojure/contrib/seq_utils.clj @@ -14,17 +14,6 @@ ;; Change Log ;; -;; April 28, 2010 (Stuart Halloway): -;; -;; * BREAKING CHANGE: -;; moved to clojure.core: flatten, partition-all, frequencies, -;; reductions, shuffle, partition-by -;; moved with semantic changes: -;; group-by now returns an *unsorted* map -;; moved with name changes: -;; rand-elt => clojure.core/rand-nth -;; includes? => clojure.core/seq-contains? -;; ;; January 10, 2009 (Stuart Sierra): ;; ;; * BREAKING CHANGE: "includes?" now takes collection as first @@ -37,8 +26,21 @@ :doc "Sequence utilities for Clojure"} clojure.contrib.seq-utils (:import (java.util.concurrent LinkedBlockingQueue TimeUnit) - (java.lang.ref WeakReference))) - + (java.lang.ref WeakReference)) + (:refer-clojure :exclude [frequencies shuffle partition-by reductions partition-all group-by flatten])) + + +;; 'flatten' written by Rich Hickey, +;; see http://groups.google.com/group/clojure/msg/385098fabfcaad9b +(defn flatten + "DEPRECATED. Prefer clojure.core version. + Takes any nested combination of sequential things (lists, vectors, + etc.) and returns their contents as a single, flat sequence. + (flatten nil) returns nil." + {:deprecated "1.2"} + [x] + (filter (complement sequential?) + (rest (tree-seq sequential? seq x)))) (defn separate "Returns a vector: @@ -54,6 +56,46 @@ [s] (map vector (iterate inc 0) s)) +;; group-by written by Rich Hickey; +;; see http://paste.lisp.org/display/64190 +(defn group-by + "DEPRECATED. Prefer clojure.core version. + Returns a sorted map of the elements of coll keyed by the result of + f on each element. The value at each key will be a vector of the + corresponding elements, in the order they appeared in coll." + {:deprecated "1.2"} + [f coll] + (reduce + (fn [ret x] + (let [k (f x)] + (assoc ret k (conj (get ret k []) x)))) + (sorted-map) coll)) + +;; partition-by originally written by Rich Hickey; +;; modified by Stuart Sierra +(defn partition-by + "DEPRECATED. Prefer clojure.core version. + Applies f to each value in coll, splitting it each time f returns + a new value. Returns a lazy seq of lazy seqs." + {:deprecated "1.2"} + [f coll] + (when-let [s (seq coll)] + (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))))))) + +(defn frequencies + "DEPRECATED. Prefer clojure.core version. + Returns a map from distinct items in coll to the number of times + they appear." + {:deprecated "1.2"} + [coll] + (reduce (fn [counts x] + (assoc counts x (inc (get counts x 0)))) + {} coll)) + ;; recursive sequence helpers by Christophe Grand ;; see http://clj-me.blogspot.com/2009/01/recursive-seqs.html (defmacro rec-seq @@ -69,6 +111,21 @@ [binding-name & exprs] `(rec-seq ~binding-name (lazy-cat ~@exprs))) + +;; reductions by Chris Houser +;; see http://groups.google.com/group/clojure/browse_thread/thread/3edf6e82617e18e0/58d9e319ad92aa5f?#58d9e319ad92aa5f +(defn reductions + "DEPRECATED. Prefer clojure.core version. + Returns a lazy seq of the intermediate values of the reduction (as + per reduce) of coll by f, starting with init." + {:deprecated "1.2"} + ([f coll] + (if (seq coll) + (rec-seq self (cons (first coll) (map f self (rest coll)))) + (cons (f) nil))) + ([f init coll] + (rec-seq self (cons init (map f self coll))))) + (defn rotations "Returns a lazy seq of all rotations of a seq" [x] @@ -79,6 +136,35 @@ (iterate inc 0) x) (list nil))) +(defn partition-all + "DEPRECATED. Prefer clojure.core version. + Returns a lazy sequence of lists like clojure.core/partition, but may + include lists with fewer than n items at the end." + {:deprecated "1.2"} + ([n coll] + (partition-all n n coll)) + ([n step coll] + (lazy-seq + (when-let [s (seq coll)] + (cons (take n s) (partition-all n step (drop step s))))))) + +(defn shuffle + "DEPRECATED. Prefer clojure.core version. + Return a random permutation of coll" + {:deprecated "1.2"} + [coll] + (let [l (java.util.ArrayList. coll)] + (java.util.Collections/shuffle l) + (seq l))) + +(defn rand-elt + "DEPRECATED. Prefer clojure.core/rand-nth. + Return a random element of this seq" + {:deprecated "1.2"} + [s] + (nth s (rand-int (count s)))) + + ;; seq-on written by Konrad Hinsen (defmulti seq-on "Returns a seq on the object s. Works like the built-in seq but as @@ -145,7 +231,7 @@ "Returns true if coll contains something equal (with =) to x, in linear time. Deprecated. Prefer 'contains?' for key testing, or 'some' for ad hoc linear searches." - {:deprecated true} + {:deprecated "1.2"} [coll x] (boolean (some (fn [y] (= y x)) coll))) |