You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How to iterate over a list of strings and execute a method for each string as long as an error occurs #1261
Suppose you have a method Fin<Unit> Calc(string input) and a list of strings that you want to call Calc on. Calc can fail and when that happens you want to stop iterating and return the error. I know you can do it with a foreach loop like that:
public Fin<Unit> Process(List<string> inputValues)
{
foreach (var input in inputValues)
{
var result = Calc(input);
if (result.IsFail)
return result;
}
return Unit.Default;
}
This approach is rather non-functional and not in the style of LanguageExt. Is there a more functional version that someone can recommend?
Activity
olegdunkan commentedon Oct 20, 2023
public Fin<IEnumerable<Unit>> Process(List<string> values) => values.Select(Calc).Traverse(x => x);