Created
March 19, 2018 21:39
-
-
Save gaganjakhotiya/b02cd823e0503b5d908d57c5b17279b6 to your computer and use it in GitHub Desktop.
Traditional looping in Elm
This file contains hidden or 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
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