prelude-ls icon indicating copy to clipboard operation
prelude-ls copied to clipboard

mapAccumL and mapAccumR

Open pier-bezuhoff opened this issue 6 years ago • 0 comments

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]

pier-bezuhoff avatar Apr 13 '18 19:04 pier-bezuhoff