Logo

Programming-Idioms

History of Idiom 222 > diff from v10 to v11

Edit summary for version 11 by setupminimal:
New Haskell implementation by user [setupminimal]

Version 10

2020-04-28, 19:08:29

Version 11

2020-04-29, 00:34:41

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
Imports
import Data.Foldable (find)
import Data.Maybe (fromMaybe)
Code
findIndex x items = fst <$> (find ((==x) . snd) . zip [0..]) items

i = fromMaybe -1 (findIndex x items)