This article was published as a part of the Data Science Blogathon.
1) Introduction
2) Installation
3) Writing code with Scala
Scala is a high-level language that combines the paradigms of both functional and object-oriented programming, which makes it powerful. It is used by tech giants like Netflix, Twitter, and LinkedIn.
It is a general-purpose language, what that means is it does not cater to a specific domain alone, it is used across multiple domains like healthcare, finance, online services, and media. An example of a domain-specific language would be SQL, which is used to query a relational database.
It is a strongly typed language, meaning, in case you declare a variable to be an integer and pass a string value, the compiler will throw an error. You may be put off by this in case you are used to a dynamic language like python, but if you think about it, it does make sense to throw an error when your declaration is of an integer but you are passing a string, it can help save you from bugs.
As per talent.com the median average salary of a scala developer in India is 15,00,000 INR per annum, and given the comparatively smaller talent pool of scala developers, even higher competitive salary packages are offered to experienced candidates.
There is definitely a market for scala developers but as is with every programming language or merely anything in existence, we have both advantages and disadvantages of Scala.
Let us look at some drawbacks of Scala below
It has a steep learning curve, given that it combines both functional and object-oriented programming styles, Scala is usually reported by developers starting with it as a complex or confusing language. However, there are communities like StackOverflow and Gitter that you can use to solve your doubts and clarify your understanding.
Given your project size, scala can take a longer time to compile.
You can install scala easily using coursier CLI, it will download any other dependencies like JVM required for scala by using simple commands like the below.
cs install scala cs java --jvm 11 -version
Scala uses REPL (Read, Evaluate, Print, Loop), an interactive top level or language shell, which is a simple interactive computer programming environment that takes single user inputs, executes them, and returns the result to the user; a program written in a REPL environment is executed piecewise. (Reference:- Wikipedia)
Other popular languages like Python and Java also have a REPL.
Let’s use Scala REPL to write some code. We would first open our command prompt on our machine, go to the directory that has scala installed, and simply type scala3, which would instantiate the Scala REPL.
We have typed in simple arithmetic commands whose result Scala calculates for us, if you notice it creates a variable res0 for storing the result of the first computation, res1 for the second, and res2 for the third, we can further use these variables as well, let’s see an example.
Source: Author
Variables in scala are of two types var and val, var can be reassigned or are mutable, if you declare a variable as a val it is immutable. In case your code contains a constant you may want to declare it as a val to avoid it being mistakenly updated later, for other times when your variable needs frequent updates you would want to use a var.
We have data types like Integer, Float, String, Boolean, and Unit.
Now let’s further move towards writing multi-line code
println("Welcome to the world of Scala!") //Let's start by understanding pattern matching in scala var day = "Monday" day match { case "Monday" => println("Workday") case "Saturday" => println("Weekday") }
In the above code snippet we are using pattern matching using the match keyword, and then mentioning the case statements against which the pattern is to be matched and the action to be performed given the pattern matches.
The above code would run into a problem if we assign the day variable with any other pattern than Monday or Saturday, it would give us a Match error as below.
To avoid running into such issues we would add a case that would match any other value that day variable could be assigned, as below.
var day = "Sunday" day match { case "Monday" => println("Workday") case "Saturday" => println("Weekday") case _ => println("Input does not match with an existing pattern") }
Source: Author
Notice how in the above code snippet we are hardcoding the patterns to be matched which might not be a very good approach while programming and could be a reason for possible bugs. Let’s change it a little by using a guard(the if condition in the first case statement)
println("Welcome to the world of Scala!") //Let's start by understanding pattern matching in scala var day = "Sunday" day match { case day if day!="Saturday" && day!="Sunday" => println("Workday") case _ => println("Weekend") }
Source: Author
Let us now look at how if-else statements are used in Scala.
var amount = 100 if (amount > 50) { println("Amount is greater than 50") } else { println("Amount is lesser than 50") }
Using If else statements can help in decision-making scenarios. Another interesting thing with Scala is that the if-else statement is also an expression, meaning that it can be evaluated to a value. Let me give you an example.
var amount = 100 val output: String = if (amount > 50) { "Amt greater than 50" } else { "Amt lesser than 50" }
The article aims to provide an understanding of the reasons to use Scala and some basic programming blocks using which you can start out and write your first Scala program. If you like the content and would want to see more tutorials you can definitely mention it in the comments. Feel free to connect with me on Linkedin or check out some of my public work here. Thanks for reading!
The media shown in this article is not owned by Analytics Vidhya and are used at the Author’s discretion.