Scala Tutorial - Learn How To Use Tail Function With Examples

By Nadim Bahadoor | Last updated: December 17, 2017 at 21:08 pm

Overview

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

 

The tail method returns a collection consisting of all elements except the first one.

 

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

def tail: Repr

The tail method is a member of TraversableLike 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 return all elements in the sequence except the head using the tail function

Using the tail method, you can get all the elements in the donut sequence except the first one as shown below.


println("\nStep 2: How to return all elements in the sequence except the head using the tail function")
println(s"Elements of donuts excluding the head = ${donuts.tail}")

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


Step 2: How to return all elements in the sequence except the head using the tail function
Elements of donuts excluding the head = List(Strawberry Donut, Glazed Donut)

 

3. How to access the last element of the donut sequence by using the last function

The tail method from Step 2 should not be confused with the last method. As shown in the example below, the last method is used to access only the last element in the donut sequence.


println("\nStep 3: How to access the last element of the donut sequence by using the last function")
println(s"Last element of donut sequence = ${donuts.last}")

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


Step 3: How to access the last element of the donut sequence by using the last function
Last element of donut sequence = Glazed Donut

 

4. How to access the first element of the donut sequence by using the head function

Similar to the last method, you can use the head method to access only the first element in the donut sequence as shown below.


println("\nStep 4: How to access the first element of the donut sequence by using the head function")
println(s"First element of donut sequence = ${donuts.head}")

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


Step 4: How to access the first element of the donut sequence by using the head function
First element of donut sequence = Plain Donut

This concludes our tutorial on Learn How To Use Tail 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 return all elements in the sequence except the head using the tail function
  • How to access the last element of the donut sequence by using the last function
  • How to access the first element of the donut sequence by using the head 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 take 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