Skip to content

Unfold for Maps and Easier Conversion from Foldable #338

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
andrewthad opened this issue Sep 8, 2016 · 2 comments
Open

Unfold for Maps and Easier Conversion from Foldable #338

andrewthad opened this issue Sep 8, 2016 · 2 comments

Comments

@andrewthad
Copy link

Let's say I have a list of things that I want to turn into a Map. Right now, my only option for doing that is with fromList. But fromList needs a list of tuples.

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

So, I would have to first map over my list, converting everything to a tuple with the key and value, and then I could call fromList:

fromList . map (\d -> (dogId d, dogName, d)) :: [Dog] -> Map Int Text

or more succinctly

fromList . map (dogId &&& dogName)

It would be nice to have a more direct function for doing this:

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

This could be generalized to work with any Foldable:

unfold :: (Ord k, Foldable f) => (a -> (k,v)) -> f a -> Map k v

Similarly, the existing fromList could use a more general counterpart:

fromPairs :: (Ord k, Foldable f) => f (k, a) -> Map k a
@treeowl
Copy link
Contributor

treeowl commented Sep 9, 2016

I like some of this. I don't like the name unfold, which generally suggests
building a structure from a seed. One option for fromPairs might be to
generalize fromList to any Foldable; I haven't looked into the efficiency
implications, but I don't think we rely on having an actual list as much
as we do in Data.Sequence.

On Sep 8, 2016 8:44 AM, "Andrew Martin" [email protected] wrote:

Let's say I have a list of things that I want to turn into a Map. Right
now, my only option for doing that is with fromList. But fromList needs a
list of tuples.

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

So, I would have to first map over my list, converting everything to a
tuple with the key and value, and then I could call fromList:

fromList . map (\d -> (dogId d, dogName, d)) :: [Dog] -> Map Int Text

or more succinctly

fromList . map (dogId &&& dogName)

It would be nice to have a more direct function for doing this:

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

This could be generalized to work with any Foldable:

unfold :: (Ord k, Foldable f) => (a -> (k,v)) -> f a -> Map k v

Similarly, the existing fromList could use a more general counterpart:

fromPairs :: (Ord k, Foldable f) => f (k, a) -> Map k a


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#338, or mute the thread
https://github.com/notifications/unsubscribe-auth/ABzi_SoV9JxCPKxQRscfXLqo9MY5E4Uhks5qoANIgaJpZM4J38vo
.

@andrewthad
Copy link
Author

Thanks. You're right that unfold is a bad name. I just took a look at fromList and it's way more optimized than I assumed it was. I thought it would just be foldr (\(k,v) m -> Map.insert k v m) Map.empty or something like that. I think that's the only way you could define it to work with all Foldables.

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