Scala Tutorial - Learn How To Use TakeWhile Function With Examples

By Nadim Bahadoor | Last updated: December 20, 2017 at 21:05 pm

Overview

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

 

The takeWhile method takes a predicate function and will use it to return a new collection consisting of elements which match the predicate function.

 

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

def takeWhile(p: (A) ⇒ Boolean): Repr

The takeWhile method is a member of IterableLike trait.

Steps

1. How to initialize a List of donuts

The code below shows how to create a List of donuts of type String.


println("Step 1: How to initialize a List of donuts")
val donuts: Seq[String] = List("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 List of donuts
Elements of donuts = List(Plain Donut, Strawberry Donut, Glazed Donut)

2. How to take elements from the List using the takeWhile function

The code below shows how to use the takeWhile method to take donut elements whose names start with letter P.


println("\nStep 2: How to take elements from the List using the takeWhile function")
println(s"Take donut elements which start with letter P = ${donuts.takeWhile(_.charAt(0) == 'P')}")

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


Step 2: How to take elements from the List using the takeWhile function
Take donut elements which start with letter P = List(Plain Donut)

3. How to declare a predicate function to be passed-through to the takeWhile function

Let's create a predicate function which will encapsulate the donut predicate of selecting donut elements whose names start with letter P.


println("\nStep 3: How to declare a predicate function to be passed-through to the takeWhile function")
val takeDonutPredicate: (String) => Boolean = (donutName) => donutName.charAt(0) == 'P'
println(s"Value function takeDonutPredicate = $takeDonutPredicate")

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 takeWhile function
Value function takeDonutPredicate = <function1>

4. How to take elements using the predicate function from Step 3

With the predicate function defined in Step 3, we can not pass it to the takeWhile function in order to take donut elements whose names start with letter P.


println("\nStep 4: How to take elements using the predicate function from Step 3")
println(s"Take elements using function from Step 3 = ${donuts.takeWhile(takeDonutPredicate)}")

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


Step 4: How to take elements using the predicate function from Step 3
Take elements using function from Step 3 = List(Plain Donut)

 

This concludes our tutorial on Learn How To Use TakeWhile 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 List of donuts
  • How to take elements from the List using the takeWhile function
  • How to declare a predicate function to be passed-through to the takeWhile function
  • How to take 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 the transpose function.

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Senior Principal Engineer | Elsevier
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