From 1640c0433e35cf9a60e1483424c0a2a8ca56b730 Mon Sep 17 00:00:00 2001 From: Aaron Bedra and Stuart Halloway Date: Tue, 25 Aug 2009 21:11:59 -0400 Subject: positions takes only a predicate, per Rich's feedback --- src/clojure/contrib/expect/util.clj | 2 +- src/clojure/contrib/seq_utils.clj | 16 ++++------------ src/clojure/contrib/test_contrib/seq_utils_test.clj | 5 +---- 3 files changed, 6 insertions(+), 17 deletions(-) diff --git a/src/clojure/contrib/expect/util.clj b/src/clojure/contrib/expect/util.clj index 14bd6d16..6b834570 100644 --- a/src/clojure/contrib/expect/util.clj +++ b/src/clojure/contrib/expect/util.clj @@ -12,4 +12,4 @@ (defn index-of "Returns the first index of value v in the collection or nil." [coll v] - (first (positions v coll))) + (first (positions #{v} coll))) diff --git a/src/clojure/contrib/seq_utils.clj b/src/clojure/contrib/seq_utils.clj index 5f3b7446..ad913f70 100644 --- a/src/clojure/contrib/seq_utils.clj +++ b/src/clojure/contrib/seq_utils.clj @@ -211,20 +211,12 @@ (cons (if (identical? x NIL) nil x) (drain)))))))))) -(defmulti positions - "Returns a lazy sequence containing the positions at which item - is found in coll. Functions (responding true to fn?) are called - against members of the collection, other items are compared for - equality." - { :arglists '([item-or-pred coll]) } - (fn [item-or-pred _] - (if (fn? item-or-pred) :pred :item))) - -(defmethod positions :pred [pred coll] +(defn positions + "Returns a lazy sequence containing the positions at which pred + is true for items in coll." + [pred coll] (for [[idx elt] (indexed coll) :when (pred elt)] idx)) -(defmethod positions :item [item coll] - (for [[idx elt] (indexed coll) :when (= item elt)] idx)) diff --git a/src/clojure/contrib/test_contrib/seq_utils_test.clj b/src/clojure/contrib/test_contrib/seq_utils_test.clj index 1de61cb5..f8815041 100644 --- a/src/clojure/contrib/test_contrib/seq_utils_test.clj +++ b/src/clojure/contrib/test_contrib/seq_utils_test.clj @@ -5,10 +5,7 @@ (deftest test-positions (are [expected pred coll] (= expected (positions pred coll)) - [1] nil [0 nil 5] - [1] nil? [0 nil 5] - () string? [:a :b :c] [2] string? [:a :b "c"] () :d [:a :b :c] - [0 2] :d [:d :a :d :a])) + [0 2] #{:d} [:d :a :d :a])) \ No newline at end of file -- cgit v1.2.3-18-g5258