How do I map over a Map?
- DFeichtingerOP 3
Let’s say I have a
Next.Mapsuch asMap Party Intand I want to apply a function to the values in the map, for example,* 2. How do I do that? - cocreature 6
Hi @DFeichtinger, welcome to the forum!
Map(fromDA.Next.Map) is an instance of theFunctortypeclass (technicallyMap kis an instance ofFunctornotMapbut let’s ignore that here).Functorprovides a methodfmap : Functor f => (a -> b) -> f a -> f bor specialized to
Mapfmap : (a -> b) -> Map k a -> Map k bThat allows you to write something like
fmap (* 2) (DA.Next.Map.fromList [("x", 1), ("y", 2)]) === DA.Next.Map.fromList [("x", 2), ("y", 4)] - andreolf
Welcome to the forum @DFeichtinger!