is there any reason why Kotlin doesn't provide fun...
# announcements
is there any reason why Kotlin doesn't provide functions for stack in the stdlib? like
stackOf("value 1", "value 2")
copied from the last time this was asked
Copy code
fun <E> MutableList<E>.push(element: E) = add(element)
fun <E> MutableList<E>.pop() = removeAt(lastIndex)
a stack by any other name
ArrayDeque is in 1.3.70
💯 5
and good ol’ LinkedList is always there if you’re on the JVM
Unless you need to put things in the middle, ArrayDeque is a better choice than LinkedList for both memory and performance
I have no problem using the standard Java
. Was just wondering if there was any reason for it not being in the stdlib as a Stack is kinda common to use. Thanks for the response and I'm looking forward to try out the
in the future 🙂
Even on the JVM I would avoid Stack. It even tells you to use Deque/ArrayDeque because the default behavior of synchronization (inherited from Vector) is usually unnecessary.
JB generally wants to keep the stdlib slim. The rules for that are not clear but that's what they say.
there is kind of a blind spot in most collections api's for tiny placeholders of 2-4 items. sometimes singly linked list nodes as stacks are elegant for very local scope and short lifespan. chunked http decoding comes to mind in my experience as a place with a need to sometimes stack one or two small fragments and the overhead of anything else in the critical section seems a tad too high. key feature of a singly linked stack node is total simplicity and immutability. something you can do with immutable stack nodes in the jvm is escape analysis instead of gc. define the node class in the local scope, use it, unwind whatever stack needed it, exit.