Stas_P

Yet another filter implementation in c#

Aug 15th, 2014
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.49 KB | None | 0 0
  1. class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             var coll = new[] {1, 2, 4, 22, 23, 12};
  6.             var result = coll.Filter(i => i > 13);
  7.         }
  8.     }
  9.  
  10.     static class Ext
  11.     {
  12.         public static IEnumerable<T> AsEnumerable<T> (this T item)
  13.         {
  14.             yield return item;
  15.         }
  16.  
  17.         public static IEnumerable<T> Filter<T>(this IEnumerable<T> items, Predicate<T> predicate)
  18.         {
  19.             return items.Aggregate(Enumerable.Empty<T>(), (ints, arg) => predicate(arg) ? ints.Concat(arg.AsEnumerable()) : ints);
  20.         }
  21.     }
Advertisement
Add Comment
Please, Sign In to add comment