Scala Tutorial - Learn How To Use View Function With Examples
Overview
In this tutorial, we will learn how to use the view function with examples on collection data structures in Scala. The view function is applicable to both Scala's Mutable and Immutable collection data structures.
The view method will create a non-strict version of the collection which means that the elements of the collection will only be made available at access time.
As per the Scala documentation, the definition of the view method is as follows:
def view: TraversableView[A, Repr]
The view method is a member of TraversableLike trait.
Steps
1. How to create a large numeric range and take the first 10 odd numbers
For the purpose of this tutorial, we will take a step back from our usual donut examples :) Instead, we will create a rather large List of Int consisting of 1 million Int. When creating the list, we will also make use of the convenient Numeric Range features from Scala.
From this list of type Int, we will filter all the even numbers and finally only take the first 10 odd numbers.
println("Step 1: How to create a large numeric range and take the first 10 odd numbers")
val largeOddNumberList: List[Int] = (1 to 1000000).filter(_ % 2 != 0).take(10).toList
You should see the following output when you run your Scala application in IntelliJ:
Step 1: How to create a large numeric range and take the first 10 odd numbers
First 100 odd numbers from largeOddNumberList = List(1, 3, 5, 7, 9, 11, 13, 15, 17, 19)
NOTE:
- The size of our List of Int matters here. In the above example, we only had 1 million elements. What if you had a much larger list?
- Running computation on the large list would require to eagerly build the elements of the list which could result in the typical OutOfMemory exception.
2. How to lazily create a large numeric range and take the first 10 odd numbers
Fortunately, Scala provides a convenient view method which will only make the collection elements available at access time. With this in mind, let's rewrite the example from Step 1 and add the view method as shown below.
println(s"\nStep 2: How to lazily create a large numeric range and take the first 10 odd numbers")
val lazyLargeOddNumberList = (1 to 1000000).view.filter(_ % 2 != 0).take(10).toList
println(s"Lazily take the first 100 odd numbers from lazyLargeOddNumberList = ${lazyLargeOddNumberList}")
You should see the following output when you run your Scala application in IntelliJ:
Step 2: How to lazily create a large numeric range and take the first 10 odd numbers
Lazily take the first 100 odd numbers from lazyLargeOddNumberList = List(1, 3, 5, 7, 9, 11, 13, 15, 17, 19)
Summary
In this tutorial, we went over the following:
- How to create a large numeric range and take the first 10 odd numbers
- How to lazily create a large numeric range and take the first 10 odd numbers
Tip
Source Code
The source code is available on the allaboutscala GitHub repository.
What's Next
In the next tutorial, I will show you how to use the withFilter function.