Scala Crash Course, part 1: Scala Basics

Introduction

  • 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

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

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!

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

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$@378e3c51

The preferred version of starting your programs:

HelloWorld2$@2de41398