Let's go back to basics with Scala!
Software Engineer, Durga Prasana helps us learn the Scala essentials from start to finish. Want to brush up on your skills? Or new to Scala Programming? Check it out and get learning.
'Scala is a general purpose, high-level programming language that offers a balance between developing functional and object-oriented programs.
What is functional programming all about? In simple terms, functions are the first-class citizens in functional programming. In order to extend upon a core set of functionalities of a program, we tend to write additional classes extending upon certain guidelines / interfaces. In functional programming, functions help us achieve the same.
We’ll use the Scala REPL for all explanations. It is a very handy and informative tool for learning Scala. It logs cute little messages on how our code is interpreted and executed.
Let’s start with the basics first.
1. Variables
We can define immutable variables using 'val':
Mutable variables can be defined and modified using 'var':
We use 'def' to assign a label to an immutable value whose evaluation is deferred for a later time. It means the label’s value is lazily evaluated every time upon use.
Did you observe something interesting?
While defining 'alias', no value was assigned to 'alias: String' since it is lazily associated, when we invoke it. What would happen if we change the value of 'name'?
2. Control flow
We use control flow statements to express our decision logic.
You can write an 'if-else' statement as below:
Or, you can use 'while':
3. Collections
Scala explicitly distinguishes between immutable versus mutable collections — right from the package namespace itself ( 'scala.collection.immutable' or 'scala.collection.mutable').
Unlike immutable collections, mutable collections can be updated or extended in place. This enables us to change, add, or remove elements as a side effect.
But performing addition, removal, or update operations on immutable collections returns a new collection instead.
Immutable collections are always automatically imported via the 'scala._'(which also contains alias for 'scala.collection.immutable.List').
However, to use mutable collections, you need to explicitly import 'scala.collection.mutable.List'.
In the spirit of functional programming, we’ll primarily base our examples on immutable aspects of the language, with minor detours into the mutable side.
List
We can create a list in various ways:
Another handy approach is to define a list using the cons '::' operator. This joins a head element with the remaining tail of a list.
Which is equivalent to:
We can access list elements directly by their index. Remember Scala uses zero-based indexing:
Some common helper methods include:
'list.head', which returns the first element:
'list.tail', which returns the tail of a list (which includes everything except the head):
Set
Here, ‘Arthur’ is repeated twice, and so is ‘Uther’.
Let’s create a Set with the same names. Notice how it excludes the duplicates.
We can check for the existence of specific element in Set using 'contains()':
We can add elements to a Set using the + method (which takes 'varargs' i.e. variable-length arguments)
Similarly we can remove elements using the '-' method
Map
Values for a specific key in map can be accessed as:
We can add an entry to Map using the '+' method:
To modify an existing mapping, we simply re-add the updated key-value:
Note that since the collection is immutable, each edit operation returns a new collection( 'res0', 'res1') with the changes applied. The original collection 'kingSpouses' remains unchanged.
4. Functional combinators
Now that we’ve learned how to group a set of entities together, let’s see how we can use functional combinators to generate meaningful transformations on such collections.
In John Hughes’ simple words:
A combinator is a function which builds program fragments from program fragments.
An in-depth look at how combinators work is outside of this article’s scope. But, we’ll try to touch upon a high-level understanding of the concept anyhow.
Let’s take an example.
Suppose we want to find names of all queens using the 'kingSpouses' collection map that we created.
We’d want to do something along the lines of examining each entry in the map. If the 'key' has the name of a king, then we’re interested in the name of it’s spouse (i.e. queen).
We shall use the 'filter' combinator on map, which has a signature like:
Overall we shall perform the following steps to find queens:
- Find the (key, value) pairs with kings’ names as keys.
- Extract the values (names of queen) only for such tuples.
The 'filter' is a function which, when given a (key, value), returns true / false.
- Find the map entries pertaining to kings.
Now we shall use the filter function defined above to 'filter' kingly entries.
2. Extract the names of respective queens from the filtered tuples.
Let’s print out the names of queens using the 'foreach' combinator:
Some other useful combinators are 'foreach', 'filter', 'zip', 'partition', 'find'.
We shall re-visit some of these after having learnt how to define functions and passing functions as arguments to other functions in higher-order functions.
Let’s recap on what we’ve learned:
- Different ways of defining variables
- Various control-flow statements
- Some basics about various collections
- Overview of using functional combinators on collections
I hope you found this article useful. It is first in a series of articles to follow on learning Scala.
In part two, we’ll learn about defining classes, traits, encapsulation and other object-oriented concepts.
Please feel free to let me know your feedback and suggestions on how I can improve the content. Until then, happy coding.'
This article was written by Durga Prasana and posted originally on Medium.