Workiva/go-datastructures

queue.TakeUntil - does not pop final matching element

mikeatlas opened this issue · 3 comments

This isn't quite the behavior I expected the queue.TakeUntil(...) function to behave. Here's a quick non-test-harness way I wrote a snippit to check my understanding of how this feature works:

    q := queue.New(10)
    q.Put(`a`, `b`, `c`)
    takeItems, _ := q.TakeUntil(func(item interface{}) bool {
        return item != `c`
    })

    restItems, _ := q.Get(3)

    fmt.Sprintf("TakeUntil Test. takeItems: %v, restItems: %v",
        takeItems, restItems)

This prints:
TakeUntil Test. takeItems: [a b], restItems: [b c]

I would have assumed that restItems would be either [c] (mutable call) or [a b c] (immutable call) but the result of [b c] is unexpected because the function both returned [a b] (looks like an immutable return value) but when I see [b c] it looks as if the queue was indeed mutated, but I would have expected in that case for the queue to have been [c]. Am I not understanding something, or is this a bug?

Anyways, the point was that I was hoping TakeUntil could serve as a Peek (a common operation on many queue data structure implementations) but the behavior I'm seeing doesn't indicate that it's usable nor that TakeUntil behaves as I expected from the documentation.

Closing since for now since the maintainers didn't comment on this behavior being correctly defined as expected or not. Will reopen if others see a reason.

@mikeatlas That is unexpected. I would expect get to return [c]. Sorry for the slow response, been very busy lately. I'll take a look when I get a chance.