If one is unaware of the complexity of the .length() method, one might use it to find out if a dequeue is possible.
If one is unaware of the complexity of the .length() method, one might use it to find out if a dequeue is possible.
Lots of data structures keep a size field and access it in O(1), so it's not completely unreasonable to expect erlang:length to be O(1) without making sure.
At least that's how I understood the context of the problem, I may be wrong of course.
erlang's queue's pop ("out") is amortized O(1), worst case O(N) just like every other immutable double headed queue. Here's the source code:
https://github.com/erlang/otp/blob/172e812c491680fbb175f56f7...
The correct implementation for checking if a list is empty would have been a pattern match, something akin to
case List of [] -> empty; [_ | _] -> not_empty end.
Or, you know. Reading the API docs and using queue:is_empty(Queue), which is O(1) instead of O(n) (which queue:len(Queue) is -explicitly stated to be-).