aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Bedra and Stuart Halloway <pair@thinkrelevance.com>2009-08-23 15:01:31 -0400
committerAaron Bedra and Stuart Halloway <pair@thinkrelevance.com>2009-08-23 15:01:31 -0400
commitb70cba0c656586836a3c951079012cd9d371fa40 (patch)
tree5978f88adabee605391e817890fe337e95c39bc7
parent0b8b47631efe950df74d17d8f410eb8196342359 (diff)
add seq-utils/positions
-rw-r--r--src/clojure/contrib/seq_utils.clj18
-rw-r--r--src/clojure/contrib/test_contrib.clj3
-rw-r--r--src/clojure/contrib/test_contrib/seq_utils_test.clj14
3 files changed, 34 insertions, 1 deletions
diff --git a/src/clojure/contrib/seq_utils.clj b/src/clojure/contrib/seq_utils.clj
index d4c29c46..5f3b7446 100644
--- a/src/clojure/contrib/seq_utils.clj
+++ b/src/clojure/contrib/seq_utils.clj
@@ -211,3 +211,21 @@
(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]
+ (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.clj b/src/clojure/contrib/test_contrib.clj
index 13498076..8402b7ba 100644
--- a/src/clojure/contrib/test_contrib.clj
+++ b/src/clojure/contrib/test_contrib.clj
@@ -20,7 +20,8 @@
[:complex-numbers :fnmap :macro-utils :monads :pprint.pretty
:pprint.cl-format :str-utils :shell-out :test-graph
:test-dataflow :test-java-utils :test-lazy-seqs
- :test-trace :test-jmx :java-utils :expect-test :expect-test.test-adapter-test])
+ :test-trace :test-jmx :java-utils :expect-test :expect-test.test-adapter-test
+ :seq-utils-test])
(def test-namespaces
(map #(symbol (str "clojure.contrib.test-contrib." (name %)))
diff --git a/src/clojure/contrib/test_contrib/seq_utils_test.clj b/src/clojure/contrib/test_contrib/seq_utils_test.clj
new file mode 100644
index 00000000..1de61cb5
--- /dev/null
+++ b/src/clojure/contrib/test_contrib/seq_utils_test.clj
@@ -0,0 +1,14 @@
+(ns clojure.contrib.test-contrib.seq-utils-test
+ (:use clojure.test
+ clojure.contrib.seq-utils))
+
+
+(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]))
+ \ No newline at end of file