0

I want an infinite queue that is backed by an infinite stream in Scala. This feels simple enough but I just can't find how to do this.

I.e.

Stream.from(0).toQueue // Except that there is no method "toQueue"
BasilTomato
  • 1,071
  • 1
  • 8
  • 14

1 Answers1

3

A queue can't be infinite because it requires access from both ends: dequeue operation takes an element from the front of the queue, but enqueue puts an element to the back of the queue. For infinite sequences it does not make sense. Infinite stack would make sense (and in fact that's what single-linked lists and streams essentially are), but Stack interface is deprecated in favor of using implementations directly.

What you need an infinite queue for? If you only want to consume the stream sequentially, you can use native stream methods or destructuring or just generic collection methods like foreach.

Vladimir Matveev
  • 120,085
  • 34
  • 287
  • 296
  • Oh I see.. It's to feed an infinite stream to a library that expects a Queue (Spark's StreamingContext#queueStream). It looks like a quasi bug then as I don't see how Spark needs anything other than the ability to consume the queue. – BasilTomato Jan 02 '15 at 12:08