EnumerableExt.cs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using System.Collections.Generic;
  3. namespace ZenFulcrum.EmbeddedBrowser.Promises
  4. {
  5. /// <summary>
  6. /// General extensions to LINQ.
  7. /// </summary>
  8. public static class EnumerableExt
  9. {
  10. public static IEnumerable<T> Empty<T>()
  11. {
  12. return new T[0];
  13. }
  14. public static IEnumerable<T> LazyEach<T>(this IEnumerable<T> source, Action<T> fn)
  15. {
  16. foreach (var item in source)
  17. {
  18. fn.Invoke(item);
  19. yield return item;
  20. }
  21. }
  22. public static void Each<T>(this IEnumerable<T> source, Action<T> fn)
  23. {
  24. foreach (var item in source)
  25. {
  26. fn.Invoke(item);
  27. }
  28. }
  29. public static void Each<T>(this IEnumerable<T> source, Action<T, int> fn)
  30. {
  31. int index = 0;
  32. foreach (T item in source)
  33. {
  34. fn.Invoke(item, index);
  35. index++;
  36. }
  37. }
  38. }
  39. }