Recursive Streams and collection in Scala

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program for resursive 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)
          
        
    

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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) 
    

chevron_right


Output :

Stream(5, ?)

Example 3: Create a lazy list by placing a recursive call to method in a brief and clear manner

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)
  
    

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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(" ; "))
    

chevron_right


Output :

Stream(0, ?)
0 ; 1 ; 2 ; 3 ; 4 ; 5 ; 6 ; 7 ; 8 ; 9)

Example 5: Using filter

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)
    

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)
  
    

chevron_right


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.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.