Skip to content

Instantly share code, notes, and snippets.

@gaganjakhotiya
Created March 19, 2018 21:39
Show Gist options
  • Save gaganjakhotiya/b02cd823e0503b5d908d57c5b17279b6 to your computer and use it in GitHub Desktop.
Save gaganjakhotiya/b02cd823e0503b5d908d57c5b17279b6 to your computer and use it in GitHub Desktop.
Traditional looping in Elm
module Loop exposing (forEach)
loop : List a -> List b -> (a -> Int -> b) -> List b
loop a b fn =
case a of
[] ->
b
x::xs ->
loop xs (b ++ [(fn x (List.length b))]) fn
forEach : List a -> (a -> Int -> b) -> List b
forEach a fn =
loop a [] fn
-- Example Usage:
-- forEach [4,7,2,5] (\e i -> (i, e))
-- Output:
-- [(0,4),(1,7),(2,2),(3,5)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment