Created
September 23, 2010 09:16
-
-
Save ishikawa/593380 to your computer and use it in GitHub Desktop.
index-of-any - Programming Clojure "2. Exploring Clojure" p71
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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