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.
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:
Now, we can start coding out the examples on this Scala application.
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:
Also, if we want we can see all immutable collection objects in package
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 in scala are a general interface for data structures that have a well-defined order and can be indexed.
These support various operations like:
reversefor indexing and iterating.
- Concatenation, appending, prepending.
- A lot of others - grouping, sorting, zipping, searching and slicing.
Let's see how to define a sequence:
Range is one of the sequences available, we can define a
Range like this:
There is another type of sequence,
LinearSeq which is an immutable linked list with the following properties:
- Accessing head, tail and using methods like
isEmptyare fast and takes
- Most other operations like
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.
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.
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
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:
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
Listsequence, you can try these on other collections as well.
First, define our list collection like this:
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
flatMap methods. A
for loop in scala can be written as:
These are some important methods to remember while coding out in scala.
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:
- Scala For The Impatient
- Scala Programming Projects
- Functional Programming in Scala
- Programming 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.
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
++your GitHub Profile README? Check out Quote - README
Till next time!