<div dir="ltr"><div>How about `eitherId` for the `Either a a -> a`? I.e one usually does `either id id` to extract either value.</div><div><br></div><div>`fromEither` isn't a bad idea, but it's long.<br>  </div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Sep 23, 2020 at 3:49 PM Carter Schonwald <<a href="mailto:carter.schonwald@gmail.com">carter.schonwald@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="auto">Rob, </div><div dir="auto">1) your foldEither is way better and I love it. </div><div dir="auto"><br></div><div dir="auto">2) this is a way more pleasing Avenue of design for the tuple and either combinators ! Thank you for sharing this great little exposition. </div><div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Sep 23, 2020 at 8:20 AM Rob Rix via Libraries <<a href="mailto:libraries@haskell.org" target="_blank">libraries@haskell.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="overflow-wrap: break-word;"><div><span style="color:rgb(0,0,0)">TL;DR: maybe we can think of this as combining disjoint empty & non-empty cases of folds.</span></div><div><span style="color:rgb(0,0,0)"><br></span></div>Every time I’ve wanted to define something like this I’ve found myself dissatisfied because of this asymmetry.<div><br></div><div>Looking at it from another angle, I frequently encounter a vaguely similar situation with folds: given some collection which can be empty or non-, I want to fold over it with a function if non-empty, or return a default value if empty:</div><div><br></div><blockquote style="margin:0px 0px 0px 40px;border:medium none;padding:0px"><div>foldr1OrDefault :: Foldable t => (a -> a -> a) -> a -> t a -> a</div><div>foldr1OrDefault f z t</div><div>  | null t    = z</div><div>  | otherwise = foldr1 f t</div></blockquote><div><br></div><div>This is a bit like Control.Lens.Fold.foldBy in lens, except that the combining function f does not receive z at any point. Quite useful when you have a Semigroup but not a Monoid, or when you otherwise want to treat the empty case specially.</div><div><br></div><div>fromMaybe is a special case of this function, without the conceit of pretending that Maybe can hold multiple values. I’ve sometimes involved Maybe in this construction for just that reason when e.g. a is a Semigroup:</div><div><br></div><blockquote style="margin:0px 0px 0px 40px;border:medium none;padding:0px"><div>fold1Maybe :: (Foldable t, Semigroup a) => t a -> Maybe a</div><div>fold1Maybe = foldMap Just</div><div><br></div><div>fold1OrDefault :: (Foldable t, Semigroup a) => a -> t a -> a</div><div>fold1OrDefault z = fromMaybe z . foldMap Just</div></blockquote><div><br></div><div>(All of these names are intentionally bad because I don’t want to propose adding these functions anywhere just yet.)</div><div><br></div><div>For Either (and These for that matter), a full generalization also has to deal with the extra information in the null case, i.e. Either b a would need (b -> a) instead of a:</div><div><br></div><blockquote style="margin:0px 0px 0px 40px;border:medium none;padding:0px"><div>fromEither :: (b -> a) -> Either b a -> a</div><div>fromEither f = either f id</div></blockquote><div><br></div><div>I’m surprised about once every six months that this doesn’t exist (and then surprised again when consulting the types that fromLeft/fromRight aren’t either this *or* analogous to fromJust).</div><div><br></div><div>All of which is to say, I think I would prefer to have the generality of this fromEither to the a -> Either a a -> a version, but it’d be nice to figure out a way to see it as a fold, too. Maybe the Bifoldable1 instance for Either has something to contribute here?</div></div><div style="overflow-wrap: break-word;"><div><br></div><div>Rob</div><div><div><br><blockquote type="cite"><div>On Sep 17, 2020, at 1:13 PM, Philip Hazelden <<a href="mailto:philip.hazelden@gmail.com" target="_blank">philip.hazelden@gmail.com</a>> wrote:</div><br><div><div dir="ltr"><div dir="ltr">On Thu, Sep 17, 2020 at 5:59 PM Jon Purdy <<a href="mailto:evincarofautumn@gmail.com" target="_blank">evincarofautumn@gmail.com</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="auto">{ fromMaybe, fromLeft, fromRight, fromEither, fromThese, fromDynamic, … } extracts “the” value out of a { Maybe, These, Dynamic, … } with a suitable default if “it” isn’t present.</div></blockquote><div> </div><div>Hm. A weird thing about fromEither compared to (I think) all the rest of these, is that the type of the Either isn't fully general. That is, fromLeft and fromRight take an `Either a b`; fromThese takes a `These a b`. But the proposed `fromEither` takes an `Either a a`.</div><div><br></div><div>This maybe isn't a big deal. Just seemed worth noting.</div></div></div><br><br>_______________________________________________<br>Libraries mailing list<br><a href="mailto:Libraries@haskell.org" target="_blank">Libraries@haskell.org</a><br><a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries</a><br></div></blockquote></div><br></div></div>_______________________________________________<br><br>Libraries mailing list<br><br><a href="mailto:Libraries@haskell.org" target="_blank">Libraries@haskell.org</a><br><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></div>
_______________________________________________<br>
Libraries mailing list<br>
<a href="mailto:Libraries@haskell.org" target="_blank">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>
</blockquote></div><br clear="all"><br>-- <br><div dir="ltr" class="gmail_signature">Markus Läll<br></div>