Created
February 14, 2018 21:51
-
-
Save kspeakman/cc9c67d15db59c62ef6274eed1bbeac5 to your computer and use it in GitHub Desktop.
Revisions
-
kspeakman created this gist
Feb 14, 2018 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,43 @@ module ListFolds exposing (..) {-| Alternative implementations of List.foldl and List.foldr -- this will add the `fold` and `foldBack` functions to the List module import ListFolds as List List.fold (++) "" [ "Hello", " ", "World", "!" ] == "Hello World!" -} {-| Fold a list from the left, properly. List.fold (++) "" [ "Hello", " ", "World", "!" ] == "Hello World!" -- Compare to built-in `foldl`: List.foldl (++) "" [ "Hello", " ", "World", "!" ] == "!World Hello" -- What you actually have to do to use built-in: flip argument order List.foldl (\a b -> b ++ a) "" [ "Hello", " ", "World", "!" ] == "Hello World!" -} fold : (b -> a -> b) -> b -> List a -> b fold f b list = List.foldl (flip f) b list {-| Right fold with visual argument order -- direction of processing: 4<----------3<----------2<-----------1< List.foldBack (::) [ "Option 3", "Option 2", "Option 1" ] [ "Select an option" ] == [ "Select an option", "Option 1", "Option 2", "Option 3" ] -- --------------------------------v -- Compare to built-in `foldr`: >1-/ 4<----------3<-----------2 List.foldr (::) [ "Select an option" ] [ "Option 3", "Option 2", "Option 1" ] == [ "Select an option", "Option 1", "Option 2", "Option 3" ] -} foldBack : (a -> b -> b) -> List a -> b -> b foldBack f list b = List.foldr f b list