Skip to content

mapAccumL and variants #304

Open
Open
@andrewthad

Description

@andrewthad

In Data.List, there is a function that maps over a collection while building an accumulator:

mapAccumL :: Traversable t => (a -> b -> (a, c)) -> a -> t b -> (a, t c) 

Specialized to Array, this would be:

mapAccumL :: (a -> b -> (a, c)) -> a -> Array b -> (a, Array c)

This is basically a combination of foldl and map. There is a less expressive variant of this where the accumulator is a Monoid and you do not inspect it (a comparison to the contrast between Writer and State is apt here). This variant looks like this:

mapAccumMonoid :: Monoid a => (b -> (a, c)) -> Array b -> (a, Array c)

I use this function somewhat often. I've thought about adding it to primitive, but I'm going to just add it to contiguous for the time being. Just wanted to get these thoughts written down somewhere.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions