I would like to split collection on item, which matches specific condition. I can do that using TakeWhile and SkipWhile, which is pretty easy to understand:
public static bool IsSeparator(int value) => value != 3;
var collection = new [] { 1, 2, 3, 4, 5 };
var part1 = collection.TakeWhile(IsSeparator);
var part2 = collection.SkipWhile(IsSeparator);
But this would iterate from start of collection twice and if IsSeparator takes long it might be performance issue.
Faster way would be to use something like:
var part1 = new List<int>();
var index = 0;
for (var max = collection.Length; index < max; ++index) {
if (IsSeparator(collection[i]))
part1.Add(collection[i]);
else
break;
}
var part2 = collection.Skip(index);
But that's really less more readable than first example.
So my question is: what would be the best solution to split collection on specific element?
What I though of combining those two above is:
var collection = new [] { 1, 2, 3, 4, 5 };
var part1 = collection.TakeWhile(IsSeparator).ToList();
var part2 = collection.Skip(part1.Count);
