Skip to content

Instantly share code, notes, and snippets.

@themaxhero
Created December 4, 2017 15:36
Show Gist options
  • Save themaxhero/be858c61dbe026e9c7eea3e2a8f760a8 to your computer and use it in GitHub Desktop.
Save themaxhero/be858c61dbe026e9c7eea3e2a8f760a8 to your computer and use it in GitHub Desktop.
Get a element index in a List on Elm for list without duplicates
getElementIndex : a -> List a -> Maybe Int
getElementIndex element list =
let
reducer item (index, found) =
if found then
(index, True)
else
if element == item then
(index, True)
else
(index+1, False)
folded =
List.foldl reducer (0, False) list
in
if Tuple.second folded then
Just <| Tuple.first folded
else
Nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment