If you are interested to learn about the scala Tuples
In Scala, a Stream is a lazily evaluated, immutable data structure that represents a sequence of elements. It is similar to a List, but its elements are computed only when they are accessed. This can be useful when dealing with large or infinite sequences of data.
How Stream works in Scala?
As we have seen that scala stream is lazy list, they are like list on scala with only difference is that their element are computed lazily not at once. Only the element’s which we request are computed thus increases the performance of our program. But stream in scala is fast.
Example:
varmystm = 10 #:: 20 #:: 30 #:: Stream.empty
In this the head of the stream is 10 and 20, 30 are the tails of the stream. So in output also the head will be printed but not the tail because they have not been computed yet. In this we have 3 elements in the stream.
Here’s an example of creating a stream in Scala:
scalaCopy codeval myStream = Stream(1, 2, 3, 4, 5)
In this example, we create a Stream containing the integers 1 through 5. Note that this Stream is immutable and cannot be modified.
To access the elements of a Stream, we can use the head
and tail
methods. The head
method returns the first element of the Stream, while the tail
method returns a new Stream containing all elements except the first one.
Here’s an example of using head
and tail
on a Stream:
scalaCopy codeval myStream = Stream(1, 2, 3, 4, 5)
println(myStream.head) // prints 1
println(myStream.tail.head) // prints 2
In this example, we access the first and second elements of the Stream using the head
and tail
methods.
One important feature of Streams is their laziness. This means that elements are computed only when they are accessed, which can improve performance when dealing with large or infinite sequences of data. For example, consider the following Stream of Fibonacci numbers:
scalaCopy codelazy val fib: Stream[Int] = Stream.cons(0, Stream.cons(1, fib.zip(fib.tail).map { case (a, b) => a + b }))
In this example, we define a Stream fib
that contains the Fibonacci sequence. Note that we use the lazy val
keyword to create a lazily evaluated Stream. We also use the Stream.cons
method to construct the Stream and the zip
and map
methods to compute the Fibonacci sequence.
Examples of Scala Stream
- Generate a Stream of even numbers:
scalaCopy codeval evenNumbers = Stream.from(2).filter(_ % 2 == 0)
In this example, we use the Stream.from
method to create a Stream of integers starting from 2, and then use the filter
method to filter out all odd numbers.
- Generate a Stream of Fibonacci numbers:
scalaCopy code<code>lazy val fib: Stream[Int] = Stream.cons(0, Stream.cons(1, fib.zip(fib.tail).map { case (a, b) => a + b })) </code>
In this example, we define a lazily evaluated Stream fib
that contains the Fibonacci sequence. We use the Stream.cons
method to construct the Stream and the zip
and map
methods to compute the Fibonacci sequence.
- Generate a Stream of prime numbers:
scalaCopy code<code>def primes(numbers: Stream[Int]): Stream[Int] = { Stream.cons(numbers.head, primes(numbers.tail.filter(_ % numbers.head != 0))) } val primeNumbers = primes(Stream.from(2)) </code>
4. Applying Predefined Methods
In the following example, we have used some predefined methods like toStream, which is used to iterate stream elements.
import scala.collection.immutable._ object MainObject{ def main(args:Array[String]){ var stream = 100 #:: 200 #:: 85 #:: Stream.empty println(stream) var stream2 = (1 to 10).toStream println(stream2) var firstElement = stream2.head println(firstElement) println(stream2.take(10)) println(stream.map{_*2}) } }
Output:
Stream(100, ?)
Stream(1, ?)
1
Stream(1, ?)
Stream(200, ?)
In this example, we define a recursive function primes
that generates a Stream of prime numbers. We use the Stream.cons
method to construct the Stream and the filter
method to remove all numbers that are divisible by the current prime number.