DEV Community

Preston Pham
Preston Pham

Posted on • Originally published at undertherain.io

5 reasons to learn Scala in 2019

scala
If you are looking for a new programming language to learn this year, Scala is a good choice. Here are 5 reasons why:

Flexibility

If it is 10 years ago, I would never recommend Scala to anyone. Because a programming language can be very fast, elegant, and feature rich but is still not good to learn if nobody else is using it. However, that is not the case anymore because Scala has been growing quickly and is now used in many areas such as:

There is even an open-source chess server written in Scala.

Elegance

Having used C++, C#, Java, and Python, I can say that Scala has a very concise and expressive syntax.

For example, to print "hello world" on console:

println("hello world")
Enter fullscreen mode Exit fullscreen mode

To declare a value (or variable in Java):

val i: Int = 6
val j = 9 // automatically inferred as `Int`
Enter fullscreen mode Exit fullscreen mode

To declare a method:

def add(a: Int, b: Int) = a + b
Enter fullscreen mode Exit fullscreen mode

To remove all odd numbers in a list:

val list = List(1, 2, 3)
list.filter(x => isEven(x)) // result: List(2)
// or more like English
list filter isEven // result: List(2)
Enter fullscreen mode Exit fullscreen mode

You can omit punctuations to have a more English-like syntax for extra expressiveness, this is called infix notation.

Rich API

Scala built-in library comes with an extremely rich API. Filtering a list in the code snippet above is a very typical example.

List API also contains many other methods. For example, to loop through each item in a list:

list.foreach(println)
Enter fullscreen mode Exit fullscreen mode

To sum a list of numbers:

list.sum // result: 6
Enter fullscreen mode Exit fullscreen mode

It works as long as the list contains numbers (e.g. integers, doubles...)

it_just_works

Computing the intersection of 2 lists is also simple:

val l1 = List(1, 2, 3)
val l2 = List(2, 3, 4)
l1.intersect(l2) // result: List(2, 3)
Enter fullscreen mode Exit fullscreen mode

Great community & libraries

Yes, indeed. Compared to NodeJS or Python, Scala has a much smaller community and far fewer libraries (not counting Java ones). But it is not really bad because:

  • Most of the time, we only use a few top libraries and those in Scala have high quality to make up for quantity
  • People are very active and willing to help each other. Especially Gitter channels like Spark with Scala and typelevel/cats

Java Interoperability

Simply said, Java methods can be called and Java classes can be extended in Scala code. This means you have access to all Java libraries out there.

Conclusion

I was once questioning why should I use a language that looks so complicated. But after I understand Scala and realize how my life becomes much more easier, I'm in love with it. If the 5 reasons above are interesting to you, make sure to check out a short tour of Scala.

It's my first post to DEV, if you have any question or feedback, please let me know :)

Top comments (0)