How do I map over a Map?

Let’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?

3 Likes

Hi @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)]
6 Likes

Welcome to the forum @DFeichtinger!