Skip to content

Instantly share code, notes, and snippets.

@fgui
Created September 29, 2015 11:53
Show Gist options
  • Save fgui/48443e08844e42c674cd to your computer and use it in GitHub Desktop.
Save fgui/48443e08844e42c674cd to your computer and use it in GitHub Desktop.
indexof.cljc
;; 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