<div dir="ltr">+1, I'm a fan of the functionality that witherable provides. Would it be possible to provide benchmarks with and without using the hidden constructors? It might make the case more compelling.<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Mar 8, 2016 at 12:43 AM, Fumiaki Kinoshita <span dir="ltr"><<a href="mailto:fumiexcel@gmail.com" target="_blank">fumiexcel@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><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 style="color:blue">class</span> <span>T</span><span>.</span><span>Traversable</span> <span>t</span> <span style="color:red">=></span> <span>Witherable</span> <span>t</span> <span style="color:blue">where</span>
<a name="2095498801_line-131"></a>
<a name="2095498801_line-132"></a>  <span>wither</span> <span style="color:red">::</span> <span>Applicative</span> <span>f</span> <span style="color:red">=></span> <span style="color:red">(</span><span>a</span> <span style="color:red">-></span> <span>f</span> <span style="color:red">(</span><span>Maybe</span> <span>b</span><span style="color:red">)</span><span style="color:red">)</span> <span style="color:red">-></span> <span>t</span> <span>a</span> <span style="color:red">-></span> <span>f</span> <span style="color:red">(</span><span>t</span> <span>b</span><span 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" target="_blank">http://hackage.haskell.org/package/witherable</a></div></div>
<br>_______________________________________________<br>
Libraries mailing list<br>
<a href="mailto:Libraries@haskell.org">Libraries@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries</a><br>
<br></blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature">Love in Jesus Christ, John Alfred Nathanael Chee<br><a href="http://www.biblegateway.com/" target="_blank">http://www.biblegateway.com/</a><br><a href="http://web.cecs.pdx.edu/~chee/" target="_blank">http://web.cecs.pdx.edu/~chee/</a></div>
</div>