gkz / prelude-ls

prelude.ls is a functionally oriented utility library - powerful and flexible, almost all of functions are curried. It is written in, and is the recommended base library for, http://livescript.net
http://preludels.com/
MIT License
424 stars 57 forks source link

mapAccumL and mapAccumR #113

Open pier-bezuhoff opened 6 years ago

pier-bezuhoff commented 6 years ago

I suggest to add the functions:

mapAccumL :: (a -> b -> (a, c)) -> a -> [b] -> (a, [c])
mapAccumR :: (a -> b -> (a, c)) -> a -> [b] -> (a, [c])

from haskell Data.List They afford to map a list with auxiliary value, kind of hybrid of map and fold, applies a function to each element of a structure, passing an accumulating parameter from left to right or vice versa, and returning a final value of this accumulator together with the new structure. Possible implementation:

mapAccumL = (next, start, bs) -->
    | empty bs => [start, []]
    | otherwise => 
        scanned = tail <| scanl (([a, c], b) -> next a, b), [start, null] <| bs
        [head <| last scanned; map last <| scanned]