Skip to content

Feature request: traverseKeys and traverseKeysWith #771

Open
@mixphix

Description

@mixphix

Currently there are functions

mapKeys :: (Ord k) => (j -> k) -> Map j a -> Map k a
traverse :: (Applicative f) => (a -> f b) -> Map k a -> f (Map k b)

but nothing of the type (Applicative f, Ord k) => (j -> f k) -> Map j a -> f (Map k a).

A sample implementation:

traverseKeys :: (Applicative f, Ord k) => (j -> f k) -> Map j a -> f (Map k a)
traverseKeys f = fmap fromList . traverse (\(j, a) -> (,a) <$> f j) . assocs

traverseKeysWith :: (Applicative f, Ord k) => (a -> a -> a) -> (j -> f k) -> Map j a -> f (Map k a)
traverseKeysWith c f = fmap (fromListWith c) . traverse (\(j, a) -> (,a) <$> f j) . assocs

which is O(n log n), if I'm not mistaken; though I haven't looked through the internals of the implementation to dig for further optimizations.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions