Skip to content

Instantly share code, notes, and snippets.

@ashee
Created November 1, 2012 20:41
Show Gist options
  • Save ashee/3996385 to your computer and use it in GitHub Desktop.
Save ashee/3996385 to your computer and use it in GitHub Desktop.
scala equivalent of haskell's cycle
// cycle will lazily cycle through values from the seq argument
def cycle[T](seq: Seq[T]) = Stream.continually(seq).flatten
scala> val c = cycle(List(1, 2, 3))
scala> c take 10 force
res24: scala.collection.immutable.Stream[Int] = Stream(1, 2, 3, 1, 2, 3, 1, 2, 3, 1)
@PeterPerhac
Copy link

I like this, it's very neat on half a line. There's one danger lurking though.
When you try to cycle over an empty sequence, you'll melt your cpu. Just try cycle(Nil) take 10 force with your version of cycle.

I would suggest wrapping the Stream.continually... in a block and guard it with an assertion first

def cycle[T](seq: Seq[T]): Stream[T] = {
  assert(seq.nonEmpty, "Cannot cycle over an empty sequence!")
  Stream.continually(seq).flatten
}

val fruit = List("apple", "banana", "orange", "mango")

//both yield same output
(cycle(fruit) take 10).force
(Stream.continually(fruit).flatten take 10).force

//this works
(cycle(Nil) take 10).force
//this will melt your cpu
(Stream.continually(Seq()).flatten take 10).force

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment