DEV Community

Siddharth Chandra
Siddharth Chandra

Posted on

Scala For Beginners - Crash Course - Part 5

Welcome to the 5th and last part of the Scala beginners crash course, here we will go through the concepts like collections, sequences and map, flatmap, filter in the Scala programming language. This will be a short article, so if you need much more in-depth information you can check out the recommended books at the end of this article.

It will be a no-nonsense, to the point kind of article (like part 1, part 2, part 3 and part 4 with all the necessary links wherever needed.

Without any further ado, let's get started.


Before We Start

Before we start coding out, first create a new object in the crashcourse package (please go through part 1 for more information on creating packages and objects).

Let's name it Collections like below:

collections.png

Now, we can start coding out the examples on this Scala application.

Collections

Scala offers 2 sets of collections, one is mutable and the other one is immutable. Scala by default works with immutable collection objects.

But, we can import mutable collection objects whenever needed like:

import scala.collection.mutable
Enter fullscreen mode Exit fullscreen mode

Also, if we want we can see all immutable collection objects in package scala.collections.immutable.

Following are some of the immutable collection objects provided in scala:

Similar collection objects are available in the mutable set. This link has all the mutable collection objects set.

The commonality between mutable and immutable collection objects is that both extends trait Traversable. It is a base trait for all collections and offers a great variety of methods.

Some of these collection objects are often sequenced in nature. Let's discuss sequences now.

Sequences

Sequences in scala are a general interface for data structures that have a well-defined order and can be indexed.

These support various operations like:

  • apply, iterator, length, reverse for indexing and iterating.
  • Concatenation, appending, prepending.
  • A lot of others - grouping, sorting, zipping, searching and slicing.

Let's see how to define a sequence:

sequence.png

Output:

sequence2.png

Range is one of the sequences available, we can define a Range like this:

range.png

Output:

range2.png

There is another type of sequence, LinearSeq which is an immutable linked list with the following properties:

  • Accessing head, tail and using methods like isEmpty are fast and takes O(1) time.
  • Most other operations like length or reverse takes O(n) time.

Arrays in scala are equivalent to java arrays, these can be manually constructed with predefined lengths. These can be mutated, have fast indexing and are interoperable with java's arrays.

array.png

Vectors are also present in scala, these are the default implementation for immutable sequences taking constant time for indexed based read/write, with fast append/prepend, shows good performance for large size vectors and are implemented using a fixed branched trie data structure.

vector.png

Tuples are finite ordered kinds of lists that can contain a maximum of 22 elements. A tuple value data type is defined similarly to a function like TupleN[...].

tuple.png

There is one more collection object, Map, which is a key->value pair data structure present in scala. Following are some ways to manipulate a map:

map.png

Output:

map2.png

More on Scala collections can be found here or you can check the books provided in the last section for the more detailed in-depth working of collections.

map, flatMap, filter and for

Finally, on the most important part of functional programming, that would become the daily need of a Scala programmer. I wanted to include these in the previous part but to show how these work I needed to show collections in scala first, now that we know enough about collections, let's move on to these functions.

Note: For the sake of examples, I will be showing these operations on the List sequence, you can try these on other collections as well.

First, define our list collection like this:

list.png

What is the map method? It allows a certain function to be applied to all the elements present in the given sequence and evaluates to a sequence of updated values.

What is the filter method? It filters out the elements which do not meet a certain criterion defined as another function and evaluates to a sequence of filtered values.

What is the flatMap method? This is identical to the map method, but the only difference is that in flatMap the inner grouping of an item is removed and a sequence is generated. So, if flatMap is applied to a list then the generated output will be a list of lists, changing the inner grouping of integer elements to a list and later flattening it.

What is the for loop in scala? Yeah, you read it right 'What is' and not 'How to', in scala, writing a for loop is just a fancy way of writing a coupled map and flatMap methods. A for loop in scala can be written as:

mapfilter.png

Output:

mapfilter2.png

These are some important methods to remember while coding out in scala.

Wrap Up

Well, that wraps up part 5 and the end of this crash course.

I hope it was something worth it for you as a reader and made you a scala enthusiast at the end of this course.

You can pick up any of the below books to master the art of programming in scala:

To keep in touch, you can follow me on here or subscribe to get the updates of the new blogs that I will write in the future or follow me on Twitter where I share my thoughts not only on programming but also on personal finance.


  • Make sure to follow me to get regular updates or subscribe so that you never miss my upcoming articles, ranging from Python to Computer Vision to Scala.

  • Just starting your Open Source Journey? Don't forget to check Hello Open Source

  • Want to showcase your Python project or just out there to explore? Check Awesome Python Repos

  • Want to make a simple and awesome game from scratch? Check out PongPong

  • Want to ++ your GitHub Profile README? Check out Quote - README

Till next time!

Namaste 🙏

Top comments (0)