question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

lefts and rights helpers for Either

See original GitHub issue

I 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:closed
  • Created 8 years ago
  • Comments:22 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
Profpatschcommented, Oct 5, 2015

Haha, that’s super great!

if you’ve come from a Haskell background why didn’t you go straight to F# rather than C#?

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 …

1reaction
louthycommented, Oct 4, 2015

It wouldn’t be great, no. So the implementation I have done is:

    public static IEnumerable<L> Lefts<L, R>(this IEnumerable<Either<L, R>> self)
    {
        foreach (var item in self)
        {
            if (item.IsLeft)
            {
                yield return item.LeftValue;
            }
        }
    }

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 with foreach via the IEnumerator interface).

I have just done a check-in with updates based on your suggestions:

  • Rights and Lefts extension methods for IEnumerable<Either<L,R>> and IEnumerable<EitherUnsafe<L,R>>
  • rights and lefts static methods in Prelude
  • partition function that returns a tuple of lefts and rights
  • More variants of ifLeft and ifRight
  • Left variants of iter, forall, fold, exists, map, bind and filter

Note, the LINQ functions Where, Select and SelectMany will continue to be biased toward Right and will early-out when an Either 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found