scala quick introduction
TRANSCRIPT
![Page 2: Scala Quick Introduction](https://reader031.vdocuments.site/reader031/viewer/2022022202/588568b61a28ab491b8b5e41/html5/thumbnails/2.jpg)
AGENDAA little bit about ScalaBasic syntaxObject-oriented ScalaFunctional Scala
Live code
![Page 3: Scala Quick Introduction](https://reader031.vdocuments.site/reader031/viewer/2022022202/588568b61a28ab491b8b5e41/html5/thumbnails/3.jpg)
SCALAGeneral purpose programming languageMultiparadigm: object-oriented & functionalStatically typedRuns on the JVMCreated by Martin OderskyFirst release in 2004
![Page 4: Scala Quick Introduction](https://reader031.vdocuments.site/reader031/viewer/2022022202/588568b61a28ab491b8b5e41/html5/thumbnails/4.jpg)
GET STARTED WITH SCALABinaries
scala-lang.org/download
SBT scala-sbt.org/download
IDE Scala IDE (Eclipse), IntelliJ, NetBeans
![Page 5: Scala Quick Introduction](https://reader031.vdocuments.site/reader031/viewer/2022022202/588568b61a28ab491b8b5e41/html5/thumbnails/5.jpg)
SBTA build tool for Scala, Java and more
scala-sbt.org
![Page 6: Scala Quick Introduction](https://reader031.vdocuments.site/reader031/viewer/2022022202/588568b61a28ab491b8b5e41/html5/thumbnails/6.jpg)
FIRST APPobject HelloWorld {
def main(args: Array[String]): Unit = { println("Hello, World!") } }
![Page 7: Scala Quick Introduction](https://reader031.vdocuments.site/reader031/viewer/2022022202/588568b61a28ab491b8b5e41/html5/thumbnails/7.jpg)
BASIC SYNTAXval name: String = "John"
var age: Int = 30 i = 31
def add(x: Int, y: Int): Int = { x + y }
![Page 8: Scala Quick Introduction](https://reader031.vdocuments.site/reader031/viewer/2022022202/588568b61a28ab491b8b5e41/html5/thumbnails/8.jpg)
STRING INTERPOLATIONval name: String = "John"
println(s"Hello $name")
![Page 9: Scala Quick Introduction](https://reader031.vdocuments.site/reader031/viewer/2022022202/588568b61a28ab491b8b5e41/html5/thumbnails/9.jpg)
MULTILINE STRINGSval text: String = """ |This text spans |multiple lines. """.stripMargin
![Page 10: Scala Quick Introduction](https://reader031.vdocuments.site/reader031/viewer/2022022202/588568b61a28ab491b8b5e41/html5/thumbnails/10.jpg)
STATICALLY TYPED LANGUAGEvar name: String = "John" name = "Mark"
name = 2 // compilation error !!!
def add(x: Int, y: Int): Int = x + y
add(1, "two") // compilation error !!!
![Page 11: Scala Quick Introduction](https://reader031.vdocuments.site/reader031/viewer/2022022202/588568b61a28ab491b8b5e41/html5/thumbnails/11.jpg)
TYPE INFERENCEval name = "John"
val age = 30
def add(x: Int, y: Int): Int = x + y
val sum = add(1, 2)
![Page 12: Scala Quick Introduction](https://reader031.vdocuments.site/reader031/viewer/2022022202/588568b61a28ab491b8b5e41/html5/thumbnails/12.jpg)
OBJECT-ORIENTED SCALAEverything is an object
val x = 10
x.toString
![Page 13: Scala Quick Introduction](https://reader031.vdocuments.site/reader031/viewer/2022022202/588568b61a28ab491b8b5e41/html5/thumbnails/13.jpg)
CLASSESabstract class Vehicle { def move(): Unit }
class Car extends Vehicle { override def move(): Unit = { println("driving") } }
![Page 14: Scala Quick Introduction](https://reader031.vdocuments.site/reader031/viewer/2022022202/588568b61a28ab491b8b5e41/html5/thumbnails/14.jpg)
TRAITStrait 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()) } }
![Page 15: Scala Quick Introduction](https://reader031.vdocuments.site/reader031/viewer/2022022202/588568b61a28ab491b8b5e41/html5/thumbnails/15.jpg)
OBJECTSobject 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") } }
![Page 16: Scala Quick Introduction](https://reader031.vdocuments.site/reader031/viewer/2022022202/588568b61a28ab491b8b5e41/html5/thumbnails/16.jpg)
CASE CLASSES// declare case class User(email: String, password: String)
// create val admin = User("[email protected]", "buddy")
// access fields val adminEmail = admin.email
// create copy val otherAdmin = admin.copy(email = "[email protected]")
// compare assert(admin != otherAdmin)
![Page 17: Scala Quick Introduction](https://reader031.vdocuments.site/reader031/viewer/2022022202/588568b61a28ab491b8b5e41/html5/thumbnails/17.jpg)
PATTERN MATCHINGval 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" }
![Page 18: Scala Quick Introduction](https://reader031.vdocuments.site/reader031/viewer/2022022202/588568b61a28ab491b8b5e41/html5/thumbnails/18.jpg)
PATTERN MATCHING AND CASE CLASSESval result = user match { case User("[email protected]", "buddy") => "it's administrator"
case User(email, password) => "it's " + email + ", his password is: " + password }
![Page 19: Scala Quick Introduction](https://reader031.vdocuments.site/reader031/viewer/2022022202/588568b61a28ab491b8b5e41/html5/thumbnails/19.jpg)
FUNCTIONAL SCALA
![Page 20: Scala Quick Introduction](https://reader031.vdocuments.site/reader031/viewer/2022022202/588568b61a28ab491b8b5e41/html5/thumbnails/20.jpg)
FUNCTIONAL PROGRAMMINGPure functionsNo side effects
![Page 21: Scala Quick Introduction](https://reader031.vdocuments.site/reader031/viewer/2022022202/588568b61a28ab491b8b5e41/html5/thumbnails/21.jpg)
FIRST-CLASS FUNCTIONSFunction is a first-class citizen
Can be assigned to a variableCan be passed as an argument of a functionCan be returned from a function
![Page 22: Scala Quick Introduction](https://reader031.vdocuments.site/reader031/viewer/2022022202/588568b61a28ab491b8b5e41/html5/thumbnails/22.jpg)
HIGH-ORDER FUNCTIONSTake other functions as an argumentReturn functions as a result
![Page 23: Scala Quick Introduction](https://reader031.vdocuments.site/reader031/viewer/2022022202/588568b61a28ab491b8b5e41/html5/thumbnails/23.jpg)
SCALA FUNCTIONS// function type (Int, Int) => Int
// anonymous function (x: Int, y: Int) => x + y
![Page 24: Scala Quick Introduction](https://reader031.vdocuments.site/reader031/viewer/2022022202/588568b61a28ab491b8b5e41/html5/thumbnails/24.jpg)
ASSIGNING FUNCTION TO A VARIABLEcase 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)
![Page 25: Scala Quick Introduction](https://reader031.vdocuments.site/reader031/viewer/2022022202/588568b61a28ab491b8b5e41/html5/thumbnails/25.jpg)
PASSING FUNCTION TO A HIGH-ORDERFUNCTION
// 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)
![Page 26: Scala Quick Introduction](https://reader031.vdocuments.site/reader031/viewer/2022022202/588568b61a28ab491b8b5e41/html5/thumbnails/26.jpg)
RETURNING FUNCTION FROM A HIGH-ORDERFUNCTION
// 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)
![Page 27: Scala Quick Introduction](https://reader031.vdocuments.site/reader031/viewer/2022022202/588568b61a28ab491b8b5e41/html5/thumbnails/27.jpg)
PARTIAL FUNCTIONStrait 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) }
![Page 28: Scala Quick Introduction](https://reader031.vdocuments.site/reader031/viewer/2022022202/588568b61a28ab491b8b5e41/html5/thumbnails/28.jpg)
SCALA COLLECTIONSImmutable/mutableOperated by pure functions
![Page 29: Scala Quick Introduction](https://reader031.vdocuments.site/reader031/viewer/2022022202/588568b61a28ab491b8b5e41/html5/thumbnails/29.jpg)
LISTSval 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)
![Page 30: Scala Quick Introduction](https://reader031.vdocuments.site/reader031/viewer/2022022202/588568b61a28ab491b8b5e41/html5/thumbnails/30.jpg)
SETSval 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)
![Page 31: Scala Quick Introduction](https://reader031.vdocuments.site/reader031/viewer/2022022202/588568b61a28ab491b8b5e41/html5/thumbnails/31.jpg)
MAPSval students = Map("John" -> 4, "Adam" -> 3)
val moreStudents = students + ("Robert" -> 5)
moreStudents.map { case (name, grade) => name -> (grade + 1) }
![Page 32: Scala Quick Introduction](https://reader031.vdocuments.site/reader031/viewer/2022022202/588568b61a28ab491b8b5e41/html5/thumbnails/32.jpg)
AND THERE IS MOREFuturesImplicitsType ClassesGeneric Classes...
![Page 33: Scala Quick Introduction](https://reader031.vdocuments.site/reader031/viewer/2022022202/588568b61a28ab491b8b5e41/html5/thumbnails/33.jpg)
LEARN MOREscala-lang.orgProgramming in Scala, First Edition - artima.com/pins1edThe Neophyte's Guide to Scala -danielwestheide.com/scala/neophytes.htmlTwitter's Scala School - twitter.github.io/scala_schoolscala-exercises.org
![Page 34: Scala Quick Introduction](https://reader031.vdocuments.site/reader031/viewer/2022022202/588568b61a28ab491b8b5e41/html5/thumbnails/34.jpg)
COURSERAFunctional Programming Principles in ScalaFunctional Program Design in Scala
![Page 35: Scala Quick Introduction](https://reader031.vdocuments.site/reader031/viewer/2022022202/588568b61a28ab491b8b5e41/html5/thumbnails/35.jpg)
![Page 36: Scala Quick Introduction](https://reader031.vdocuments.site/reader031/viewer/2022022202/588568b61a28ab491b8b5e41/html5/thumbnails/36.jpg)
![Page 37: Scala Quick Introduction](https://reader031.vdocuments.site/reader031/viewer/2022022202/588568b61a28ab491b8b5e41/html5/thumbnails/37.jpg)
THANK YOUQUESTIONS ???