fork download
  1. namespace BakaIterator
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6.  
  7. public class BakaIterator<T>
  8. {
  9. public T Current => _items[_index];
  10. public bool HasNext => _index + 1 < _items.Count - 1;
  11. public int Position => _index;
  12. public int Count => _items.Count;
  13. public BakaIterator(IList<T> items)
  14. {
  15. if (items is null)
  16. {
  17. throw new ArgumentNullException(nameof(items));
  18. }
  19. if (items.Count == 0)
  20. {
  21. throw new ArgumentException("Items must contain at least 1 element", nameof(items));
  22. }
  23. _items = items;
  24. _index = 0;
  25. }
  26.  
  27. private readonly IList<T> _items;
  28. private int _index;
  29. public BakaIterator<T> MoveForward(int steps = 1)
  30. {
  31. if (steps < 0)
  32. {
  33. throw new ArgumentException("Invalid steps count. Must be 0 or positiv integer");
  34. }
  35. if (_index + steps > _items.Count - 1)
  36. {
  37. throw new IndexOutOfRangeException("To many steps");
  38. }
  39. _index += steps;
  40. return this;
  41. }
  42. public BakaIterator<T> MoveBackwards(int steps = 1)
  43. {
  44. if (steps < 0)
  45. {
  46. throw new ArgumentException("Invalid steps count. Must be 0 or positiv integer");
  47. }
  48. if (_index - steps < 0)
  49. {
  50. throw new IndexOutOfRangeException("To many steps");
  51. }
  52. _index -= steps;
  53. return this;
  54. }
  55. public BakaIterator<T> Reset()
  56. {
  57. _index = 0;
  58. return this;
  59. }
  60.  
  61. public BakaIterator<T> First(Func<T, bool> predicate, bool throwIfNotFound = true)
  62. {
  63. do
  64. {
  65. if (predicate(Current))//Мы уже получили этот элемент
  66. {
  67. return this;
  68. }
  69.  
  70. if (HasNext)
  71. {
  72. MoveForward();
  73. }
  74. } while (HasNext);
  75.  
  76. if (throwIfNotFound)
  77. {
  78. throw new Exception("Item not found");
  79. }
  80. return this;
  81. }
  82. //Для случая если нам нужен не просто первый, но первый который не текущий
  83. public BakaIterator<T> FirstNext(Func<T, bool> predicate, bool throwIfNotFound = true)
  84. {
  85. return First((x) => predicate(x) && Current.Equals(x) == false, throwIfNotFound);
  86. }
  87.  
  88. public BakaIterator<T> ThrowIf(Func<T, bool> predicate)
  89. {
  90. if (predicate(Current))
  91. {
  92. throw new Exception($"Item {Current} is ");
  93. }
  94. return this;
  95. }
  96. public BakaIterator<T> ThrowIfNot(Func<T, bool> predicate)
  97. {
  98. if (!predicate(Current))
  99. {
  100. throw new Exception("Current item is not");
  101. }
  102. return this;
  103. }
  104.  
  105. public BakaIterator<T> ForEachFromCurrent(Action<T> action)
  106. {
  107. foreach(var i in _items.Skip(_index + 1))
  108. {
  109. action(i);
  110. }
  111. return this;
  112. }
  113. public BakaIterator<T> ForEach(Action<T> action)
  114. {
  115. foreach (var i in _items)
  116. {
  117. action(i);
  118. }
  119. return this;
  120. }
  121. }
  122. }
  123.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cs(15,23): error CS1644: Feature `pattern matching' cannot be used because it is not part of the C# 7.0 language specification
Compilation failed: 1 error(s), 0 warnings
stdout
Standard output is empty