Scala 2.8 Collections API -- Immutable Queues
Immutable Queues
A Queue is just like a stack except that it is first-in-first-out rather than last-in-first-out.
Here's how you can create an empty immutable queue:
| scala> val empty = scala.collection.immutable.Queue[Int]() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| empty: scala.collection.immutable.Queue[Int] = Queue() |
| scala> val has1 = empty.enqueue(1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| has1: scala.collection.immutable.Queue[Int] = Queue(1) |
| scala> val has123 = has1.enqueue(List(2, 3)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| has123: scala.collection.immutable.Queue[Int] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| = Queue(1, 2, 3) |
| scala> val (element, has23) = has123.dequeue | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| element: Int = 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| has23: scala.collection.immutable.Queue[Int] = Queue(2, 3) |
Read full article from Scala 2.8 Collections API -- Immutable Queues
No comments:
Post a Comment