Scala Tutorial - Learn How To Use DropWhile Function

By Nadim Bahadoor | Last updated: June 23, 2019 at 18:03 pm

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:

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)

 

This concludes our tutorial on Learn How To Use DropWhile Function 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 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

  • 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 exists 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: