Scala Tutorial - Learn How To Use Take Function With Examples

By Nadim Bahadoor | Last updated: December 18, 2017 at 20:49 pm

Overview

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

 

The take method takes an integer N as parameter and will use it to return a new collection consisting of the first N elements.

 

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

def take(n: Int): Repr

The take 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 donuts 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 elements from the sequence using the take function

Using the donuts sequence from Step 1, you can use the take method to return a new collection with donut elements up to the N parameter of the take method.


println("\nStep 2: How to take elements from the sequence using the take function")
println(s"Take the first donut element in the sequence = ${donuts.take(1)}")
println(s"Take the first and second donut elements in the sequence = ${donuts.take(2)}")
println(s"Take the first, second and third donut elements in the sequence = ${donuts.take(3)}")

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


Step 2: How to take elements from the sequence using the take function
Take the first donut element in the sequence = List(Plain Donut)
Take the first and second donut elements in the sequence = List(Plain Donut, Strawberry Donut)
Take the first, second and third donut elements in the sequence = List(Plain Donut, Strawberry Donut, Glazed Donut)

 

NOTE:

  • If you pass in an integer parameter N to the take method where N is larger than the size of the collection, the take method will return all the elements in the collection.

This concludes our tutorial on Learn How To Use Take 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 Sequence of donuts
  • How to take elements from the sequence using the take function

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 takeRight 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