<div dir="ltr">&gt; It made sense in my specific use case, but I think Gabriel&#39;s version is better as the general approach.<br><div><br></div><div>Can Gabriel derive his version from Michael&#39;s with:</div><div><br></div>

<div><div>instance Bounded (Maybe a) where</div><div>  maxBound = Nothing</div><div>  minBound = Nothing</div></div><div><br></div><div>The Bounded constraint seems right to me.  If Bounded doesn&#39;t apply to your datatype, maybe a Semigroup would be more appropriate than a Monoid?<br>

</div><div><br></div><div style>Thanks,</div><div>Greg<br></div><div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Dec 27, 2012 at 11:07 AM, Michael Snoyman <span dir="ltr">&lt;<a href="mailto:michael@snoyman.com" target="_blank">michael@snoyman.com</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote"><div class="im">On Thu, Dec 27, 2012 at 8:45 PM, Gabriel Gonzalez <span dir="ltr">&lt;<a href="mailto:gabriel439@gmail.com" target="_blank">gabriel439@gmail.com</a>&gt;</span> wrote:<br>



<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I don&#39;t know if this has been brought up before or not, but would it be possible to add the Maximum and Minimum monoids to Data.Monoid?  The following implementations extend the traditional semigroups using Maybe.<br>




<br>
******<br>
<br>
newtype Maximum a = Maximum { getMaximum :: Maybe a }<br>
<br>
instance (Ord a) =&gt; Monoid (Maximum a) where<br>
    mempty = Maximum Nothing<br>
<br>
    mappend (Maximum Nothing) m2 = m2<br>
    mappend m1 (Maximum Nothing) = m1<br>
    mappend (Maximum (Just a1)) (Maximum (Just a2)) = Maximum (Just (max a1 a2))<br>
<br>
newtype Minimum a = Minimum { getMinimum :: Maybe a }<br>
<br>
instance (Ord a) =&gt; Monoid (Minimum a) where<br>
    mempty = Minimum Nothing<br>
<br>
    mappend (Minimum Nothing) m2 = m2<br>
    mappend m1 (Minimum Nothing) = m1<br>
    mappend (Minimum (Just a1)) (Minimum (Just a2)) = Minimum (Just (min a1 a2))<br>
<br>
******<br>
<br>
These also give the correct behavior when folding empty structures by returning Nothing.<br>
<br>
The reason I&#39;m asking is that my `pipes` library uses `WriterT` to implement folds and having the above monoids lets me implement minimum and maximum folds elegantly.  I can always provide these monoids myself, but I feel like they belong in Data.Monoid.<br>




<br></blockquote><div><br></div></div><div>+1, I&#39;ve had to implement at least one of these in the past. In my case, I think I ended up doing it something like:</div><div><br></div><div>newtype Maximum a = Maximum { getMaximum :: a }</div>



<div>instance (Ord a, Bounded a) =&gt; Monoid (Maximum a) where</div><div>    mempty = Maximum minBound</div><div>    mappend (Maximum x) (Maximum y) = Maximum (max x y)</div><div><br></div><div>

It made sense in my specific use case, but I think Gabriel&#39;s version is better as the general approach.</div><span class="HOEnZb"><font color="#888888"><div><br></div><div>Michael</div></font></span></div></div></div>


<br>_______________________________________________<br>
Libraries mailing list<br>
<a href="mailto:Libraries@haskell.org">Libraries@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/libraries" target="_blank">http://www.haskell.org/mailman/listinfo/libraries</a><br>
<br></blockquote></div><br></div>