2. AGENDA
A little bit about Scala
Basic syntax
Object-oriented Scala
Functional Scala
Live code
3. SCALA
General purpose programming language
Multiparadigm: object-oriented & functional
Statically typed
Runs on the JVM
Created by Martin Odersky
First release in 2004
4. GET STARTED WITH SCALA
Binaries
scala-lang.org/download
SBT
scala-sbt.org/download
IDE
Scala IDE (Eclipse), IntelliJ, NetBeans
10. STATICALLY TYPED LANGUAGE
var name: String = "John"
name = "Mark"
name = 2 // compilation error !!!
def add(x: Int, y: Int): Int = x + y
add(1, "two") // compilation error !!!
11. TYPE INFERENCE
val name = "John"
val age = 30
def add(x: Int, y: Int): Int = x + y
val sum = add(1, 2)
13. CLASSES
abstract class Vehicle {
def move(): Unit
}
class Car extends Vehicle {
override def move(): Unit = {
println("driving")
}
}
14. TRAITS
trait Diving {
val deep = 100
def dive(): String = s"diving $deep meters"
}
class Car extends Vehicle with Diving {
override val deep = 200
override def move(): Unit = {
println(dive())
}
}
15. OBJECTS
object SeeDiving {
val MaxDepth = 500
def pressure(depth: Int): Double = depth / 10 * 0.99
}
class Car extends Vehicle with Diving {
override val deep: Int = SeeDiving.MaxDepth
override def move(): Unit = {
println(dive() + s" with ${SeeDiving.pressure(deep)} atm")
}
}
16. CASE CLASSES
// declare
case class User(email: String, password: String)
// create
val admin = User("admin@company.com", "buddy")
// access fields
val adminEmail = admin.email
// create copy
val otherAdmin = admin.copy(email = "admin2@company.com")
// compare
assert(admin != otherAdmin)
17. PATTERN MATCHING
val result = something match {
case "value" => "it's String equal to 'value'"
case 10 => "it's Int equal to 10"
case s: String => "it's String with value: " + s
case _ => "it's something else"
}
18. PATTERN MATCHING AND CASE CLASSES
val result = user match {
case User("admin@company.com", "buddy") =>
"it's administrator"
case User(email, password) =>
"it's " + email + ", his password is: " + password
}
21. FIRST-CLASS FUNCTIONS
Function is a first-class citizen
Can be assigned to a variable
Can be passed as an argument of a function
Can be returned from a function
24. ASSIGNING FUNCTION TO A VARIABLE
case class Student(name: String, grade: Int)
val goodStudent: Student => Boolean =
student => student.grade > 3
assert(goodStudent(Student("John", 4)) == true)
assert(goodStudent(Student("Adam", 3)) == false)
25. PASSING FUNCTION TO A HIGH-ORDER
FUNCTION
// List high-order function
def count(predicate: Student => Boolean): Int
val students = List(Student("John", 4), Student("Adam", 3))
val counter = students.count(goodStudent)
assert(counter == 1)
26. RETURNING FUNCTION FROM A HIGH-ORDER
FUNCTION
// high-order function
def gradeHigherThen(threshold: Int): Student => Boolean =
student => student.grade > threshold
val above3 = gradeHigherThen(3)
val above4 = gradeHigherThen(4)
val counter = students.count(above3)
27. PARTIAL FUNCTIONS
trait PartialFunction[-A, +B] extends (A => B) {
def isDefinedAt(x: A): Boolean
}
val improveGrade: PartialFunction[Student, Student] = {
case student if student.name == "John" =>
student.copy(grade = 5)
}
29. LISTS
val numbers = List(2, 3)
val moreNumbers = 1 :: numbers // List(1, 2, 3)
moreNumbers.count(n => n > 2) // 1
val oddNumbers = moreNumbers.filter(n => n % 2 != 0) // List(1, 3)
val squares = moreNumbers.map(n => n * n) // List(1, 4, 9)
30. SETS
val letters = Set("a", "b", "a", "c") // Set(a, b, c)
val moreLetters = letters + "d" // Set(a, b, c, d)
val upperLetters = moreLetters.map(l => l.toUpperCase)
// Set(A, B, C, D)
31. MAPS
val students = Map("John" -> 4, "Adam" -> 3)
val moreStudents = students + ("Robert" -> 5)
moreStudents.map {
case (name, grade) => name -> (grade + 1)
}
32. AND THERE IS MORE
Futures
Implicits
Type Classes
Generic Classes
...
33. LEARN MORE
scala-lang.org
Programming in Scala, First Edition - artima.com/pins1ed
The Neophyte's Guide to Scala -
danielwestheide.com/scala/neophytes.html
Twitter's Scala School - twitter.github.io/scala_school
scala-exercises.org