Created
December 4, 2017 15:36
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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