Scripts, applications, and real-world workflows

Introduction to Scala

David Venturi

Curriculum Manager, DataCamp

The Scala interpreter

$ scala
Welcome to Scala 2.12.7.
Type in expressions for evaluation. Or try :help.
scala> 2 + 3
res0: Int = 5
Introduction to Scala

Scala scripts

  • A sequence of instructions in a file, executed sequentially
  • Useful for smaller projects
  • At a command prompt, the scala command executes a script by wrapping it in a template and then compiling and executing the resulting program

Run code button and submit answer button

Introduction to Scala

Scala scripts

If we put this code into a file named game.scala:

// Start game
println("Let's play Twenty-One!")

Then run:

$ scala game.scala
Let's play Twenty-One!
Introduction to Scala

Interpreted language vs. compiled language

Interpreter: a program that directly executes instructions written in a programming language, without requiring them previously to have been compiled into machine code.

Compiler: a program that translates source code from a high-level programming language to a lower level language (e.g., machine code) to create an executable program.

Introduction to Scala

Scala applications

  • Compiled explicitly then run explicitly
  • Consist of many source files that can be compiled individually
  • Useful for larger programs
  • No lag time since applications are precompiled
Introduction to Scala

Scala applications

If we put this code into a file named Game.scala:

object Game extends App {
    println("Let's play Twenty-One!")
}

First, compile with scalac:

$ scalac Game.scala

Second, run with scala:

$ scala Game
Introduction to Scala

Scala applications

If we put this code into a file named Game.scala:

object Game extends App {
    println("Let's play Twenty-One!")
}

First, compile with scalac:

$ scalac Game.scala

Second, run with scala:

$ scala Game
Let's play Twenty-One!
Introduction to Scala

Pros and cons of compiled languages

Pros
  • Increased performance once compiled

$$

Cons
  • It takes time to compile code
Introduction to Scala

Scala workflows

There are two main ways people prefer to work in Scala:

  • Using the command line
  • Using an IDE (integrated development environment)
Introduction to Scala

IDE

  • Especially useful for larger projects with many files
  • IntelliJ IDEA: most commonly-used IDE by Scala developers

IntelliJ logo

Introduction to Scala

IntelliJ IDEA

IntelliJ IDEA interface

Introduction to Scala

sbt

IntelliJ IDEA

IntelliJ IDEA interface

sbt

  • "simple build tool"

  • Compiles, runs, and tests Scala applications

Introduction to Scala

Scala kernel for Jupyter

Jupyter logo

Almond logo

1 https://almond.sh/
Introduction to Scala

Let's practice!

Introduction to Scala

Preparing Video For Download...