Introduction:
This is going to be a series of theories and personal experience with Scala. I am currently using Scala for developing Spark applications.
Background:
Scala is an abbreviation of the term SCAlable LAnguage. It was created by Professor Martin Odersky and his group at EPFL, Ecole polytechnique federale de Lausanne, Switzerland, in 2003. Scala provides a high-performance, concurrent-ready environment for functional programming and object-oriented programming on the Java Virtual Machine (JVM) platform.
Installing Scala:
As a JVM language, Scala requires the use of a Java runtime. Scala 2.11 needs at least Java 6. For optimal performance, rather install Java 8 JDK. The download for Java 8 JDK is available on Oracle’s website.
The following installation can be done on Ubuntu. Copy and create an executable file and run it. For instance, using nano or any editor of your choice, from the terminal:
sudo nano install_java_scala.sh -> this will create a file called install_java_scala.sh
copy the script below and paste
ctl + o + enter to save
ctl + x to exit nano
sudo chmod +x install_java_scala.sh to make the file executable
run ./install_java_scala.sh
Notes:
We are installing wget in order to download the installation files needed to install Java and Scala.
Also not that you might have to change the link to download Java, because it changes.
Script:
install_java_scala
Once it is done, you can test to see if both Java and Scala have been installed correctly. To test, from the terminal run java -version and you should get:
java version “1.8.0_161”
Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)
As for Scala, just run scala and you should get:
After running scala from the terminal, you will be now on the Scala REPL and all is good to go for coding in Scala. If you are having issues in getting started with Scala, make sure that the Java command is installed correctly and that the system path points to the correct Java version.
The Scala REPL is a tool for evaluating expressions in Scala: for instance, type println(“Welcome to scala!”), it will print, Welcome to Scala!. The scala command will execute a source script, like the above println(text), by wrapping it in a template and then compiling and executing the resulting program. A help system is available and can be started by entering :help command.
Note: A Read-Eval-Print Loop (REPL), also known as an interactive top level or language shell, is a simple, interactive computer programming environment that takes single user inputs (i.e single expressions), evaluates them and returns the result to the user.
The other tool to install is SBT, which is the de facto build tool for Scala and the instructions for installing the tool can be found here (scala-sbt.org). Once the installation is done, you will be able to run sbt command from the terminal. If you start the sbt command and don’t specify a task to run, SBT starts the REPL. Below is the lists of sbt tasks you can run together with the description of the task:
-
-
- clean Delete all build artifacts
- compile Incrementally compile
- console Start the Scala REPL
- eclipse Generate Eclipse project files
- exit Quit the REPL or Ctrl+d
- help Describe commands
- run Run one of the “main” routines in the project
- show x Show the definition of variable “x”
- tasks Show the most commonly-used available tasks
- tasks -V Show ALL the available tasks
- test Incrementally compile the code and run the tests
- ~test Run incr. compiles and tests whenever files are saved
This works for any command prefixed by “~”
-
Conclusion
This is a basic introduction to Scala. In the next series I will look into useful terms in Scala that needs to be defined and explained.
Source:
Learning scala
REPL
REPL 2