<div dir="ltr"><div>As far as I know, the most general form of a function that allows traversing and filtering is:</div><div><br></div><div>    type Filter s t a b = foall f. Applicative f => (a -> f (Maybe b)) -> s -> f t</div><div><br></div><div>In my witherable[0] package, I defined `Witherable` as a subclass of `Traversable` to provide such operation for various containers.</div><div><br></div><div><pre style="color:rgb(0,0,0)"><span class="" style="color:blue">class</span> <span class="">T</span><span class="">.</span><span class="">Traversable</span> <span class="">t</span> <span class="" style="color:red">=></span> <span class="">Witherable</span> <span class="">t</span> <span class="" style="color:blue">where</span>
<a name="line-131"></a>
<a name="line-132"></a>  <span class="">wither</span> <span class="" style="color:red">::</span> <span class="">Applicative</span> <span class="">f</span> <span class="" style="color:red">=></span> <span class="" style="color:red">(</span><span class="">a</span> <span class="" style="color:red">-></span> <span class="">f</span> <span class="" style="color:red">(</span><span class="">Maybe</span> <span class="">b</span><span class="" style="color:red">)</span><span class="" style="color:red">)</span> <span class="" style="color:red">-></span> <span class="">t</span> <span class="">a</span> <span class="" style="color:red">-></span> <span class="">f</span> <span class="" style="color:red">(</span><span class="">t</span> <span class="">b</span><span class="" style="color:red">)</span></pre></div><div>   ...</div><div><br></div><div>However, the `wither` for `Map` is currently inefficient because it is defined in terms of `traverse` and `mapMaybe`, so it traverses the container twice. Efficient implementation.would have to use the hidden constructors.</div><div><br></div><div>I would like to propose adding `traverseMaybe` and `traverseMaybeWithKey` for `Data.Map`, `Data.IntMap`, and their strict variants (I'm suggesting more conservative name because wither might sound too unusual or poetic for a standard library. I like 'wither' though). A possible implementation would be like this:</div><div><br></div><div>traverseMaybeWithKey :: Applicative f => (k -> a -> f (Maybe b)) -> Map k a -> f (Map k b)</div><div>traverseMaybeWithKey _ Tip = pure Tip</div><div>traverseMaybeWithKey f (Bin _ kx x l r) = maybe merge (link kx)</div><div>  <$> f kx x</div><div>  <*> traverseMaybeWithKey f l</div><div>  <*> traverseMaybeWithKey f r</div><div><br></div><div>I think there is potential demand for this function as well as mapMaybe.</div><div><br></div><div>[0] <a href="http://hackage.haskell.org/package/witherable">http://hackage.haskell.org/package/witherable</a></div></div>