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

What's hot (15)

PPTX
PHP PPT FILE
AbhishekSharma2958
 
PPT
DBIx-DataModel v2.0 in detail
Laurent Dami
 
PPTX
Class 8 - Database Programming
Ahmed Swilam
 
PPT
Basic Object Oriented Concepts
Scott Lee
 
PDF
How to write code you won't hate tomorrow
Pete McFarlane
 
PDF
The underestimated power of KeyPaths
Vincent Pradeilles
 
PPTX
Js types
LearningTech
 
PDF
Web 6 | JavaScript DOM
Mohammad Imam Hossain
 
PPTX
javascript
Kaya Ota
 
PPTX
Introduction in php part 2
Bozhidar Boshnakov
 
PDF
Introduzione JQuery
orestJump
 
PDF
Scala 101
Andrey Myatlyuk
 
PDF
perl-pocket
tutorialsruby
 
PDF
PHP OOP
Oscar Merida
 
PDF
A quick python_tour
cghtkh
 
PHP PPT FILE
AbhishekSharma2958
 
DBIx-DataModel v2.0 in detail
Laurent Dami
 
Class 8 - Database Programming
Ahmed Swilam
 
Basic Object Oriented Concepts
Scott Lee
 
How to write code you won't hate tomorrow
Pete McFarlane
 
The underestimated power of KeyPaths
Vincent Pradeilles
 
Js types
LearningTech
 
Web 6 | JavaScript DOM
Mohammad Imam Hossain
 
javascript
Kaya Ota
 
Introduction in php part 2
Bozhidar Boshnakov
 
Introduzione JQuery
orestJump
 
Scala 101
Andrey Myatlyuk
 
perl-pocket
tutorialsruby
 
PHP OOP
Oscar Merida
 
A quick python_tour
cghtkh
 

Viewers also liked (20)

PDF
Rekabentuk mesra OKU
Mohd Affandi hashim
 
PDF
Brochure Acqua Group
Matteo Lefebvre
 
DOC
MAdissertation
Mike Furber
 
PPTX
Question 3
BBJN
 
PDF
Presentazione Club4business
Umberto Benedetti
 
PDF
9781119101987RetailNetworksForDummies_15954 (1)
Alec Thorkelson
 
PDF
THYMIO-130513
Isabella Loddo
 
PPTX
silla de trabajo desplazable para un estudiante con movilidad reducida
Wilmer Marcano
 
PDF
Momento inercia
jesu buitra
 
PPTX
Design presentation #1
Paramveer Singh
 
PDF
CaseStudies
Frank Rudolph
 
PPTX
International clearing union.pptx 2
Freddie North
 
PDF
Del Tingo Al Tango_ From Here to There
Jasmine Nation
 
PPTX
Content marketing 101
James Perry
 
DOCX
GMPacketFLAAB(20)
Danielle Gertner
 
PPTX
Knowledge management
milkaSeba
 
PPT
BVServices
Ben Arditi
 
DOCX
Final SHOT plan
Danielle Gertner
 
Rekabentuk mesra OKU
Mohd Affandi hashim
 
Brochure Acqua Group
Matteo Lefebvre
 
MAdissertation
Mike Furber
 
Question 3
BBJN
 
Presentazione Club4business
Umberto Benedetti
 
9781119101987RetailNetworksForDummies_15954 (1)
Alec Thorkelson
 
THYMIO-130513
Isabella Loddo
 
silla de trabajo desplazable para un estudiante con movilidad reducida
Wilmer Marcano
 
Momento inercia
jesu buitra
 
Design presentation #1
Paramveer Singh
 
CaseStudies
Frank Rudolph
 
International clearing union.pptx 2
Freddie North
 
Del Tingo Al Tango_ From Here to There
Jasmine Nation
 
Content marketing 101
James Perry
 
GMPacketFLAAB(20)
Danielle Gertner
 
Knowledge management
milkaSeba
 
BVServices
Ben Arditi
 
Final SHOT plan
Danielle Gertner
 
Ad

Similar to Scala Quick Introduction (20)

PDF
Functional programming in Scala
Damian Jureczko
 
ODP
Scala ntnu
Alf Kristian Støyle
 
PPTX
Scala for curious
Tim (dev-tim) Zadorozhniy
 
PDF
Scala: Object-Oriented Meets Functional, by Iulian Dragos
3Pillar Global
 
PDF
Scala for Java Developers (Silicon Valley Code Camp 13)
Ramnivas Laddad
 
PPTX
Principles of functional progrmming in scala
ehsoon
 
PDF
Scala in Practice
Francesco Usai
 
PDF
Programming in scala - 1
Mukesh Kumar
 
PPTX
Scala, Play 2.0 & Cloud Foundry
Pray Desai
 
PDF
Functional programming in Scala
datamantra
 
ODP
Functional programming with Scala
Neelkanth Sachdeva
 
PDF
Meet scala
Wojciech Pituła
 
PPTX
Scala fundamentals
Alfonso Ruzafa
 
PPTX
Scala Introduction
Constantine Nosovsky
 
PPTX
Intro to Scala
manaswinimysore
 
PPTX
Intro to Functional Programming in Scala
Shai Yallin
 
PDF
Scala intro workshop
Fredrik Vraalsen
 
PDF
Introduction To Scala
Innar Made
 
PDF
Scala Intro
Paolo Platter
 
PDF
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
Functional programming in Scala
Damian Jureczko
 
Scala for curious
Tim (dev-tim) Zadorozhniy
 
Scala: Object-Oriented Meets Functional, by Iulian Dragos
3Pillar Global
 
Scala for Java Developers (Silicon Valley Code Camp 13)
Ramnivas Laddad
 
Principles of functional progrmming in scala
ehsoon
 
Scala in Practice
Francesco Usai
 
Programming in scala - 1
Mukesh Kumar
 
Scala, Play 2.0 & Cloud Foundry
Pray Desai
 
Functional programming in Scala
datamantra
 
Functional programming with Scala
Neelkanth Sachdeva
 
Meet scala
Wojciech Pituła
 
Scala fundamentals
Alfonso Ruzafa
 
Scala Introduction
Constantine Nosovsky
 
Intro to Scala
manaswinimysore
 
Intro to Functional Programming in Scala
Shai Yallin
 
Scala intro workshop
Fredrik Vraalsen
 
Introduction To Scala
Innar Made
 
Scala Intro
Paolo Platter
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
Ad

Recently uploaded (20)

PPTX
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PDF
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PDF
MiniTool Power Data Recovery 8.8 With Crack New Latest 2025
bashirkhan333g
 
PPTX
Tally software_Introduction_Presentation
AditiBansal54083
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PDF
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
PDF
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
PPTX
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PDF
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
PDF
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PDF
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
MiniTool Power Data Recovery 8.8 With Crack New Latest 2025
bashirkhan333g
 
Tally software_Introduction_Presentation
AditiBansal54083
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 

Scala Quick Introduction