code_junkie

Existing LINQ extension method similar to Parallel.For?

Nov 12th, 2011
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3.  
  4. public delegate void Function<T>(T item);
  5. public delegate void Function(object item);
  6.  
  7. public static class EnumerableExtensions
  8. {
  9. public static void For(this IEnumerable enumerable, Function func)
  10. {
  11. foreach (object item in enumerable)
  12. {
  13. func(item);
  14. }
  15. }
  16.  
  17. public static void For<T>(this IEnumerable<T> enumerable, Function<T> func)
  18. {
  19. foreach (T item in enumerable)
  20. {
  21. func(item);
  22. }
  23. }
  24. }
  25.  
  26. static void Main(string[] args)
  27. {
  28. IEnumerable<int> list = new List<int>() { -5, 3, -2, 1, 2, -7 };
  29. IEnumerable<bool> isPositiveList = list.Select<int, bool>(i => i > 0);
  30.  
  31. foreach (bool isPositive in isPositiveList)
  32. {
  33. Console.WriteLine(isPositive);
  34. }
  35.  
  36. Console.ReadKey();
  37. }
  38.  
  39. var res = GetRandomNumbers(100).Take(10)
  40. .Do(x => Console.WriteLine("Source -> {0}", x))
  41. .Where(x => x % 2 == 0)
  42. .Do(x => Console.WriteLine("Where -> {0}", x))
  43. .OrderBy(x => x)
  44. .Do(x => Console.WriteLine("OrderBy -> {0}", x))
  45. .Select(x => x + 1)
  46. .Do(x => Console.WriteLine("Select -> {0}", x));
  47.  
  48. Source -> 96
  49. Where -> 96
  50. Source -> 25
  51. Source -> 8
  52. Where -> 8
  53. Source -> 79
  54. Source -> 25
  55. Source -> 3
  56. Source -> 36
  57. Where -> 36
  58. Source -> 51
  59. Source -> 53
  60. Source -> 81
  61. OrderBy -> 8
  62. Select -> 9
  63. 9
  64. OrderBy -> 36
  65. Select -> 37
  66. 37
  67. OrderBy -> 96
  68. Select -> 97
  69. 97
Add Comment
Please, Sign In to add comment