An infinite stream:
val ones: Stream[Int] = Stream.cons(1, ones)
How is it possible for a value to be used in its own declaration? It seems this should produce a compiler error, yet it works.
A variable is a quantity that may change within the context of a mathematical problem or experiment. Typically, we use a single letter to represent a variable. The letters x, y, and z are common generic symbols used for variables.
Variables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information.
There are three main variables: independent variable, dependent variable and controlled variables. Example: a car going down different surfaces.
Noun. Plural for an element, feature, or factor that is liable to vary or change. factors. parameters. consideration.
It's not always a recursive definition. This actually works and produces 1:
val a : Int = a + 1
println(a)
variable a
is created when you type val a: Int
, so you can use it in the definition. Int
is initialized to 0 by default. A class will be null.
As @Chris pointed out, Stream accepts => Stream[A]
so a bit another rules are applied, but I wanted to explain general case. The idea is still the same, but the variable is passed by-name, so this makes the computation recursive. Given that it is passed by name, it is executed lazily. Stream computes each element one-by-one, so it calls ones
each time it needs next element, resulting in the same element being produces once again. This works:
val ones: Stream[Int] = Stream.cons(1, ones)
println((ones take 10).toList) // List(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
Though you can make infinite stream easier: Stream.continually(1)
Update As @SethTisue pointed out in the comments Stream.continually
and Stream.cons
are two completely different approaches, with very different results, because cons
takes A
when continually
takes =>A
, which means that continually
recomputes each time the element and stores it in the memory, when cons
can avoid storing it n times unless you convert it to the other structure like List
. You should use continually
only if you need to generate different values. See @SethTisue comment for details and examples.
But notice that you are required to specify the type, the same as with recursive functions.
And you can make the first example recursive:
lazy val b: Int = b + 1
println(b)
This will stackoverflow.
Look at the signature of Stream.cons.apply:
apply[A](hd: A, tl: ⇒ Stream[A]): Cons[A]
The ⇒
on the second parameter indicates that it has call-by-name semantics. Therefore your expression Stream.cons(1, ones)
is not strictly evaluated; the argument ones
does not need to be computed prior to being passed as an argument for tl
.
The reason this does not produce a compiler error is because both Stream.cons
and Cons
are non-strict and lazily evaluate their second parameter.
ones
can be used in it's own definition because the object cons has an apply method defined like this:
/** A stream consisting of a given first element and remaining elements
* @param hd The first element of the result stream
* @param tl The remaining elements of the result stream
*/
def apply[A](hd: A, tl: => Stream[A]) = new Cons(hd, tl)
And Cons is defined like this:
final class Cons[+A](hd: A, tl: => Stream[A]) extends Stream[A]
Notice that it's second parameter tl
is passed by name (=> Stream[A]
) rather than by value. In other words, the parameter tl
is not evaluated until it is used in the function.
One advantage to using this technique is that you can compose complex expressions that may be only partially evaluated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With