Monday, March 10, 2014

Streams



Streams and Scala

TEST HERE!!!!
import scala.Stream


Generator

import scala.util.Random

def randoms() : List[Int]= Random.nextInt :: randoms() //() (result)
randoms().take(10).toList
The problem is that function never ends. We get stack overflow quickly. Once we call randoms() scala will try to construct whole List. If constructed sequence is very large or never ends tail of list should be constructed lazily (as late as possible). Solution: replace usages of sequence (e.g. List) with Stream and replace append
::
operator with stream append
#::
List(-49551798, 853356364, -696905074, 841723248, -1015255896, 1889203648, -695376165, -1787417339, -1709151460, -969124661)
#::
operator is equivalent to cons function


Table of contents...

No comments:

Post a Comment