You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
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.
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
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.
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 withfromList
. ButfromList
needs a list of tuples.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
:or more succinctly
It would be nice to have a more direct function for doing this:
This could be generalized to work with any
Foldable
:Similarly, the existing
fromList
could use a more general counterpart:The text was updated successfully, but these errors were encountered: