-
Notifications
You must be signed in to change notification settings - Fork 182
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
Labels
Comments
Ah, now I understand the (a -> b) function. You're using that because you
surely have at least one element if you're folding. I'm leaning toward
foldl'Duplicates :: (b -> a -> b) -> (a -> b) -> [(k, a)] -> Map k b
foldrDuplicates :: (a -> b -> b) -> (a -> b) -> [(k, a)] -> Map k b
but these are also a bit ugly. Another option is
foldMapDuplicatesLeftStrict:: Semigroup s => (a -> s) -> [(k, a)] -> Map k s
foldMapDuplicatesRightStrict :: Semigroup s => (a -> s) -> [(k, a)] -> Map
k s
but I can't seem to find good names for those.
…On Mon, Jan 30, 2017 at 5:20 PM, Max ***@***.***> wrote:
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 !
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#386>, or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABzi_e3ufBOVyNDQxRNhkbDaGIf0iLJyks5rXmIsgaJpZM4Lx_ig>
.
|
About, the name, as it constructing a Map from a list, I think it should belongs to the |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I often use
Map
to group list by a key. The result is of typeMap k [a]
. There is at the moment no easy way to build such Map even though this use case seems frequent. I have to usefromwith (++)
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 itwill 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 .
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 !
The text was updated successfully, but these errors were encountered: