Scala Tutorial - Learn How To Use DropWhile Function
Overview
In this tutorial, we will learn how to use the dropWhile function on collection data structures in Scala. The dropWhile function is applicable to both Scala's Mutable and Immutable collection data structures.
The dropWhile method takes a predicate function parameter that will be used to drop certain elements in a collection which satisfies the predicate function. The "dropping" process, or removal of elements, will stop as soon as it encounters an element that does not match the predicate function. Therefore, it will return a new collection with the remaining elements from the original collection, starting from the first element that did not match the predicate function.
As per the Scala documentation, the definition of the dropWhile method is as follows:
def dropWhile(p: (A) ⇒ Boolean): Repr
The dropWhile method is a member of the IterableLike trait.
Steps
1. How to initialize a Sequence of donuts
The code below shows how to initialize 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 1", "Plain Donut 2", "Strawberry Donut", "Plain Donut 3", "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 1, Plain Donut 2, Strawberry Donut, Plain Donut 3, Glazed Donut)
2. How to drop elements from the sequence using the dropWhile function
The code below shows how to use the dropWhile method to drop elements from the Donut sequence whose donut names start with the letter P. The dropWhile method conveniently removes the first two elements, namely, Plain Donut 1 and Plain Donut 2. Since the first letter of the third element (Strawberry Donut) starts with the character 'S', our predicate function no longer holds true. As a result, the "dropping" iteration stops at the third element, and we end up with all the remaining elements starting from the Strawberry Donut element itself - that is, Strawberry Donut, Plain Donut 3 and Glazed Donut.
println("\nStep 2: How to drop elements from the sequence using the dropWhile function")
println(s"Drop donut elements whose name starts with letter P = ${donuts.dropWhile(_.charAt(0) == 'P')}")
You should see the following output when you run your Scala application in IntelliJ:
Step 2: How to drop elements from the sequence using the dropWhile function
Drop donut elements whose name starts with letter P = List(Strawberry Donut, Plain Donut 3, Glazed Donut)
3. How to declare a predicate function to be passed-through to the dropWhile function
The code below shows how to declare a predicate function which will be passed-through to the dropWhile method. The predicate function will have the same behaviour as Step 2 where it will return true for donut names which start with the letter P.
println("\nStep 3: How to declare a predicate function to be passed-through to the dropWhile function")
val dropElementsPredicate: (String) => Boolean = (donutName) => donutName.charAt(0) == 'P'
println(s"Value function dropElementsPredicate = $dropElementsPredicate")
You should see the following output when you run your Scala application in IntelliJ:
Step 3: How to declare a predicate function to be passed-through to the dropWhile function
Value function dropElementsPredicate = <function1>
NOTE:
- The predicate function is a Value Function.
4. How to drop elements using the predicate function from Step 3
The code below replicates Step 2, but makes use of the above-mentioned Value Function that provides a closure for the logic or behavior to our dropWhile predicate function.
println("\nStep 4: How to drop elements using the predicate function from Step 3")
println(s"Drop elements using function from Step 3 = ${donuts.dropWhile(dropElementsPredicate)}")
You should see the following output when you run your Scala application in IntelliJ:
Step 4: How to drop elements using the predicate function from Step 3
Drop elements using function from Step 3 = List(Strawberry Donut, Plain Donut 3, Glazed Donut)
Summary
In this tutorial, we went over the following:
- How to initialize a Sequence of donuts
- How to drop elements from the sequence using the dropWhile function
- How to declare a predicate function to be passed-through to the dropWhile function
- How to drop elements using the predicate function from Step 3
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 exists function.