How do I map over a Map?
App Development3 posts331 views9 likesLast activity Jul 2020
DF
DFeichtingerOP
Jul 2020Let’s say I have a Next.Map such as Map Party Int and I want to apply a function to the values in the map, for example, * 2. How do I do that?
CO
cocreature
Jul 2020Hi @DFeichtinger, welcome to the forum!
Map (from DA.Next.Map) is an instance of the Functor typeclass (technically Map k is an instance of Functor not Map but let’s ignore that here). Functor provides a method
fmap : Functor f => (a -> b) -> f a -> f b
or specialized to Map
fmap : (a -> b) -> Map k a -> Map k b
That allows you to write something like
fmap (* 2) (DA.Next.Map.fromList [("x", 1), ("y", 2)]) === DA.Next.Map.fromList [("x", 2), ("y", 4)]
AN
andreolf
Jul 2020Welcome to the forum @DFeichtinger!