A Beginner’s Guide to Scala

A Beginner’s Guide to Scala

Learn the basics in Scala including syntax, how to set up an environment, and various common data types.

Originally published by Aditya Sharma at https://www.datacamp.com

Introduction

Scala was designed by Martin Odersky, a professor at École Polytechnique Fédérale de Lausanne (EPFL) in Switzerland and a German computer scientist. Martin started working on creating Scala in 2001, and it was first released in 2004.

Scala stands for Scalable Language. It is a general-purpose, object-oriented language which has a few commonalities with the Java programming language. It provides support for functional programming. Many of Scala's design decisions were aimed to address the drawbacks of the Java programming language.

A few features that Scala offers are:

  • Scala's source code can be compiled to Java bytecode and hence, are executable on a Java Virtual Machine (JVM).
  • It provides language interoperability with Java so that libraries written in either language can be referenced in Scala or Java codebase.
  • Like Java, Scala also uses a curly-brace syntax.
  • It has many features of functional programming languages like Scheme, Standard Machine Learning and Haskell, including currying, type inference, immutability, lazy evaluation, and pattern matching.
  • Support for Web application using JavaScript.
  • Unlike Java, Scala also supports operator overloading, optional parameters, named parameters, and raw strings.

One Interesting fact about Scala is that it is widely used by companies like Apple, Twitter, Walmart, and Google because of its scalability and the capability of being used in backend operations.

Setting up Scala in Jupyter Notebook

Jupyter notebook is the most widely used tools in computer science, especially in the data science domain. It also has support for Scala development with the help of spylon-kernel.

Setting up the Scala environment in jupyter notebook is not rocket science. So, let's quickly set it up in just a few steps.

  • First, you will install the spylon-kernel.
  • pip install spylon-kernel (Python 2)
  • pip3 install spylon-kernel (Python 3)
  • Next, you will create a kernel specification which will allow you to select the scala-kernel in jupyter notebook.
  • python -m spylon_kernel install (Python 2)
  • python3 -m spylon_kernel install (Python 3)
  • Then, as an important step, you will install pyspark and findspark since spylon-kernel also installs some spark components.
  • pip install pyspark findspark (Python 2)
  • pip3 install pyspark findspark (Python 3)
  • Finally, start your jupyter notebook and choose kernel as spylon-kernel and you are good to go!

  • Let's run a small piece of code to test whether the Scala kernel was correctly configured or not.
val x = 2 
val y = 3 
x*y


x: Int = 2 
y: Int = 3 res1: 
Int = 6

Great! So as you can observe from the above output, you were successful in configuring the Scala environment in jupyter notebook.

Basic Syntax

Let's start by printing Hello, World! in Scala.

print("Hello, World!") 


Hello, World!

As you can see, it was so simple, just a print command followed by the input as an argument.

Now, let's take a look at few Syntax related details in Scala:

  • Scala is case-sensitive, which means identifier DataCamp and dataCamp would have a different meaning in Scala.
  • In Scala, all class names first letter should be in Upper Case. If many words are combined to form a name of the class, each separate word's first letter should be in Upper Case. For example, class MyScalaDataCampTutorial.
  • The method names in Scala work slightly differently as compared to class names mainly to distinguish the method names from class names. The method names should start with a Lower Case letter (Source). If multiple words are combined to form the name of the method, then each inner word's first letter should be in Upper Case. For example, def firstDatacampScalaTutorial()
  • In Scala, the name of the program file should exactly match the object name. When saving the file, you need to save it using the object name and append .scala to the end of the name. For example, Let's say ScalaTutorial is the object name. Then the file should be saved as ScalaTutorial.scala.
  • Note: If the file name and the object name does not match, then your program will not even compile.
  • Finally, like most programming languages function, Scala program processing also starts from the main() method, which is a crucial part of every Scala Program.

Below figure shows the reserved words in Scala which cannot be used as constants or variables or as any other identifiers.

(Source

Data Types

Data types in Scala are almost identical to many other programming languages like C, C++, Java, Python, etc. with similar memory footprint and precision.

Finally, let's take a look at a few of the data types that exist in Scala:

A few of the other data types are String, Boolean, Null.

Conclusion

Congratulations on finishing this tutorial.

It is a good starting point for beginners who are interested in learning Scala.

There is a plethora of information related to Scala that remains unraveled like if/else, for loops, various operators in Scala, etc., which will be covered in the future tutorials, so stay tuned!

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow us on Facebook | Twitter

Further reading

The Data Science Course 2019: Complete Data Science Bootcamp

Machine Learning A-Z™: Hands-On Python & R In Data Science

Tableau 10 A-Z: Hands-On Tableau Training For Data Science!

R Programming A-Z™: R For Data Science With Real Exercises!

Machine Learning, Data Science and Deep Learning with Python

Deep Learning A-Z™: Hands-On Artificial Neural Networks

Python for Data Science and Machine Learning Bootcamp

A Beginners Guide for Building Neural Networks in Tensorflow

Cheat Sheets for AI, Neural Networks, Machine Learning, Deep Learning & Big Data

scala data-science python

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

50 Data Science Jobs That Opened Just Last Week

Data Science and Analytics market evolves to adapt to the constantly changing economic and business environments. Our latest survey report suggests that as the overall Data Science and Analytics market evolves to adapt to the constantly changing economic and business environments, data scientists and AI practitioners should be aware of the skills and tools that the broader community is working on. A good grip in these skills will further help data science enthusiasts to get the best jobs that various industries in their data science functions are offering.

Basic Data Types in Python | Python Web Development For Beginners

In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.

Data Science With Python | Python For Data Science | Data Science For Beginners

This Data Science with Python Tutorial will help you understand what is Data Science, basics of Python for data analysis, why learn Python, how to install Python, Python libraries for data analysis, exploratory analysis using Pandas, introduction to series and dataframe, loan prediction problem, data wrangling using Pandas, building a predictive model using Scikit-Learn and implementing logistic regression model using Python.

Data Science Course in Dallas

Become a data analysis expert using the R programming language in this [data science](https://360digitmg.com/usa/data-science-using-python-and-r-programming-in-dallas "data science") certification training in Dallas, TX. You will master data...

Applications Of Data Science On 3D Imagery Data

The agenda of the talk included an introduction to 3D data, its applications and case studies, 3D data alignment and more.