How I learned Scala in one month
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!
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.
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
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 firstname.lastname@example.org