Scala Tutorial - Learn How To Use View Function With Examples

By Nadim Bahadoor | Last updated: March 16, 2018 at 15:14 pm

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)

 

This concludes our tutorial on Learn How To Use View Function With Examples and I hope you've found it useful!

 

Stay in touch via Facebook and Twitter for upcoming tutorials!

 

Don't forget to like and share this page :)

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

  • Review the tutorials on Mutable and Immutable collection data structures in Scala.

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.

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Technology and Finance Consultant with over 14 years of hands-on experience building large scale systems in the Financial (Electronic Trading Platforms), Risk, Insurance and Life Science sectors. I am self-driven and passionate about Finance, Distributed Systems, Functional Programming, Big Data, Semantic Data (Graph) and Machine Learning.
Other allaboutscala.com tutorials you may like: