From 78ee9b3e64c5ac6082fb223fc79292175e8e4f0c Mon Sep 17 00:00:00 2001 From: Stuart Halloway Date: Wed, 28 Apr 2010 15:56:24 -0400 Subject: update contrib to remove seq fns promoted to clojure.core --- src/main/clojure/clojure/contrib/command_line.clj | 3 +- .../clojure/clojure/contrib/datalog/literals.clj | 3 +- src/main/clojure/clojure/contrib/lazy_seqs.clj | 8 +- src/main/clojure/clojure/contrib/monads.clj | 10 +-- .../clojure/clojure/contrib/pprint/cl_format.clj | 4 +- src/main/clojure/clojure/contrib/seq.clj | 99 +++------------------- src/main/clojure/clojure/contrib/seq_utils.clj | 98 +++------------------ src/test/clojure/clojure/contrib/test_jmx.clj | 12 +-- .../clojure/clojure/contrib/test_repl_utils.clj | 13 ++- src/test/clojure/clojure/contrib/test_seq.clj | 53 ------------ src/test/clojure/clojure/contrib/test_with_ns.clj | 7 +- 11 files changed, 52 insertions(+), 258 deletions(-) diff --git a/src/main/clojure/clojure/contrib/command_line.clj b/src/main/clojure/clojure/contrib/command_line.clj index b93e251b..907b009f 100644 --- a/src/main/clojure/clojure/contrib/command_line.clj +++ b/src/main/clojure/clojure/contrib/command_line.clj @@ -12,11 +12,10 @@ #^{:author "Chris Houser", :doc "Process command-line arguments according to a given cmdspec"} clojure.contrib.command-line - (:require (clojure.contrib [seq :as su])) (:use (clojure.contrib [string :only (join)]))) (defn make-map [args cmdspec] - (let [{spec true [rest-sym] false} (su/group-by vector? cmdspec) + (let [{spec true [rest-sym] false} (group-by vector? cmdspec) rest-str (str rest-sym) key-data (into {} (for [[syms [_ default]] (map #(split-with symbol? %) (conj spec '[help? h?])) diff --git a/src/main/clojure/clojure/contrib/datalog/literals.clj b/src/main/clojure/clojure/contrib/datalog/literals.clj index 8a9e7948..37e5d8c9 100644 --- a/src/main/clojure/clojure/contrib/datalog/literals.clj +++ b/src/main/clojure/clojure/contrib/datalog/literals.clj @@ -18,8 +18,7 @@ (:use clojure.contrib.datalog.util) (:use clojure.contrib.datalog.database) (:use [clojure.set :only (intersection)]) - (:use [clojure.contrib.set :only (subset?)]) - (:use [clojure.contrib.seq :only (flatten)])) + (:use [clojure.contrib.set :only (subset?)])) ;;; Type Definitions diff --git a/src/main/clojure/clojure/contrib/lazy_seqs.clj b/src/main/clojure/clojure/contrib/lazy_seqs.clj index dda5aac5..6b42d0a6 100644 --- a/src/main/clojure/clojure/contrib/lazy_seqs.clj +++ b/src/main/clojure/clojure/contrib/lazy_seqs.clj @@ -21,7 +21,9 @@ ;; ;; == Lazy sequence functions == ;; -;; (rotations, partition-all, shuffle, rand-elt moved to seq_utils.clj) +;; (partition-all, shuffle moved to clojure.core) +;; (rand-elt moved to clojure.core/rand-nth) +;; (rotations, moved to seq_utils.clj) ;; (permutations and combinations moved to combinatorics.clj) ;; ;; [1] http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf @@ -46,7 +48,9 @@ ==== Lazy sequence functions ==== - (rotations, partition-all, shuffle, rand-elt moved to seq_utils.clj) + (partition-all, shuffle moved to clojure.core) + (rand-elt moved to clojure.core/rand-nth) + (rotations, rand-elt moved to seq_utils.clj) (permutations and combinations moved to combinatorics.clj) [1] http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf diff --git a/src/main/clojure/clojure/contrib/monads.clj b/src/main/clojure/clojure/contrib/monads.clj index 8d287105..8f90c01b 100644 --- a/src/main/clojure/clojure/contrib/monads.clj +++ b/src/main/clojure/clojure/contrib/monads.clj @@ -268,13 +268,13 @@ ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -(defn- flatten +(defn- flatten* "Like #(apply concat %), but fully lazy: it evaluates each sublist only when it is needed." [ss] (lazy-seq (when-let [s (seq ss)] - (concat (first s) (flatten (rest s)))))) + (concat (first s) (flatten* (rest s)))))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; @@ -313,10 +313,10 @@ [m-result (fn m-result-sequence [v] (list v)) m-bind (fn m-bind-sequence [mv f] - (flatten (map f mv))) + (flatten* (map f mv))) m-zero (list) m-plus (fn m-plus-sequence [& mvs] - (flatten mvs)) + (flatten* mvs)) ]) ; Set monad @@ -543,7 +543,7 @@ (fn m-bind-sequence-t [mv f] (m-bind mv (fn [xs] - (m-fmap flatten + (m-fmap flatten* (m-map f xs)))))) m-zero (with-monad m (m-result (list))) m-plus (with-monad m diff --git a/src/main/clojure/clojure/contrib/pprint/cl_format.clj b/src/main/clojure/clojure/contrib/pprint/cl_format.clj index 145697ff..0488a079 100644 --- a/src/main/clojure/clojure/contrib/pprint/cl_format.clj +++ b/src/main/clojure/clojure/contrib/pprint/cl_format.clj @@ -249,7 +249,7 @@ for improved performance" (clojure.core/format format-str val) (base-str base val)))) -(defn- group-by [unit lis] +(defn- group-by* [unit lis] (reverse (first (consume (fn [x] [(seq (reverse (take unit x))) (seq (drop unit x))]) (reverse lis))))) @@ -261,7 +261,7 @@ for improved performance" pos-arg (if neg (- arg) arg) raw-str (opt-base-str base pos-arg) group-str (if (:colon params) - (let [groups (map #(apply str %) (group-by (:commainterval params) raw-str)) + (let [groups (map #(apply str %) (group-by* (:commainterval params) raw-str)) commas (repeat (count groups) (:commachar params))] (apply str (next (interleave commas groups)))) raw-str) diff --git a/src/main/clojure/clojure/contrib/seq.clj b/src/main/clojure/clojure/contrib/seq.clj index 8575e732..5b335be5 100644 --- a/src/main/clojure/clojure/contrib/seq.clj +++ b/src/main/clojure/clojure/contrib/seq.clj @@ -14,13 +14,23 @@ ;; 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"} @@ -29,29 +39,12 @@ (java.lang.ref WeakReference))) -;; 'flatten' written by Rich Hickey, -;; see http://groups.google.com/group/clojure/msg/385098fabfcaad9b -(defn flatten - "Takes any nested combination of sequential things (lists, vectors, - etc.) and returns their contents as a single, flat sequence. - (flatten nil) returns nil." - [x] - (filter (complement sequential?) - (rest (tree-seq sequential? seq x)))) - (defn separate "Returns a vector: [ (filter f s), (filter (complement f) s) ]" [f s] [(filter f s) (filter (complement f) s)]) -(defn includes? - "Returns true if coll contains something equal (with =) to x, - in linear time." - [coll x] - (if (some (fn [y] (= y x)) coll) - true false)) - (defn indexed "Returns a lazy sequence of [index, item] pairs, where items come from 's' and indexes count up from zero. @@ -60,40 +53,6 @@ [s] (map vector (iterate inc 0) s)) -;; group-by written by Rich Hickey; -;; see http://paste.lisp.org/display/64190 -(defn group-by - "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." - [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 - "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 [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 - "Returns a map from distinct items in coll to the number of times - they appear." - [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 @@ -109,19 +68,6 @@ [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 - "Returns a lazy seq of the intermediate values of the reduction (as - per reduce) of coll by f, starting with init." - ([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] @@ -132,29 +78,6 @@ (iterate inc 0) x) (list nil))) -(defn partition-all - "Returns a lazy sequence of lists like clojure.core/partition, but may - include lists with fewer than n items at the end." - ([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 - "Return a random permutation of coll" - [coll] - (let [l (java.util.ArrayList. coll)] - (java.util.Collections/shuffle l) - (seq l))) - -(defn rand-elt - "Return a random element of this seq" - [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 diff --git a/src/main/clojure/clojure/contrib/seq_utils.clj b/src/main/clojure/clojure/contrib/seq_utils.clj index ad913f70..406f0d3e 100644 --- a/src/main/clojure/clojure/contrib/seq_utils.clj +++ b/src/main/clojure/clojure/contrib/seq_utils.clj @@ -14,6 +14,17 @@ ;; 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 @@ -29,29 +40,12 @@ (java.lang.ref WeakReference))) -;; 'flatten' written by Rich Hickey, -;; see http://groups.google.com/group/clojure/msg/385098fabfcaad9b -(defn flatten - "Takes any nested combination of sequential things (lists, vectors, - etc.) and returns their contents as a single, flat sequence. - (flatten nil) returns nil." - [x] - (filter (complement sequential?) - (rest (tree-seq sequential? seq x)))) - (defn separate "Returns a vector: [ (filter f s), (filter (complement f) s) ]" [f s] [(filter f s) (filter (complement f) s)]) -(defn includes? - "Returns true if coll contains something equal (with =) to x, - in linear time." - [coll x] - (if (some (fn [y] (= y x)) coll) - true false)) - (defn indexed "Returns a lazy sequence of [index, item] pairs, where items come from 's' and indexes count up from zero. @@ -60,40 +54,6 @@ [s] (map vector (iterate inc 0) s)) -;; group-by written by Rich Hickey; -;; see http://paste.lisp.org/display/64190 -(defn group-by - "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." - [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 - "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 [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 - "Returns a map from distinct items in coll to the number of times - they appear." - [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 @@ -109,19 +69,6 @@ [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 - "Returns a lazy seq of the intermediate values of the reduction (as - per reduce) of coll by f, starting with init." - ([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] @@ -132,29 +79,6 @@ (iterate inc 0) x) (list nil))) -(defn partition-all - "Returns a lazy sequence of lists like clojure.core/partition, but may - include lists with fewer than n items at the end." - ([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 - "Return a random permutation of coll" - [coll] - (let [l (java.util.ArrayList. coll)] - (java.util.Collections/shuffle l) - (seq l))) - -(defn rand-elt - "Return a random element of this seq" - [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 diff --git a/src/test/clojure/clojure/contrib/test_jmx.clj b/src/test/clojure/clojure/contrib/test_jmx.clj index 17ba1e55..ef7037b0 100644 --- a/src/test/clojure/clojure/contrib/test_jmx.clj +++ b/src/test/clojure/clojure/contrib/test_jmx.clj @@ -15,18 +15,18 @@ [javax.management MBeanAttributeInfo AttributeList] [java.util.logging LogManager Logger] clojure.contrib.jmx.Bean) - (:use clojure.test [clojure.contrib.seq :only (includes?)]) + (:use clojure.test) (:require [clojure.contrib [jmx :as jmx]])) (defn =set [a b] (= (set a) (set b))) -(defn includes-all? +(defn seq-contains-all? "Does container contain every item in containee? Not fast. Testing use only" [container containee] - (every? #(includes? container %) containee)) + (every? #(seq-contains? container %) containee)) (deftest finding-mbeans (testing "as-object-name" @@ -43,14 +43,14 @@ (deftest testing-actual-beans (testing "reflecting on capabilities" (are [attr-list mbean-name] - (includes-all? (jmx/attribute-names mbean-name) attr-list) + (seq-contains-all? (jmx/attribute-names mbean-name) attr-list) [:Verbose :ObjectPendingFinalizationCount :HeapMemoryUsage :NonHeapMemoryUsage] "java.lang:type=Memory") (are [op-list mbean-name] - (includes-all? (jmx/operation-names mbean-name) op-list) + (seq-contains-all? (jmx/operation-names mbean-name) op-list) [:gc] "java.lang:type=Memory")) (testing "mbean-from-oname" (are [key-names oname] - (includes-all? (keys (jmx/mbean oname)) key-names) + (seq-contains-all? (keys (jmx/mbean oname)) key-names) [:Verbose :ObjectPendingFinalizationCount :HeapMemoryUsage :NonHeapMemoryUsage] "java.lang:type=Memory"))) (deftest raw-reading-attributes diff --git a/src/test/clojure/clojure/contrib/test_repl_utils.clj b/src/test/clojure/clojure/contrib/test_repl_utils.clj index ac8376e8..77ae2aae 100644 --- a/src/test/clojure/clojure/contrib/test_repl_utils.clj +++ b/src/test/clojure/clojure/contrib/test_repl_utils.clj @@ -1,21 +1,20 @@ (ns clojure.contrib.test-repl-utils (:use clojure.test - clojure.contrib.repl-utils - [clojure.contrib.seq :only (includes?)])) + clojure.contrib.repl-utils)) (deftest test-apropos (testing "with a regular expression" (is (= '[defmacro] (apropos #"^defmacro$"))) - (is (includes? (apropos #"def.acr.") 'defmacro)) + (is (seq-contains? (apropos #"def.acr.") 'defmacro)) (is (= [] (apropos #"nothing-has-this-name")))) (testing "with a string" - (is (includes? (apropos "defmacro") 'defmacro)) - (is (includes? (apropos "efmac") 'defmacro)) + (is (seq-contains? (apropos "defmacro") 'defmacro)) + (is (seq-contains? (apropos "efmac") 'defmacro)) (is (= [] (apropos "nothing-has-this-name")))) (testing "with a symbol" - (is (includes? (apropos 'defmacro) 'defmacro)) - (is (includes? (apropos 'efmac) 'defmacro)) + (is (seq-contains? (apropos 'defmacro) 'defmacro)) + (is (seq-contains? (apropos 'efmac) 'defmacro)) (is (= [] (apropos 'nothing-has-this-name))))) diff --git a/src/test/clojure/clojure/contrib/test_seq.clj b/src/test/clojure/clojure/contrib/test_seq.clj index fafb4b05..144f7ced 100644 --- a/src/test/clojure/clojure/contrib/test_seq.clj +++ b/src/test/clojure/clojure/contrib/test_seq.clj @@ -53,10 +53,6 @@ #{1 2 3 4 5} '(1 2 3 4 5))) -(deftest test-includes? - (is (includes? [1 2 3 4 5] 5)) - (is (not (includes? [1 2 3 4 5] 6)))) - ;Note - this does not make sense for maps and sets, because order is expected (deftest test-indexed (are [expected test-seq] (= (indexed test-seq) expected) @@ -64,33 +60,6 @@ [[0 :a] [1 :b] [2 :c] [3 :d]] '(:a :b :c :d) [[0 \1] [1 \2] [2 \3] [3 \4]] "1234")) -(deftest test-group-by - (is (= (group-by even? [1 2 3 4 5]) - {false [1 3 5], true [2 4]}))) - -;Note - this does not make sense for maps and sets, because order is expected -(deftest test-partition-by - (are [test-seq] (= (partition-by (comp even? count) test-seq) - [["a"] ["bb" "cccc" "dd"] ["eee" "f"] ["" "hh"]]) - ["a" "bb" "cccc" "dd" "eee" "f" "" "hh"] - '("a" "bb" "cccc" "dd" "eee" "f" "" "hh")) - (is (=(partition-by #{\a \e \i \o \u} "abcdefghijklm") - [[\a] [\b \c \d] [\e] [\f \g \h] [\i] [\j \k \l \m]]))) - -(deftest test-frequencies - (are [expected test-seq] (= (frequencies test-seq) expected) - {\p 2, \s 4, \i 4, \m 1} "mississippi" - {1 4 2 2 3 1} [1 1 1 1 2 2 3] - {1 4 2 2 3 1} '(1 1 1 1 2 2 3))) - -;Note - this does not make sense for maps and sets, because order is expected -;This is a key differnce between reductions and reduce. -(deftest test-reductions - (is (= (reductions + [1 2 3 4 5]) - [1 3 6 10 15])) - (is (= (reductions + 10 [1 2 3 4 5]) - [10 11 13 16 20 25]))) - ;Note - this does not make sense for maps and sets, because order is expected (deftest test-rotations (is (= (rotations [1 2 3 4]) @@ -99,28 +68,6 @@ [3 4 1 2] [4 1 2 3]]))) -;Note - this does not make sense for maps and sets, because order is expected -(deftest test-partition-all - (is (= (partition-all 4 [1 2 3 4 5 6 7 8 9]) - [[1 2 3 4] [5 6 7 8] [9]])) - (is (= (partition-all 4 2 [1 2 3 4 5 6 7 8 9]) - [[1 2 3 4] [3 4 5 6] [5 6 7 8] [7 8 9] [9]]))) - -;Thanks to Andy Fingerhut for the idea of testing invariants -(deftest test-shuffle-invariants - (is (= (count (shuffle [1 2 3 4])) 4)) - (let [shuffled-seq (shuffle [1 2 3 4])] - (is (every? #{1 2 3 4} shuffled-seq)))) - -(deftest test-shuffle-distributions - (let [a-statistician-needed-to-do-this? true] - (is a-statistician-needed-to-do-this?))) - -;Thanks to Andy Fingerhut for the idea of testing invariants -(deftest test-rand-elt-invariants - (let [elt (rand-elt [:a :b :c :d])] - (is (#{:a :b :c :d} elt)))) - ;Note - this does not make sense for maps and sets, because order is expected (deftest test-find-first (is (= (find-first even? [1 2 3 4 5]) 2)) diff --git a/src/test/clojure/clojure/contrib/test_with_ns.clj b/src/test/clojure/clojure/contrib/test_with_ns.clj index 14ab5570..5ba62c0e 100644 --- a/src/test/clojure/clojure/contrib/test_with_ns.clj +++ b/src/test/clojure/clojure/contrib/test_with_ns.clj @@ -1,13 +1,12 @@ (ns clojure.contrib.test-with-ns (:use clojure.test - clojure.contrib.with-ns - [clojure.contrib.seq :only (includes?)])) + clojure.contrib.with-ns)) (deftest test-namespace-gets-removed (let [all-ns-names (fn [] (map #(.name %) (all-ns)))] (testing "unexceptional return" (let [ns-name (with-temp-ns (ns-name *ns*))] - (is (not (includes? (all-ns-names) ns-name))))) + (is (not (seq-contains? (all-ns-names) ns-name))))) (testing "when an exception is thrown" (let [ns-name-str (try @@ -16,4 +15,4 @@ (catch clojure.lang.Compiler$CompilerException e (-> e .getCause .getMessage)))] (is (re-find #"^sym.*$" ns-name-str)) - (is (not (includes? (all-ns-names) (symbol ns-name-str)))))))) + (is (not (seq-contains? (all-ns-names) (symbol ns-name-str)))))))) -- cgit v1.2.3-18-g5258