Changes for clojure.core/keep-indexed

clojureking about 16 hours ago
(defn position [x coll & {:keys [from-end all] :or {from-end false all false}}]
  (let [all-idxs (keep-indexed (fn [idx val] (when (= val x) idx)) coll)]
  (cond
   (true? from-end) (last all-idxs)
   (true? all)      all-idxs
   :else            (first all-idxs))))

user> (position [1 1] [[1 0][1 1][2 3][1 1]])
1
user> (position [1 1] [[1 0][1 1][2 3][1 1]] :from-end true)
3
user> (position [1 1] [[1 0][1 1][2 3][1 1]] :all true)
(1 3)

user> (def foo (shuffle (range 10)))
#'user/foo
user> foo
(5 8 9 1 2 7 0 6 3 4)
user> (position 5 foo)
0
user> (position 0 foo)
6
clojureking about 16 hours ago
(defn position [x coll & {:keys [from-end all] :or {from-end false all false}}]
  (let [all-idxs (keep-indexed (fn [idx val] (if (= val x) idx)) coll)]
  (cond
   (true? from-end) (last all-idxs)
   (true? all)      all-idxs
   :else            (first all-idxs))))

user> (position [1 1] [[1 0][1 1][2 3][1 1]])
1
user> (position [1 1] [[1 0][1 1][2 3][1 1]] :from-end true)
3
user> (position [1 1] [[1 0][1 1][2 3][1 1]] :all true)
(1 3)

user> (def foo (shuffle (range 10)))
#'user/foo
user> foo
(5 8 9 1 2 7 0 6 3 4)
user> (position 5 foo)
0
user> (position 0 foo)
6
yasuto on Fri, 27 May 2011
(defn position [x coll & {:keys [from-end all] :or {from-end false all false}}]
  (cond
   (true? from-end) (last (keep-indexed #(if (= x %2) %1) coll))
   (true? all) (keep-indexed #(if (= x %2) %1) coll)
   :else (first (keep-indexed #(if (= x %2) %1) coll))))

user> (position [1 1] [[1 0][1 1][2 3][1 1]])
1
user> (position [1 1] [[1 0][1 1][2 3][1 1]] :from-end true)
3
user> (position [1 1] [[1 0][1 1][2 3][1 1]] :all true)
(1 3)

user> (def foo (shuffle (range 10)))
#'user/foo
user> foo
(5 8 9 1 2 7 0 6 3 4)
user> (position 5 foo)
0
user> (position 0 foo)
6
yasuto on Fri, 27 May 2011
(defn position [x coll & {:keys [from-end all] :or {from-end false all false}}]
  (cond
   (true? from-end) (last (keep-indexed #(if (= x %2) %1) coll))
   (true? all) (keep-indexed #(if (= x %2) %1) coll)
   :else (first (keep-indexed #(if (= x %2) %1) coll))))

one-d-ca.core> (position [1 1] [[1 0][1 1][2 3][1 1]])
1
one-d-ca.core> (position [1 1] [[1 0][1 1][2 3][1 1]] :from-end true)
3
one-d-ca.core> (position [1 1] [[1 0][1 1][2 3][1 1]] :all true)
(1 3)

user> (def foo (shuffle (range 10)))
#'user/foo
user> foo
(5 8 9 1 2 7 0 6 3 4)
user> (position 5 foo)
0
user> (position 0 foo)
6
yasuto on Wed, 25 May 2011
(defn position [x coll & {:keys [from-end all] :or [from-end false all false]}]
  (cond
   (true? from-end) (last (keep-indexed #(if (= x %2) %1) coll))
   (true? all) (keep-indexed #(if (= x %2) %1) coll)
   :else (first (keep-indexed #(if (= x %2) %1) coll))))

user> (position [1 1] [[1 0][1 1][2 3]] :from-end true)
1
user> (position [1 1] [[1 0][1 1][2 3]] :all true)
(1)
user> (position [1 1] [[1 0][1 1][2 3]])
1
user> (def foo (shuffle (range 10)))
#'user/foo
user> foo
(5 8 9 1 2 7 0 6 3 4)
user> (position 5 foo)
0
user> (position 0 foo)
6