fix: trywhere

This commit is contained in:
kostas 2024-06-05 09:57:09 +02:00
parent f70e3fe180
commit 493b76e0ed
1 changed files with 49 additions and 0 deletions

View File

@ -0,0 +1,49 @@
namespace InnovEnergy.App.SchneiderDriver;
public static class Utils
{
public static IEnumerable<T> TryWhere<T>(this IEnumerable<T> src, Func<T, Boolean> predicate)
{
foreach (var e in src)
{
var ok = false;
try
{
ok = predicate(e);
}
catch
{
// ignored
}
if (ok)
yield return e;
}
}
public static IEnumerable<R> TrySelect<T,R>(this IEnumerable<T> src, Func<T, R> map)
{
foreach (var e in src)
{
var ok = false;
var result = default(R);
try
{
result = map(e);
ok = true;
}
catch
{
// ignored
}
if (ok)
yield return result!;
}
}
}