concept Map in category haskell

appears as: Map, Map
Get Programming with Haskell

This is an excerpt from Manning's book Get Programming with Haskell.

The trick is that Organ needs to be of type Ord to be a key for a Map.

Figure 18.4. The fromList function for building a Map

These are four different types, but they’re all parameterized by the same type: Int (Map is a special case, but the values are type Int). Now suppose you have a function with the following type signature:

27.3.4. Converting a Map of RobotParts to HTML

The partsDB Map has been useful, but it turns out all you need it for is converting RobotParts to HTML. If that’s the case, wouldn’t it make more sense to have an htmlPartsDB so you don’t have to continually convert? Because Map is an instance of Functor, you can do this easily.

Listing 27.14. Turning your partsDB into a Map of HTML rather than RobotParts

Now you can see that you’ve transformed your Map of RobotParts into a Map of Html snippets!

GHCi> Map.lookup 1 htmlPartsDB
Just "<h2>left arm</h2><p><h3>desc</h3>left ...

This example highlights just how powerful the simple interface that Functor provides can be. You can now trivially perform any transformation that you can on a RobotPart to an entire Map of robot parts.

The careful reader may have noticed something strange about Map being a Functor. Map’s kind is * -> * -> * because Map takes two type arguments, one for its keys and another for its values. Earlier we said that Functors must be of kind * -> *, so how can this be? If you look at the behavior of <$> on your partsDB, it becomes clear. Functor for Map is concerned only about the Map’s values and not its keys. When Map is made an instance of Functor, you’re concerned only about a single type variable, the one used for its values. So for the purposes of Map being a member of Functor, you treat it as being of kind * -> *. When we introduced kinds in lesson 18, they may have seemed overly abstract. But they can be useful for catching issues that arise with more advanced type classes.

sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest