lefts and rights helpers for Either
See original GitHub issueI am direly missing these functions from Haskell (http://hackage.haskell.org/package/base-4.8.1.0/docs/src/Data.Either.html#lefts).
I’m not sure how to implement them here, since Haskell uses some kind of partial pattern match magic predicates.
I’m generally not too convinced by the asymmetry of Either. In particular the IfLeft/IfRight functions are preferring Right so much that there is practically no chance to extract a left value. Maybe I’m missing the obvious way of doing it.
The following is as far as I came; it doesn’t compile though, because lout
might not be instanced (as far as the compiler is concerned).
public static class EitherExtensions<L, R>
{
private static IEnumerable<L> Lefts(this IEnumerable<Either<L, R>> eis)
{
return eis.Filter(ei => ei.IsLeft).Map(ei =>
{
L lout;
ei.Match(
Right: r => { return; },
Left: l => { lout = l; });
return lout;
});
}
}
Issue Analytics
- State:
- Created 8 years ago
- Comments:22 (11 by maintainers)
Top Results From Across the Web
Either - Crocks
Right,. 22. compose(Left, err). 23. ) 24. . 25. // flow :: a -> Either ... into an Either as a Right ,...
Read more >Pattern matching and type safety in TypeScript
Algebraic data types: Either left or right; Pattern matching ... ofType is a helper function that creates a type definition for each variant ......
Read more >Data.Either - Hackage - Haskell.org
The Either type represents values with two possibilities: a value of type Either a b is either Left a or Right b ....
Read more >Left-wing authoritarians share key psychological traits with far ...
... whether they are on the far left or the far right — have ... appeal of authoritarianism may be relevant to help...
Read more >Left-Handed and Right-Handed Fastener Threads: Uses ...
Fastening: Screw threads appear on traditional fasteners such as nuts, bolts, and screws, and they also help connect threaded pipes, hoses, caps ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Haha, that’s super great!
I’m fixing bugs and improving a small C# codebase and originally just wanted a Maybe and an Either type because I loathe the null- and exception-passing style. Maybe it escalated a little …
It wouldn’t be great, no. So the implementation I have done is:
And similar for
Rights
. This imperative loop is the quickest way of yielding the results, but also it’s lazy (if you don’t know C# too well,yield
is a coroutines system which works in tandem withforeach
via theIEnumerator
interface).I have just done a check-in with updates based on your suggestions:
Rights
andLefts
extension methods forIEnumerable<Either<L,R>>
andIEnumerable<EitherUnsafe<L,R>>
rights
andlefts
static methods inPrelude
partition
function that returns a tuple of lefts and rightsifLeft
andifRight
iter
,forall
,fold
,exists
,map
,bind
andfilter
Note, the LINQ functions
Where
,Select
andSelectMany
will continue to be biased toward Right and will early-out when anEither
is in a Left or Bottom state.It’s getting a bit late here (2 am), so I will do a nuget release tomorrow with these updates. So if you want them sooner you’ll need to build from source.