Logo

Programming-Idioms

History of Idiom 222 > diff from v17 to v18

Edit summary for version 18 by programming-idioms.org:
[Clojure] Variable name items

Version 17

2021-01-14, 22:15:09

Version 18

2021-01-17, 17:51:39

Idiom #222 Find first index of an element in list

Set i to the first index in list items at which the element x can be found, or -1 if items does not contain x.

Idiom #222 Find first index of an element in list

Set i to the first index in list items at which the element x can be found, or -1 if items does not contain x.

Extra Keywords
position
Extra Keywords
position
Code
(defn find-index [x c]
  (reduce-kv
    #(if (= x %3) (reduced %2) %1) -1 c))
Code
(defn find-index [x items]
  (reduce-kv
    #(if (= x %3) (reduced %2) %1) -1 items))
Comments bubble
clojure already provides some cool iteration functions to be used with the concept of reduction
Comments bubble
Clojure already provides some iteration functions to be used with the concept of reduction