Innovenergy_trunk/csharp/App/EmuMeterDriver/Utils.cs

49 lines
957 B
C#
Raw Normal View History

namespace InnovEnergy.App.EmuMeterDriver;
2023-02-16 12:57:06 +00:00
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!;
}
}
}