From the REPL (Read-Eval-Print Loop)

Basics

  • each expression you tpe is saved as a new numbered value
2 + 2
# res0: Int = 4
res0 + 2
# res1 Int = 6
  • Strongly typed language
  • REPL sessions can be saved
  • Files can be loaded into the REPL
:type (true, 2.0)
# (Boolean, Double)
:save /sites/repl-test.scala
:load /sites/repl-test.scala
  • You can search your recent history
:h?
  • Printing
  • Declaring values use either var or val.
  • val, immutable. var, mutable.
  • statically typed language
  • type inference. Scala compiler can guess what the type of a variable.
val x = 10 // x is now 10
x = 20 // error: ressignment to val
var y = 10
y = 20 // y is now 20

val z: Int = 10
val z: Double = 1.0

val b: Double = 10

true
false

!true
!false
true == false
10 > 5
  • Scala strings are surrounde by double quotes, and '' is a Char
  • Strings have the usual Java methods defined on them and has some extra Scala methods
"abc"
'a'

"hello world".length
"hello world".substring(2, 6)
"hello world".replace("C", "3")
"hello world".take(5)
"hello world".drop(5)
  • String interpolation
  • Expressions are also possible
  • Formatting with interpolated strings with the prefix "f"
  • Raw strings with "raw"
  • Triple double-quotes let strings span multiple rows and contain quotes
s"We hava $n apples"

val a = Array(11, 9 ,6)
s"My second daughter is ${a(0) - a(2)} years old"

f"Square root of 122: ${math.sqrt(11)}%1.4f"
raw"New line feed: \n. Carrige return: \r."

val html = """<form id="daform">
                <p>Press belo', Joe</p>
                <input type="submit">
              </form>""

Functions

def sumOfSquares(x: Int, y: Int): Int = {
    val x2 = x * x
    val y2 = y * y
    x2 + y2
}

def sumOfSquaresShort(x: Int, y: Int): Int = x * x + y * y

sumOfSquares(3, 4) // 25

def subtract(x: Int, y: Int): Int = x - y

subtract(10, 3)
subtract(y=3, x=10)

def sq(x: Int) = x * x // Compiler can guess return type is Int

def addWithDefault(x: Int, y: Int = 5) = x + y // Functions can have default parameters

addWithDefault(1, 2)
addWithDefault(1)

(x: Int) => x * x // Anoymous fucntions

var sq: Int => Int = x => x * x // Int = Int means A functions that takes Int and returns Int

val addOne: Int => Int = _ + 1
val weirSum: (Int, Int) = (_ * 2 + _ * 3)

def foo(x: Int): Int = {
    va; anonFunc: Int => Int = { z =>
        if (z > 5)
            return z
        else
            return z + 2
    }
    
    anonFunc(x)
}

Flow Control

1 to 5
val r = 1 to 5
r.foreach(println)

r foreach println

def showNumbersInRange(a: Int, b: int) Unit = {
    print(a)
    if (a < b)
        showNumbersInRange(a + 1, b)
}

val x = 10
if (x == 10) println("yeah")
if (x == 10) println("yeah")

if (x == 11) println("yeah") else println(""nay)

println(if (x == 10) "yeah" else "nope")

val text = if (x == 10) "yeah" else "nope"

Data Structures

val a = Array(1, 2, 3, 5, 8)
a(0) // Int = 1

val m = Map("fork" -> "tenedor", "spoon" -> "cuchara")
m("fork") // java.lang.String tenedor
m("bottle") // Throws an excepiton

val safeM = m.withDefaultValue("no lo se")
safeM("bottle") // java.lang.String = no lo se

val s = Set(1, 3, 7)

(1, 2) // Tuples

val divideInts = (x: Int, y: Int) => (x / y, x % y)

divideInts(10, 3) // (Int: Int) = (3, 1)

val d = divideInts(10, 3)
d._1 // Int = 3
d._2

val (div, mod) = divideInts(10, 3)

Object Oriented Programming