Skip to content

Instantly share code, notes, and snippets.

@ishikawa
Created September 23, 2010 09:16
Show Gist options
  • Save ishikawa/593380 to your computer and use it in GitHub Desktop.
Save ishikawa/593380 to your computer and use it in GitHub Desktop.
index-of-any - Programming Clojure "2. Exploring Clojure" p71
;; Programming Clojure "2. Exploring Clojure" p71
;;
; NOTE: The ``indexed`` function already exists as part of
; clojure-contrib (coljure.contrib.seq/indexed).
(defn indexed
"Returns a lazy sequence of [index, item] pairs, where items
come from 'coll' and indexes count up from zero."
[coll]
(map vector (iterate inc 0) coll))
(defn index-filter
"Returns the indexes of the matches."
[pred coll]
(when pred
(for [[idx, elt] (indexed coll) :when (pred elt)] idx)))
(defn index-of-any [pred col]
(first (index-filter pred col)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment