How I learned Scala in one month

Hi, I’m Leonard. I have been an intern developer at Clever Cloud for one month now. When I came in, the CEO put me on a Scala project the first day. As a software engineer, there’s often a pressure to know everything, and I had never done scala before. My only experience in programming was html, php, javascript, and a little bit of java. So I had to dive into this new language quite quickly.

The thing is, Scala is a functional programming language. And the most brutal change was to learn this new paradigm. If you don't know what functional programming is, there is something you need to understand. In FP, the trick is to do everything only with functions (as in math function). Define the purest function that you can.

But you'll ask me, what is the interest of a pure function? A pure function is very useful because it can be tested separately, and especially because it's very reusable!

First steps towards the infinite beauty of pure functions

What’s a pure function?

A pure function takes a value, returns an other value and does nothing else. For a given argument, it always returns the same result. You don't want any side effect like database calls, etc. But there’s some help from the compiler: Scala has a strong static type system. Be sure the compiler will yell at you if you try to bypass it.

At first sight, Scala can look like an awful programming language, especially if you only code in javascript or php. But after a couple of weeks, by trying hard and being bashed a lot by the compiler, your code gets more and more structured. You finally get used to pure functions, and employ less and less side effects and mutating values.

After one month, I did not become a professional in Scala, but I’m now more and more comfortable with it and I find pleasure in coding. I began to integrate the abilities of FP principles and their great potential. Think about all the possibilities it can offer: it is almost endless.

Getting started with Scala

To learn scala, it’s useful to have some tooling in order to evaluate your code. Check the download page to get typesafe activator. If you have a favorite IDE, I recommend to keep it. Else I personally use vim or sublime text, where Scala syntax highlighting works out of the box.

First, there is more than one way to learn scala. A good way is to look at concrete exercises. Try to rewrite a basic mathematical function or some exercises like this one.

"I have x employees in my office. I want to have the sum of the salary of all Senior employees"

First we need to create the Employee class

case class Employee(name: String, title: String, salary: Int)

Then we create a list of these salary

val employees: List[Employee] = List(
  Employee("Smith", "Junior Developer", 1250),
  Employee("George", "Senior Analyst", 2000),
  Employee("David", "Cadet Developer", 1000),
  Employee("Bernard", "Senior Developer", 2100),
  Employee("Clement", "Senior Developer", 1850)
)

I then define a function which takes an Employee as a parameter, and returns true if the employee title begins with "Senior".

def isSenior(employee: Employee) = employee.title.startsWith("Senior")

Finally, we just apply isSenior on the list of employees to keep the senior employees, and then sum their salaries.

val seniorSalaries =
   employees
     .filter(isSenior _)
     .map(_.salary)
     .sum

To explain, we apply the filter method to our list. filter is a native function which selects all elements which satisfy a predicate (here the isSenior function). Then we map over it. map transforms a list of elements of type A into a list of elements of type B, by applying a function from A to B to all its elements. Here, we transform a List[Employee] into a List[Int] by getting the salary of each employee. Finally, we do a sum on the list of salaries.

As you can see, we solved the problem with small pure functions. They are reusable regardless of the context and we were able to compose them with functions provided by the scala standard library.

If you want to dig deeper into Scala, check this book

If you have any questions, don’t hesitate to send me an email at leonard.imbert@clever-cloud.com

Blog

À lire également

clever cloud fest. is coming

Clever Cloud announces its clever cloud fest. an event to bring together customers, prospects, partners and more.
Company

Clever Cloud: Discover our clever side

Faced with increasingly demanding technical environments, Clever Cloud has established itself as a key player in supporting tech teams and IT decision-makers.
Company

Metabase on Clever Cloud: easily query and visualize your data

Your business generates data, which you need to analyse, understand and make available to your teams, both technical and non-technical. To meet this need, we worked with David Sferruzza to integrate Metabase for the cloud, which is available on our Marketplace and can be easily deployed on Clever Cloud.
Company