SlideShare a Scribd company logo
SCALA
QUICK INTRODUCTION
d.jureczko@gmail.com
@DamianJureczko
AGENDA
A little bit about Scala
Basic syntax
Object-oriented Scala
Functional Scala
Live code
SCALA
General purpose programming language
Multiparadigm: object-oriented & functional
Statically typed
Runs on the JVM
Created by Martin Odersky
First release in 2004
GET STARTED WITH SCALA
Binaries
scala-lang.org/download
SBT
scala-sbt.org/download
IDE
Scala IDE (Eclipse), IntelliJ, NetBeans
SBT
A build tool for Scala, Java and more
scala-sbt.org
FIRST APP
object HelloWorld {
def main(args: Array[String]): Unit = {
println("Hello, World!")
}
}
BASIC SYNTAX
val name: String = "John"
var age: Int = 30
i = 31
def add(x: Int, y: Int): Int = {
x + y
}
STRING INTERPOLATION
val name: String = "John"
println(s"Hello $name")
MULTILINE STRINGS
val text: String =
"""
|This text spans
|multiple lines.
""".stripMargin
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 !!!
TYPE INFERENCE
val name = "John"
val age = 30
def add(x: Int, y: Int): Int = x + y
val sum = add(1, 2)
OBJECT-ORIENTED SCALA
Everything is an object
val x = 10
x.toString
CLASSES
abstract class Vehicle {
def move(): Unit
}
class Car extends Vehicle {
override def move(): Unit = {
println("driving")
}
}
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())
}
}
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")
}
}
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)
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"
}
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
}
FUNCTIONAL SCALA
FUNCTIONAL PROGRAMMING
Pure functions
No side effects
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
HIGH-ORDER FUNCTIONS
Take other functions as an argument
Return functions as a result
SCALA FUNCTIONS
// function type
(Int, Int) => Int
// anonymous function
(x: Int, y: Int) => x + y
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)
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)
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)
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)
}
SCALA COLLECTIONS
Immutable/mutable
Operated by pure functions
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)
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)
MAPS
val students = Map("John" -> 4, "Adam" -> 3)
val moreStudents = students + ("Robert" -> 5)
moreStudents.map {
case (name, grade) => name -> (grade + 1)
}
AND THERE IS MORE
Futures
Implicits
Type Classes
Generic Classes
...
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
COURSERA
Functional Programming Principles in Scala
Functional Program Design in Scala
Scala Quick Introduction
Scala Quick Introduction
THANK YOU
QUESTIONS ???

More Related Content

PDF
7 Habits For a More Functional Swift
PDF
SOLID in Practice
PDF
Funcitonal Swift Conference: The Functional Way
PDF
From OOP To FP Through A Practical Case
PDF
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...
PDF
Solid in practice
PPTX
Object-Oriented Programming with PHP (part 1)
PPTX
Working With JQuery Part1
7 Habits For a More Functional Swift
SOLID in Practice
Funcitonal Swift Conference: The Functional Way
From OOP To FP Through A Practical Case
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...
Solid in practice
Object-Oriented Programming with PHP (part 1)
Working With JQuery Part1

What's hot (15)

PPTX
PHP PPT FILE
PPT
DBIx-DataModel v2.0 in detail
PPTX
Class 8 - Database Programming
PPT
Basic Object Oriented Concepts
PDF
How to write code you won't hate tomorrow
PDF
The underestimated power of KeyPaths
PPTX
Js types
PDF
Web 6 | JavaScript DOM
PPTX
javascript
PPTX
Introduction in php part 2
PDF
Introduzione JQuery
PDF
Scala 101
PDF
perl-pocket
PDF
PHP OOP
PDF
A quick python_tour
PHP PPT FILE
DBIx-DataModel v2.0 in detail
Class 8 - Database Programming
Basic Object Oriented Concepts
How to write code you won't hate tomorrow
The underestimated power of KeyPaths
Js types
Web 6 | JavaScript DOM
javascript
Introduction in php part 2
Introduzione JQuery
Scala 101
perl-pocket
PHP OOP
A quick python_tour
Ad

Viewers also liked (20)

