I need something to store a LIFO. No need for traversing and other functions except for push and pop.
I've found special class in scala collection for creating a stack. But it lacks Nil object in pattern matching and other handy scala idioms. Immutable lists suit well at first glance, they have cons for construction and for extraction and that is all needed from LIFO.
Is there any reason behind scala.collection.immutable.Stack existence? Why I should prefer to use it, what are use cases to show its benefits?
From the API documentation:
Note: This class exists only for historical reason and as an analogue of mutable stacks. Instead of an immutable stack you can just use a list.
And in a little more detail:
Immutable stacks are used rarely in Scala programs because their functionality is subsumed by lists: A
push
on an immutable stack is the same as a::
on a list and apop
on a stack is the same as atail
on a list.
So to answer your questions:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With