Skip to content

mapAccumL and mapAccumR #113

@pier-bezuhoff

Description

@pier-bezuhoff

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]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions