Scala Crash Course, part 1: Scala Basics

Introduction

This short Scala crash course is based on the execellent Scala School by Twitter.

We chose Scala as the language for the course (as well as the language we develop our systems in) because of several reasons, some of which are:

  • Increasingly used in NLP and ML, but don't take our word for it, take a look at the code of some of the world's leading labs: UCL Machine Reading group :), Berkeley NLP Group and another interesting ML project at UC Berkeley , University of Washington, Allen Institute for Artificial Intelligence, etc.
  • Expressive - Scala offers first class functions, and closures, and effectively enables you to quickly rewrite pseudocode or math models to code
  • Concise - it offers type inference, and is on the quest to eliminate boilerplate code
  • Java interoperability - you can reuse your favorite Java libraries! This comes in VERY handy.
  • Scala is not Java on steroids :) it is just a different, object-oriented functional programming language (cannot stress the functional part enough)

Installing Scala

In order to install Scala, please follow these instructions.www.scala-lang.org/download/install.html

Starting the interpreter: run scala from the command line

Programming in IntelliJ: www.jetbrains.com/idea

There is also an Scala IDE based on Eclipse, but we recommend IntelliJ!

The Simple Build Tool (SBT)

Get it here: http://www.scala-sbt.org/

  • A build tool (like Maven)
  • Needed for compiling the assignment code

Running the StatNLP Book locally

You need to do this if you want to make use of the interactive part of the tutorial and lecture!

git clone https://github.com/uclmr/stat-nlp-book.git; cd stat-nlp-book
git submodule update --init --recursive
sbt compile
cd wolfe; sbt compile; sbt publish-local; cd ..
cp moro/conf/application-statnlpbook.conf moro/conf/application.conf
cd moro; git checkout master; sbt run

open localhost:9000 in your browser

Scala Basics

This part of the book concerns the basics of Scala, a quick crash-course of Scala which you will need in order to understand this book, and be able to code up your assignments. All sorts of feedback are welcome and highly appreciated!

You can run these commands either in IntelliJ, or by running

sbt console

or

scala

in your command line, and thus entering Scala's REPL (Read-Evaluate-Print Loop) interpreter.

Expressions

Almost everything in Scala is an expression, for example:

Numerical Calculations

2.71828189

However, be aware that Scala's automatic type inference doesn't have to work like you want it to:

0

In this case, having two Integers, Scala infers that it needs to use integer division, which is wrong if you wanted to get a decimal value.

String Operations

Soft kitty, warm kitty

Logical expressions

true

Values and variables

Scala suports values and variables. Values are technically constants, and they cannot be changed (immutable), as opposed to variables which can (mutable). Try removing the commented piece of code to verify that:

I cannot be changed! Really!
See? I can change!

You might ask yourself: why should I use values and immutable structure? There are several reasons for and against using them. Immutable structures help with reasoning about the code, concurrency, make the code less prone to bugs (no references to take care of), etc. You can find a couple of thoughts about that here and here. You might also ask yourself: how do I change something in an immutable structure then? Easily - you copy it with a change in place :) However, you will see more in the rest of the tutorial.

Control Structures

If-then-else

Hi!

For Loop

55

While Loop

55

Functions

In Scala, functions are objects you create with the keyword def, e.g.:

9001

As you can see from the definition, you need to specify the type of the parameters, but you can freely omit the output type as the interpreter/compiler will do that implicitly (except in cases of recursive functions). Functions can be stored in variables and passed as parameters, as they are full-fledged Scala objects.

Let's take a look at a couple of functions' capabilities on a small NLP example - let's build something (maybe) useful which depluralizes (removes suffixes of plural forms of) nouns:

They are literally objects!

9
8

If you don't want your function to return a value (like void in C), use Unit as a return value:

()

Since functions are objects, we can pass them to functions!

HELLO, SCALA!

There are different ways of writing functions!

11

The last expression in the body of a function is its return value. Also, functions without arguments can be called without parenthesis.

This is fun!

???

In-code TODO statements

2
$qmark$qmark$qmark: Nothing answerToLifeTheUniverseAndEverything: ()Nothing question: (s: String)Nothing answerOnePlusOne: ()Int res48: Int = 2
Exercise
Write a function that repeats a word N times.
Exercise
Write a function that reverses a word.

Variable length arguments

+soft+kitty+warm+kitty

Case Classes

Case classes are regular Scala classes which export their constructor parameters and enable you to recursively decompose them with pattern matching. You don't have to write new!

true

For Comprehension

5
5
5

Pattern Matching

Pattern matching is the second most used feature of scala. It is a general mechanism which allows you to match on different kinds of data structures.

There is an incorporated company named Google

We can define a factorial function with pattern matching as follows.

n!=k=1nk

120
Exercise
Write a function that calculates the Nth fibonacci number.

Fn=Fn1+Fn2,F0=0,F1=1

factorial: (n: Int)Int res71: Int = 120

Class Inheritance

0.2

Hello World

Since we have enough understanding of Scala, we can proceed with understanding a Hello World application in it:

HelloWorld1$@147b51b9

The preferred version of starting your programs:

HelloWorld2$@2832510e