You can simplify the loop with the method, but you can think about it:
static bool DequeuePairIf<T>( Func<T, T, bool> predicate, Queue<T> queueA, Queue<T> queueB) { if (queueA.Count != 0 && queueB.Count != 0 && predicate(queueA.Peek(), queueB.Peek()) ) { queueA.Dequeue(); queueB.Dequeue(); return true; } return false; }
Then your loop will look like this:
while (DequeuePairIf(AreSimilar, queueA, queueB)) { }
But I doubt whether this refactoring of readability will help or harm. Firstly, it is significantly more than the source code. On the other hand, smaller code is not always readable.
(I removed assert to simplify the logic here. If you still need to assert, you will have to save the results of Peek() calls, as in the source code.)
source share