EnumerableMixin.cs 1013 B

1234567891011121314151617181920212223242526272829303132
  1. namespace Perspex.Controls
  2. {
  3. using System.Collections.Generic;
  4. public static class EnumerableMixin
  5. {
  6. public static IEnumerable<T> Shrink<T>(this IEnumerable<T> source, int left, int right)
  7. {
  8. int i = 0;
  9. var buffer = new Queue<T>(right + 1);
  10. foreach (T x in source)
  11. {
  12. if (i >= left) // Read past left many elements at the start
  13. {
  14. buffer.Enqueue(x);
  15. if (buffer.Count > right) // Build a buffer to drop right many elements at the end
  16. yield return buffer.Dequeue();
  17. }
  18. else i++;
  19. }
  20. }
  21. public static IEnumerable<T> WithoutLast<T>(this IEnumerable<T> source, int n = 1)
  22. {
  23. return source.Shrink(0, n);
  24. }
  25. public static IEnumerable<T> WithoutFirst<T>(this IEnumerable<T> source, int n = 1)
  26. {
  27. return source.Shrink(n, 0);
  28. }
  29. }
  30. }