Skip to content

fromListWithDefault #386

Open
Open
@maxigit

Description

@maxigit

I often use Map to group list by a key. The result is of type Map k [a]. There is at the moment no easy way to build such Map even though this use case seems frequent. I have to use fromwith (++) but this means I have to wrap the value in a list instead of being able to just cons them (using :).

Given a list of tuple key-value, the code I'm using is Map.fromLift (++) [(k, [x]) | <- kxs ] but I think it
will better to be able to do something like Map.newFunction ([]) (:) kxs or equivalent. I know such a function is really to write using the existing functions. However, all solution involve having to wrap the value unnecessarily in a list.
The different options I can envisage would be

Ord k => b -> (a -> b -> b) -> [(k, a)] -> Map k b
Ord k => b -> (b -> a -> b) > [(k, a)] -> Map k b
Ord k => (a -> b) -> (b -> a -> b) -> [(k, a)] -> Map k b

Or alternatively, just regroup each value in a list and let user to fold them .

Ord k => [(k, a)] -> Map k [a]
Ord k => (a -> k) -> [a] -> Map k [a]

I hope the signature is enough to describe what each function does.
I'm happy to send PR if people things is a good idea and function names are welcome !

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