PDF
Rekabentuk mesra OKU
PDF
Brochure Acqua Group
DOC
MAdissertation
PPTX
Question 3
PDF
Presentazione Club4business
PDF
9781119101987RetailNetworksForDummies_15954 (1)
PDF
THYMIO-130513
PPTX
silla de trabajo desplazable para un estudiante con movilidad reducida
PDF
Momento inercia
PPTX
Design presentation #1
PDF
CaseStudies
PPTX
International clearing union.pptx 2
PDF
Del Tingo Al Tango_ From Here to There
PPTX
Content marketing 101
DOCX
GMPacketFLAAB(20)
PPTX
Knowledge management
PPT
BVServices
DOCX
Final SHOT plan
Rekabentuk mesra OKU
Brochure Acqua Group
MAdissertation
Question 3
Presentazione Club4business
9781119101987RetailNetworksForDummies_15954 (1)
THYMIO-130513
silla de trabajo desplazable para un estudiante con movilidad reducida
Momento inercia
Design presentation #1
CaseStudies
International clearing union.pptx 2
Del Tingo Al Tango_ From Here to There
Content marketing 101
GMPacketFLAAB(20)
Knowledge management
BVServices
Final SHOT plan
Ad

Similar to Scala Quick Introduction (20)

PDF
Functional programming in Scala
ODP
PPTX
Scala for curious
PDF
Scala: Object-Oriented Meets Functional, by Iulian Dragos
PDF
Scala for Java Developers (Silicon Valley Code Camp 13)
PPTX
Principles of functional progrmming in scala
PDF
Scala in Practice
PDF
Programming in scala - 1
PPTX
Scala, Play 2.0 & Cloud Foundry
PDF
Functional programming in Scala
ODP
Functional programming with Scala
PDF
Meet scala
PPTX
Scala fundamentals
PPTX
Scala Introduction
PPTX
Intro to Scala
PPTX
Intro to Functional Programming in Scala
PDF
Scala intro workshop
PDF
Introduction To Scala
PDF
Scala Intro
PDF
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Functional programming in Scala
Scala for curious
Scala: Object-Oriented Meets Functional, by Iulian Dragos
Scala for Java Developers (Silicon Valley Code Camp 13)
Principles of functional progrmming in scala
Scala in Practice
Programming in scala - 1
Scala, Play 2.0 & Cloud Foundry
Functional programming in Scala
Functional programming with Scala
Meet scala
Scala fundamentals
Scala Introduction
Intro to Scala
Intro to Functional Programming in Scala
Scala intro workshop
Introduction To Scala
Scala Intro
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf

Recently uploaded (20)

PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
ai tools demonstartion for schools and inter college
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
Essential Infomation Tech presentation.pptx
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
Transform Your Business with a Software ERP System
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
System and Network Administration Chapter 2
PDF
How Creative Agencies Leverage Project Management Software.pdf
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Operating system designcfffgfgggggggvggggggggg
Design an Analysis of Algorithms II-SECS-1021-03
ai tools demonstartion for schools and inter college
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Essential Infomation Tech presentation.pptx
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Transform Your Business with a Software ERP System
Internet Downloader Manager (IDM) Crack 6.42 Build 41
CHAPTER 2 - PM Management and IT Context
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
How to Migrate SBCGlobal Email to Yahoo Easily
Upgrade and Innovation Strategies for SAP ERP Customers
Understanding Forklifts - TECH EHS Solution
Odoo POS Development Services by CandidRoot Solutions
2025 Textile ERP Trends: SAP, Odoo & Oracle
System and Network Administration Chapter 2
How Creative Agencies Leverage Project Management Software.pdf

Scala Quick Introduction

  • 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
  • 5. SBT A build tool for Scala, Java and more scala-sbt.org
  • 6. FIRST APP object HelloWorld { def main(args: Array[String]): Unit = { println("Hello, World!") } }
  • 7. BASIC SYNTAX val name: String = "John" var age: Int = 30 i = 31 def add(x: Int, y: Int): Int = { x + y }
  • 8. STRING INTERPOLATION val name: String = "John" println(s"Hello $name")
  • 9. MULTILINE STRINGS val text: String = """ |This text spans |multiple lines. """.stripMargin
  • 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)
  • 12. OBJECT-ORIENTED SCALA Everything is an object val x = 10 x.toString
  • 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
  • 22. HIGH-ORDER FUNCTIONS Take other functions as an argument Return functions as a result
  • 23. SCALA FUNCTIONS // function type (Int, Int) => Int // anonymous function (x: Int, y: Int) => x + y
  • 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
  • 34. COURSERA Functional Programming Principles in Scala Functional Program Design in Scala