Scala
Object Oriented
Functional
Scalable language
- Most of it is defined on the libraries but not in the language
var capital = Map("US" -> "Washington", "France" -> "Paris")
capital += ("Japan" -> "Tokyo")
println(capital("France"))
Scalable language
- Most of it is defined on the libraries but not in the language
def factorial(x: BigInt): BigInt =
if (x == 0) 1 else x * factorial(x - 1)
// factorial(30) = 265252859812191058636308480000000: BigInt
SCALABLE LANGUAGE
- Big integers
- Big decimals
- Complex numbers
- Rational numbers
- Confidence intervals
- Polynomials
Scale the language
Bazaar vs cathedral metaphor
Java compatibility
- Most of it is defined on the libraries but not in the language
import java.math.BigInteger
def factorial(x: BigInteger): BigInteger =
if (x == BigInteger.ZERO)
BigInteger.ONE
else
x.multiply(factorial(x.subtract(BigInteger.ONE)))
BigInt, Int, and basic types are wrapped around Java's classes
Object oriented
all but the most trivial programs need some sort of structure. The most straightforward way to do this is to put data and operations into some form of containers.
object oriented
Every value is an object and every operation is a method call
val x: Int = 3
val y = 3 + 5 // Int + Int
val z = (v: Int) => v + 4 // Functions are Function objects
Functional
Functions are first class members
List(1, 2, 3, 4, 5, 6).foreach(println)
def myFunction(f: Int => Int, v: Int) = {
f(v)
}
def insideFunction(x: Int) = {
x * 2
}
myFunction(insideFunction, 8) // 16
Functional
Immutability: Operations should map input values into output values
val x = List(1, 2, 3, 4, 5)
val y = x.map( n => n * 2 )
println(x) // List(1, 2, 3, 4, 5)
println(y) // List(2, 4, 6, 8, 10)
Concise
Java
// this is Java
class MyClass {
private int index;
private String name;
public MyClass(int index, String name) {
this.index = index;
this.name = name;
}
}
Concise
Scala
class MyClass(index: Int, name: String)
High Level
Java
boolean nameHasUpperCase = false; // this is Java
for (int i = 0; i < name.length(); ++i) {
if (Character.isUpperCase(name.charAt(i))) {
nameHasUpperCase = true;
break;
}
}
High level
Scala
val nameHasUpperCase = name.exists(_.isUpper)
Statically typed
- Better context help at the IDEs and at compilation time!
- Safe refactoring
- Verifiable properties
val list/*: List[Int]*/ = List/*[Int]*/(1, 3, 5, 7, 10)
val x = list.map( (n/*Int*/) => n * 2 )
HANDS
ON
With Scala
(below)
Run the scala shell
In a terminal, Run (WITHOUT $)
$ scala
Basic types
1 // Int
1.444 // Double
true // Boolean
"Hello, Scala!" // String
Compound operations
(1 + 2) * 3
Method calls
"Hello, Scala!".size
1.to(10) // 1 to 10
Operators are just methods with symbolic names
Text
3 + 2 == 3.+(2)
Naming things
val radius = 10
val pi = 3.14159
pi * radius * radius
Methods
def square(x: Double) = x * x
square(3.0) // = ???
// Multiple parameters
def sumOfSquares(x: Double, y: Double) = square(x) + square(y)
CONDITIONALS
def abs(x: Int) = if (x >= 0) x else -x
// or
def abs2(x: Int) = {
if (x >= 0) {
x
} else {
-x
}
}
Exercise:
https://www.scala-exercises.org/scala_tutorial/functional_loops
Structures
val list = List("1", "2", "3", "4") // List[String]
val myMap = Map(
1 -> "one",
2 -> "two",
3 -> "three")
val seq = Seq("1", "2", "3", "4") // Seq[String]
val range = 1 to 5
Transformations (Map)
val list = List(1, 2, 3, 4)
// Map
list.map(x => x * 2) // List(2, 4, 6, 8)
Transformations (FILTER)
val list = List(1, 2, 3, 4, 5, 6)
// Filter
list.filter(x => x < 4)
Transformations
- All of them return a new List (Immutability)
- Create a method that checks returns all the odd numbers between (inclusive) two given numbers l and k
def getOdds(l: Int, k: Int): List[Int] = {
??? // All of your code here
}
getOdds(1, 10) // Should be 1, 3, 5, 7, 9
Classes
class Dog(val name: String) {
def bark = println("Woof woof")
def call = println("Comming!")
}
val dog = new Dog("Pablito")
traits, classes, Case classes and Objects
case class Person(fullName: String, age: Int)
object Person {
private val king = Person("Tomas", 39)
def recognizeActualKing = king.fullNAme
}
Scala
By Camilo Sampedro
Scala
- 216