Scala Tutorial - Learn How To Use Slice Function With Examples

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

Overview

In this tutorial, we will learn how to use the slice function with examples on collection data structures in Scala. The slice function is applicable to both Scala's Mutable and Immutable collection data structures.

 

The slice method takes a start and end index and will use them to return a new collection with elements that are within the start and end index range.

 

As per the Scala documentation, the definition of the slice method is as follows:

def slice(from: Int, until: Int): Repr

The slice method is a member of IterableLike trait.

Steps

1. How to initialize a Sequence of donuts

The code below shows how to create a Sequence of donut elements of type String.


println("Step 1: How to initialize a Sequence of donuts")
val donuts: Seq[String] = Seq("Plain Donut", "Strawberry Donut", "Glazed Donut")
println(s"Elements of donuts = $donuts")

You should see the following output when you run your Scala application in IntelliJ:


Step 1: How to initialize a Sequence of donuts
Elements of donuts = List(Plain Donut, Strawberry Donut, Glazed Donut)

 

2. How to take a section from the sequence using the slice function

The code below shows how to call the slice method to return elements from the donuts sequence within a given range.


println("\nStep 2: How to take a section from the sequence using the slice function")
println(s"Take elements from the sequence from index 0 to 1 = ${donuts.slice(0,1)}")
println(s"Take elements from the sequence from index 0 to 2 = ${donuts.slice(0,2)}")
println(s"Take elements from the sequence from index 0 to 3 = ${donuts.slice(0,3)}")

You should see the following output when you run your Scala application in IntelliJ:


Step 2: How to take a section from the sequence using the slice function
Take elements from the sequence from index 0 to 1 = List(Plain Donut)
Take elements from the sequence from index 0 to 2 = List(Plain Donut, Strawberry Donut)
Take elements from the sequence from index 0 to 3 = List(Plain Donut, Strawberry Donut, Glazed Donut)

 

3. Slice function where the index is out of range

In the example below, we are using the slice method to return elements from index 0 to 4. However, we know that the donuts sequence only has 3 elements and therefore index 4 does not exist. With the slice method, you do not have to worry about IndexOutOfBoundsException.


println("\nStep 3: Slice function where the index is out of range")
println(s"Take elements from the sequence from index 0 to 4 = ${donuts.slice(0,4)}")

You should see the following output when you run your Scala application in IntelliJ:


Step 3: How to use slice function where the index is out of range
Take elements from the sequence from index 0 to 4 = List(Plain Donut, Strawberry Donut, Glazed Donut)

This concludes our tutorial on Learn How To Use Slice 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 initialize a Sequence of donuts
  • How to take a section from the sequence using the slice function
  • Slice function where the index is out of range

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 sortBy 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: