Programming-Idioms

This language bar is your friend. Select your favorite languages!

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.

import Data.Foldable (find)
import Data.Maybe (fromMaybe)
findIndex x items = fst <$> (find ((==x) . snd) . zip [0..]) items

i = fromMaybe -1 (findIndex x items)
i = findloc(items, x)
if (i == 0) i = -1
i := -1
for j, e := range items {
	if e == x {
		i = j
		break
	}
}
let i = items.indexOf(x);
i := Items.IndexOf(x);
i = items.index(x) if x in items else -1
i = items.index(x) || -1

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

programming-idioms.org

Related idioms