Language-ext: Evaluate IEnumerable of EitherAsync

Created on 16 Oct 2020  路  2Comments  路  Source: louthy/language-ext

How do you force the evaluation of an IEnumerable<EitherAsync<Error, Unit>>?

For context, lets assume:

var l = new List<string>().AsEnumerable()
  .Map(s => HttpPost(s)); // HttpPost returns EitherAsync<Error, Unit>
Task.WhenAll(l); // Compiler Error

Task.WhenAll() can take IEnumerable<Task> but not IEnumerable<EitherAsync<>>.

I was hoping to be able to access the task in the EitherAsync:

IEnumerable<EitherAsync<Error, Unit>>
.Map(ea => {
   var e = await ea;   // But can't await because its not a task
   return e.AsTask();
})
// OR
.Map(ea => {
   return ea.GetAsATask();   // But I can't find a method like this.
})

This seems to do it - but its pretty boiler plate. Thoughts?

IEnumerable<EitherAsync<Error, Unit>>
.Map(ea => 
  ea.MatchAsync(
      LeftAsync: e => Left(e).AsTask(),
      RightAsync: u => Right(u).AsTask()
  )
)

As a broader question - I've been finding that I'm using EitherAsync as a return type all over and I haven't found anything in LangExt to help trigger the evaluation. Are there any helper functions that help when evaluating larger sets of tasks that may do a bunch of IO?

Most helpful comment

I think this will work.

var l = new List<string>().AsEnumerable()
  .Map(s => HttpPost(s).ToEither());
Task.WhenAll(l);

All 2 comments

I think this will work.

var l = new List<string>().AsEnumerable()
  .Map(s => HttpPost(s).ToEither());
Task.WhenAll(l);

Oh nice, that did the trick. Thanks

.ToEither() - Converted from EitherAsync<> to Task<Either<>>

Was this page helpful?
0 / 5 - 0 ratings

Related issues

HistoricallyCorrect picture HistoricallyCorrect  路  6Comments

Zordid picture Zordid  路  5Comments

Kavignon picture Kavignon  路  7Comments

andrevdm picture andrevdm  路  4Comments

Malgefor picture Malgefor  路  4Comments