In some code, I'm folding sort of tree into Either a b, where b is the "better" result and a the "worse" one - to apply transformations on specific cases, I use bimap, like bimap worse better subpartResult- in cases where constructor carries 2 subtrees, I would like to choose better path if both subresults support it or worse option otherwise, for both, using transformation from better to worse option. Does this maybe fit into some sort of pattern, like single-subtree case does?
Hmm, what are the differences between "subpart", "subresult", and "subtree"? What "constructor" are you talking about? "choose better" sounds like Alternative.
subpartResult :: Either a b, where a is "worse" and b is "better" - I would like to generalize bimap onWorse onBetter subpartResult for two Eithers joined using binary operator
I don't quite understand the business with trees, but the applicative instance for Either will turn two Rights into a Right, and any other combination into a Left. Is this what you're referring to with:
I would like to choose better path if both subresults support it or worse option otherwise, for both, using transformation from better to worse option.
i think i understand the problem a little bit better now. you've got some input of the form (e + a) × (e + b), and you need to mash it together into something else of the form e + c, and you're asking whether there's some general typeclass operation that Either happens to support which you can use to accomplish this.
i don't think there's anything readymade, the easiest way is probably to just write a concrete function:
op :: (a -> e) -> (b -> e) -> (e -> e -> e) -> (a -> b -> c) -> e + a -> e × b -> e + c
op = _
In some code, I'm folding sort of tree into
Either a b
, whereb
is the "better" result anda
the "worse" one - to apply transformations on specific cases, I usebimap
, likebimap worse better subpartResult
- in cases where constructor carries 2 subtrees, I would like to choose better path if both subresults support it or worse option otherwise, for both, using transformation from better to worse option. Does this maybe fit into some sort of pattern, like single-subtree case does?Hmm, what are the differences between "subpart", "subresult", and "subtree"? What "constructor" are you talking about? "choose better" sounds like
Alternative
.subpartResult :: Either a b
, wherea
is "worse" andb
is "better" - I would like to generalizebimap onWorse onBetter subpartResult
for twoEither
s joined using binary operatorHaving binary op for worse and better option
and ability to demote better into worse if one of them is worse
How does this look? Still not sure if I understand 100%
I don't quite understand the business with trees, but the applicative instance for
Either
will turn twoRight
s into aRight
, and any other combination into aLeft
. Is this what you're referring to with:?
i think i understand the problem a little bit better now. you've got some input of the form
(e + a) × (e + b)
, and you need to mash it together into something else of the forme + c
, and you're asking whether there's some general typeclass operation thatEither
happens to support which you can use to accomplish this.i don't think there's anything readymade, the easiest way is probably to just write a concrete function:
Yeah, that's basically what I did at the end :thumbs_up:
isn't that
Validation
?@Torsten Schmits my left side isn't used as
Monoid
- they're sorta equivalent, with right side being preferable whenever possible