Scala Tutorial - Learn How To Use DropWhile Function

By Nadim Bahadoor | Last updated: July 25, 2017 at 14:57 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 all the elements in a collection which satisfies the predicate function. It will then return a new collection with elements that matched 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", "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 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.


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, 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 shows how to call the dropWhile method by passing through the value predicate function from Step 3 in order to drop elements whose donut names start with letter P.



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, 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
Senior Software Developer | Nephila Capital
Founder of allaboutscala.com. I have over 10 years of experience in building large scale real-time trading systems in the financial industry. Passionate about Distributed Systems, Scala, Big Data and Functional Programming. Stay in touch for upcoming tutorials!
Other allaboutscala.com tutorials you may like:

Share this article on