Scala Tutorial - Learn How To Use Slice Function With Examples
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)
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
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.