-
I have a a list of strings. I want to iterate it to get an Initially, I had this which returns what I want (first error or full list if successful) but still iterates over the entire list when there's an error: public EitherAsync<ErrorModel, Seq<DataModel>> GetManyAsync(Seq<string> guids)
{
EitherAsync<ErrorModel, Seq<DataModel>> seed = new Seq<DataModel>();
return guids
.Aggregate(seed, (either, guid) => either
.Bind(results => GetDataAsync(guid)
.Map(result => results.Add(result))));
}
public EitherAsync<ServiceError, DataModel> GetDataAsync(string guid) { ... } I then tried to use this library's FoldWhile/FoldUntil to stop when the Either goes Left, but I end up having to use public EitherAsync<ErrorModel, Seq<DataModel>> GetManyAsync(Seq<string> guids)
{
EitherAsync<ErrorModel, Seq<DataModel>> state = new Seq<DataModel>();
return guids
.FoldWhile(state, (either, guid) => either
.Bind(results => GetDataAsync(guid)
.Map(result => results.Add(result))), x => x.IsRight.Result);
}
public EitherAsync<ServiceError, DataModel> GetDataAsync(string guid) { ... } Is there some other way to do this that I am missing? The original |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You can use I haven't got a debugger to hand, but try this: guids.SequenceSerial(GetDataAsync); It will only return a sequence if all items succeed, otherwise you get the first error. There's also |
Beta Was this translation helpful? Give feedback.
You can use
SequenceSerial
andTraverseSerial
to turn a sequence of asynchronous actions into a asynchronous action of sequence.I haven't got a debugger to hand, but try this:
It will only return a sequence if all items succeed, otherwise you get the first error. There's also
SequenceParallel
andTraverseParallel
. The rules are the same, but the error you get back might not be the first sequentially.