Skip to content

Instantly share code, notes, and snippets.

@tomasherman
Created August 23, 2011 10:51
Show Gist options
  • Save tomasherman/1164840 to your computer and use it in GitHub Desktop.
Save tomasherman/1164840 to your computer and use it in GitHub Desktop.
InfiniteList
object InfiList{
def cons[A](x:A,f:(A)=>A) = {
new InfiList(x,f)
}
}
class InfiList[A](val init:A,gen: A=>A) {
var c:A = init
def next() = {
c = gen(c)
c
}
}
>> defined module InfiList
>> defined class InfiList
def g(x:Int) = x+1
>> g: (x: Int)Int
val z = InfiList.cons(0,g)
>> z: InfiList[Int] = InfiList@be9fc1
z.next
>> res0: Int = 1
z.next
>> res1: Int = 2
z.next
>> res2: Int = 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment