Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static IEnumerable<T> Peek<T>(this IEnumerable<T> source, Action<T> action)
- {
- if (source == null) throw new ArgumentNullException(nameof(source));
- if (action == null) throw new ArgumentNullException(nameof(action));
- return _(); IEnumerable<T> _()
- {
- foreach (var element in source)
- {
- action(element);
- yield return element;
- }
- }
- }
- public static class MyExtensions
- {
- public static IEnumerable<TResult> TrySelect<TSource, TResult, TException>(this IEnumerable<TSource> source,
- Func<TSource, TResult> selector,
- Action<TSource, TException> exceptionAction)
- where TException : Exception
- {
- foreach (var s in source)
- {
- TResult result = default(TResult);
- bool success = false;
- try
- {
- result = selector(s);
- success = true;
- }
- catch (TException ex)
- {
- exceptionAction(s,ex);
- }
- if (success)
- {
- // n.b. can't yield return inside a try block
- yield return result;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement