Created
September 29, 2015 11:53
-
-
Save fgui/48443e08844e42c674cd to your computer and use it in GitHub Desktop.
indexof.cljc
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
;; indexOf depends on hosted language (java/javascript) | |
;; recurrent solution , returns -1 on not-found | |
(defn index-of [coll value] | |
(loop [idx 0 items coll] | |
(cond | |
(empty? items) -1 | |
(= value (first items)) idx | |
:else (recur (inc idx) (rest items))))) | |
;; high order f, lazy seq , return nil on not-found | |
(defn index-of [coll value] | |
(some (fn [[item idx]] (if (= value item) idx)) | |
(partition 2 (interleave coll (iterate inc 0))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment