Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the operator #:: mean?

Tags:

scala

I realise this is probably a simple question but what is '#::' achieving in below line of code. Is it a special variance of cons ?

def from(n: Int): Stream[Int] = n #:: from(n + 1)
like image 595
user701254 Avatar asked Nov 15 '12 22:11

user701254


1 Answers

This operator is used to construct streams as opposed to lists. Consider the same code snippet with simple cons:

def from(n: Int): List[Int] = n :: from(n + 1)

running this method will result in StackOverflowError. But with Stream[Int] tail is evaluated lazily only when it's needed (and already computed values are remembered).

like image 126
Tomasz Nurkiewicz Avatar answered Oct 17 '22 01:10

Tomasz Nurkiewicz