Skip to content

fromListWithDefault #386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
maxigit opened this issue Jan 30, 2017 · 3 comments
Open

fromListWithDefault #386

maxigit opened this issue Jan 30, 2017 · 3 comments

Comments

@maxigit
Copy link

maxigit commented Jan 30, 2017

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 !

@treeowl
Copy link
Contributor

treeowl commented Jan 30, 2017 via email

@maxigit
Copy link
Author

maxigit commented Jan 31, 2017

About, the name, as it constructing a Map from a list, I think it should belongs to the fromList* family.
I thought initially of fromListWithDefault because we are just adding a default value to fromListWith.
In fact, we are mapping at the same time, so maybe fromListWithMap or something ...

@sjakobi
Copy link
Member

sjakobi commented Jul 15, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants