Language-ext: Collect Valids only of IEnumerable<Validation<Error,T>>

Created on 29 Apr 2020  路  5Comments  路  Source: louthy/language-ext

I cannot seem to find an equivalent of .Somes (for IEnumerable<Option<T>>) for IEnumerable<Validation<Error,T>> where I end up with an IEnumerable<T> (using only the SUCCs and disregarding all the errors of the FAILs).

Is it available? Or is it a conscious choice to leave this out, since there isn't always a clear usecase for this?

All 5 comments

It doesn't currently exist, not for any conscious reason, I will add it. There is functionality existing to do it, but it's a little _generic heavy_:

```c#
var vs = new[] {Success(1), Success(2), Success(3)};

var xs = Choice.rights, Validation, Seq, int>(vs);

```

@louthy Instead of the example you give for a current workaround, you could also do this right?

var vs = new[] {Success<string, int>(1), Success<string, int>(2), Success<string, int>(3)};
var xs = vs.Bind((v) => v.SuccessAsEnumerable());

Or am I missing something?

You could, but personally, I'd just write an extension method whilst you're waiting for the Successes method to exist ;)

```c#
public static class ValidationExt
{
public static IEnumerable Successes(this IEnumerable> vs) =>
Choice.rights,
Validation,
Seq, int>(vs);
}

Or, if you want the most optimal:
```c#
    public static class ValidationExt
    {
        public static IEnumerable<S> Successes<F, S>(this IEnumerable<Validation<F, S>> vs) 
        {
            foreach(var v in vs)
            {
                if(v.IsSuccess) yield return (S)v;
            }
        }
    }

The real code for this is here. I'd expect to deploy it to the beta in a day or so.

It's now in the latest beta, which I consider to be stable (it's staying as beta for a while as I'm looking to make this into version 4.0.0 of lang-ext.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jltrem picture jltrem  路  7Comments

khtan picture khtan  路  4Comments

TysonMN picture TysonMN  路  4Comments

andrevdm picture andrevdm  路  4Comments

TonyHernandezAtMS picture TonyHernandezAtMS  路  5Comments