title: How do I get a handle on deep stacks of functors in Haskell?
body: <p>Now and then I find myself mapping over a deep stack of functors, e.g. a parser for some collection of optional values:</p>
  
  <pre><code>-- parse a rectangular block of characters to a map of
  -- coordinate to the character, or Nothing for whitespace
  parseRectangle :: Parser (Map (Int, Int) (Maybe Char))
  
  data Class = Letter | Digit | Other
  
  classify :: Char -&gt; Class
  
  parseClassifiedRectangle :: Parser (Map (Int, Int) (Maybe Class))
  parseClassifiedRectangle = fmap (fmap (fmap classify)) parseRectangle
  </code></pre>
  
  <p>What are some good ways around the nested <code>fmap</code>s? Oftentimes it's not as clear as here, and I end up adding <code>fmap</code>s until the code type checks. Simple code ends up as a mess of <code>fmap</code> boilerplate, where what I really want to express is "lift this function to the appropriate depth and apply it to the contained type".</p>
  
  <p>Some ideas, none of which I've found particularly satisfactory so far:</p>
  
  <ul>
  <li>define <code>fmap2 :: (Functor f, Functor g) =&gt; (a -&gt; b) -&gt; g (f a) -&gt; g (f b)</code> and friends</li>
  <li>define concrete helpers, like <code>mapMaybeMap :: (a -&gt; b) -&gt; Map k (Maybe a) -&gt; Map k (Maybe b)</code></li>
  <li>introduce <code>newtype</code> wrappers for the functor stack, and make those instances of <code>Functor</code>, like <code>newtype MaybeMapParser a = Parser (Map (Int, Int) (Maybe a))</code></li>
  </ul>
  
  <p>Do others run into this problem in large codebases? Is it even a problem? How do you deal with it?</p>