2

Is there a more elegant method than going through a Seq?

val origin = Set("a", "b")
val intermediate = origin.toSeq
val stream = Stream.from(0).map { index =>
    intermediate(index % intermediate.size)
}.toIterator

stream.next() // => "a"
stream.next() // => "b"
stream.next() // => "a"

I am specifically targeting Scala 2.9.

Seth Tisue
  • 29,985
  • 11
  • 82
  • 149
François Beausoleil
  • 16,265
  • 11
  • 67
  • 90

1 Answers1

1

This question is very similar to this one.

The easiest way is to use recursive definition:

val stream: Stream[String] = origin.toStream #::: stream

#::: is stream concatenation operator. It is equivalent to:

val stream: Stream[String] = origin.toStream.append(stream)
Community
  • 1
  • 1
krcz
  • 361
  • 1
  • 6