Skip to content
CCPEDIAby Unity Nodes
App Development

How do I map over a Map?

3 posts361 views9 likesLast activity Jul 2020

  1. DFeichtingerOP 3

    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?

  2. cocreature 6

    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)]
    
  3. andreolf

    Welcome to the forum @DFeichtinger!