Skip to content

Instantly share code, notes, and snippets.

@SethTisue
Created November 5, 2015 12:38
Show Gist options
  • Save SethTisue/ce598578874accba98c0 to your computer and use it in GitHub Desktop.
Save SethTisue/ce598578874accba98c0 to your computer and use it in GitHub Desktop.
Parade of ones vs. circle of ones
scala> def one = { println("hey!"); 1 }
one: Int
scala> val endlessParadeOfOnes = Stream.continually(one)
hey!
endlessParadeOfOnes: scala.collection.immutable.Stream[Int] = Stream(1, ?)
scala> endlessParadeOfOnes.take(5).sum
hey!
hey!
hey!
hey!
res0: Int = 5
scala> val circleOfOnes: Stream[Int] = one #:: circleOfOnes
hey!
circleOfOnes: Stream[Int] = Stream(1, ?)
scala> circleOfOnes.take(5).sum
res1: Int = 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment