Recursive Streams and collection in Scala
Last Updated :
25 Apr, 2023
Recursive forms have their definition in terms of themselves like we have subfolders in folders which can further have subfolders. Recursive methods calling themselves is also a recursive form. Similar forms can be used to create a recursive stream. Example 1: Creating a lazy list
scala
// Scala program for recursive stream
// creating object
object Geeks
{
// Main method
def main(args: Array[String])
{
// second con can be recursive
lazy val geek = Stream.cons(1, Stream.cons(5, Stream.empty))
println (geek)
(geek take 4) foreach {
x => println(x)
}
}
}
Output :
Stream(1, ?)
1
5
In the code above the second con can be recursive. Example 2: Create a lazy list by placing a recursive call to method
scala
// Scala program for resursive stream
// creating object
object Geeks
{
// Main method
def main(args: Array[String])
{
// con can be recursive
def geek(n: Int): Stream[Int] = Stream.cons(n, geek(n+1))
// Creating lazy val
lazy val q = geek(5)
println(q)
}
}
Output :
Stream(5, ?)
Example 3: Create a lazy list by placing a recursive call to method in a brief and clear manner
scala
// Scala program for resursive stream
// creating object
object Geeks
{
// Main method
def main(args: Array[String])
{
// recursive stream
def geek(n: Int):Stream[Int] = n #:: geek(n+1)
// Lazy value
lazy val q = geek(5)
println(q)
}
}
Output :
Stream(5, ?)
Stream Collections
Stream collections in scala are very important as it allows need not to be explicitly lopped over. Declaratively things can be performed using functional combinators like map, filter, and flatMap. Streams are lazy lost collections. Example 4:
scala
// Scala program for stream collection
// creating object
object Geeks
{
// Main method
def main(args: Array[String])
{
// creating Stream
def geek(n: Int):Stream[Int] = n #:: geek(n+1)
lazy val g = geek(0)
println (g)
println (g.take(10).mkString(" ; "))
}
}
Output :
Stream(0, ?)
0 ; 1 ; 2 ; 3 ; 4 ; 5 ; 6 ; 7 ; 8 ; 9)
Example 5: Using filter
scala
// Scala program for filter list
// creating object
object Geeks
{
// Main method
def main(args: Array[String])
{
// Creating list
val geek = List(1, 2, 3, 4)
// Using filter
val abc = geek filter { x => x > 2 }
println(abc)
}
}
Output :
List(3, 4)
filter is called over the list geek. It takes function as an argument and returns a Boolean, which is a function predicate. Every element is run through the function and elements are filtered out where the function returns false. It results in a new list and the original "geek" list is remain persistent. Example 6: Using filter in a much concise manner
scala
// Scala program for collection
// creating object
object Geeks
{
// Main method
def main(args: Array[String])
{
// Creating list
val geek = List(1, 2, 3, 4)
// Filter list
val abc = geek filter { _ > 2 }
println(abc)
}
}
Output :
List(3, 4)
In the code above, there is no need to name the element. The element is referred by a shorter form via a_. Also there is no need to qualify the method call.
Similar Reads
How to go To and From Java Collections in Scala? In their respective languages, Java collections and Scala collections are two distinct sets of data structures that might be frequently utilized. Although Java collections are a factor of the Java Standard Library, Scala collections are made expressly to combine with the useful programming features
2 min read
How to Reduce Code Duplication in Scala? For this definition, duplicate code refers to a sequence of source code that appears more than once in a program, whether inside the same program or across various programs owned or maintained by the same company. For a variety of reasons, duplicate code is typically seen as bad. A minimal requireme
8 min read
Program to convert Java list to Stream in Scala A java list can be converted to Stream in Scala by utilizing toStream method of Java in Scala. Here, we need to import Scalaâs JavaConversions object in order to make this conversions work else an error will occur. Now, lets see some examples and then discuss how it works in details. Example:1# Scal
2 min read
Methods to call on a Set in Scala A set is a collection that only contains unique items. In Scala, both mutable and immutable sets are available. The mutable set is those set in which the value of the object is change, but, in the immutable set, the value of the object is not changed itself. The immutable set is defined under Scala.
13 min read
Program to convert Java Set to Stream in Scala A java Set can be converted to a Stream in Scala by utilizing toStream method of Java in Scala. Here, we need to import Scalaâs JavaConversions object in order to make this conversions work. Now, let's see some examples and then discuss how it works in details. Example:1# Scala // Scala program to c
2 min read