2 ms·
I don’t mind it, to me it reads as “pop the top of the stack off”, just like you would in a mutable version. The fact that the popped item is discarded doesn’t
by dkersten 7y ago
I don’t mind it, to me it reads as “pop the top of the stack off”, just like you would in a mutable version. The fact that the popped item is discarded doesn’t change the operation.
As for traversing twice, at least in Clojure, the reason you have peek and last as two separate functions is that peek never traverses, but what it does depends on the type of data structure passed in (and pop works to match that), eg, for a vector, it’s the same result to last and O(1) because vectors are random access, but for lists, peek is the same as first and still O(1), while last is O(n). For lists, pop does the same as rest.
Why have peek/pop when there are other functions that do the same thing? Well, they are designed to be consistent with each other, so regardless of the data structures used, peek will always return the item that pop removes.