SlideShare ist ein Scribd-Unternehmen logo
1 von 37
Downloaden Sie, um offline zu lesen
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
THANK YOU
QUESTIONS ???

Weitere ähnliche Inhalte

Was ist angesagt?

DBIx-DataModel v2.0 in detail
DBIx-DataModel v2.0 in detail DBIx-DataModel v2.0 in detail
DBIx-DataModel v2.0 in detail Laurent Dami
 
Class 8 - Database Programming
Class 8 - Database ProgrammingClass 8 - Database Programming
Class 8 - Database ProgrammingAhmed Swilam
 
Basic Object Oriented Concepts
Basic Object Oriented ConceptsBasic Object Oriented Concepts
Basic Object Oriented ConceptsScott Lee
 
How to write code you won't hate tomorrow
How to write code you won't hate tomorrowHow to write code you won't hate tomorrow
How to write code you won't hate tomorrowPete McFarlane
 
The underestimated power of KeyPaths
The underestimated power of KeyPathsThe underestimated power of KeyPaths
The underestimated power of KeyPathsVincent Pradeilles
 
javascript
javascript javascript
javascript Kaya Ota
 
Introduzione JQuery
Introduzione JQueryIntroduzione JQuery
Introduzione JQueryorestJump
 
A quick python_tour
A quick python_tourA quick python_tour
A quick python_tourcghtkh
 

Was ist angesagt? (15)

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

Andere mochten auch

Andere mochten auch (20)

Rekabentuk mesra OKU
Rekabentuk mesra OKURekabentuk mesra OKU
Rekabentuk mesra OKU
 
Brochure Acqua Group
Brochure Acqua GroupBrochure Acqua Group
Brochure Acqua Group
 
MAdissertation
MAdissertationMAdissertation
MAdissertation
 
Question 3
Question 3 Question 3
Question 3
 
Presentazione Club4business
Presentazione Club4businessPresentazione Club4business
Presentazione Club4business
 
9781119101987RetailNetworksForDummies_15954 (1)
9781119101987RetailNetworksForDummies_15954 (1)9781119101987RetailNetworksForDummies_15954 (1)
9781119101987RetailNetworksForDummies_15954 (1)
 
THYMIO-130513
THYMIO-130513THYMIO-130513
THYMIO-130513
 
mapas mentales
mapas mentalesmapas mentales
mapas mentales
 
silla de trabajo desplazable para un estudiante con movilidad reducida
silla de trabajo desplazable para un estudiante con movilidad reducidasilla de trabajo desplazable para un estudiante con movilidad reducida
silla de trabajo desplazable para un estudiante con movilidad reducida
 
MyCV
MyCVMyCV
MyCV
 
Momento inercia
Momento inerciaMomento inercia
Momento inercia
 
Design presentation #1
Design presentation #1Design presentation #1
Design presentation #1
 
CaseStudies
CaseStudiesCaseStudies
CaseStudies
 
International clearing union.pptx 2
International clearing union.pptx 2International clearing union.pptx 2
International clearing union.pptx 2
 
Del Tingo Al Tango_ From Here to There
Del Tingo Al Tango_ From Here to ThereDel Tingo Al Tango_ From Here to There
Del Tingo Al Tango_ From Here to There
 
Content marketing 101
Content marketing 101Content marketing 101
Content marketing 101
 
GMPacketFLAAB(20)
GMPacketFLAAB(20)GMPacketFLAAB(20)
GMPacketFLAAB(20)
 
Knowledge management
Knowledge managementKnowledge management
Knowledge management
 
BVServices
BVServicesBVServices
BVServices
 
Final SHOT plan
Final SHOT planFinal SHOT plan
Final SHOT plan
 

Ähnlich wie Scala Quick Introduction

Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in ScalaDamian Jureczko
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Jonas Bonér
 
Pragmatic Real-World Scala
Pragmatic Real-World ScalaPragmatic Real-World Scala
Pragmatic Real-World Scalaparag978978
 
From Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risksFrom Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risksSeniorDevOnly
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?Tomasz Wrobel
 
Intro to scala
Intro to scalaIntro to scala
Intro to scalaJoe Zulli
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecLoïc Descotte
 
The Death of Final Tagless
The Death of Final TaglessThe Death of Final Tagless
The Death of Final TaglessJohn De Goes
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Jesper Kamstrup Linnet
 

Ähnlich wie Scala Quick Introduction (20)

Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in Scala
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Scala fundamentals
Scala fundamentalsScala fundamentals
Scala fundamentals
 
Scala Intro
Scala IntroScala Intro
Scala Intro
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)
 
Pragmatic Real-World Scala
Pragmatic Real-World ScalaPragmatic Real-World Scala
Pragmatic Real-World Scala
 
From Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risksFrom Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risks
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
 
A bit about Scala
A bit about ScalaA bit about Scala
A bit about Scala
 
Intro to scala
Intro to scalaIntro to scala
Intro to scala
 
SDC - Einführung in Scala
SDC - Einführung in ScalaSDC - Einführung in Scala
SDC - Einführung in Scala
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar Prokopec
 
The Death of Final Tagless
The Death of Final TaglessThe Death of Final Tagless
The Death of Final Tagless
 
Scala for curious
Scala for curiousScala for curious
Scala for curious
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?
 

Kürzlich hochgeladen

Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
ManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide DeckManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide DeckManageIQ
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024Mind IT Systems
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfryanfarris8
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyAnusha Are
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 

Kürzlich hochgeladen (20)

Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
ManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide DeckManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide Deck
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodology
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.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
  • 35.
  • 36.