SlideShare a Scribd company logo
1 of 47
Scala fundamentals
Alfonso Ruzafa
Alfonso Ruzafa
Senior Software Engineer @ Softonic
About me
Scala Fundamentals
Java and the JVM
Scala Fundamentals
Java
1995 Scala
2003
Clojure
2007
JRuby
2001
Groovy
2004
Jython
1997
Quercus
1999
JVM
OS
Hardware
What is Scala?
Scala Fundamentals
Scala is a programming language for the JVM
• Object oriented paradigm
 Objects, inheritance, polymorphism,
overloading…
• Functional paradigm
 Immutable state, no side-effects
 Application of functions that transform values
• VERY strongly typed
• Interops with Java
var variable
• Evaluated once
• Reassignable multiple times
• Avoid it
// explicit type
var age: Int = 36
// implicit type
var isValid = true
// reassignment
isValid = false
isValid = !isValid
// error: type mismatch
isValid = “hello”
Inferred type
: Boolean
Defining things – Variable values
Scala Fundamentals
var variable
val value
• Evaluated once
• No reassignable (~ final)
// implicit type
val age = 36
val π = 3.14159
val φ = (1 + math.sqrt(5)) / 2
// explicit type
val age: Int = 36
// error: reassignment to val
age = 42
// error: reassignment to val
age = “hello”
Defining things – Constant values
Scala Fundamentals
var variable
val value
lazy val value
• Evaluated once, but only when needed
• No reassignable (~ final)
• Adds small overhead
• There is not a lazy var
// not yet evaluated
lazy val expensiveOp = ackermann(5, 5)
// expensiveOp remains unevaluated
val result =
if (false) expensiveOp
else Int.MaxValue
Defining things – Constant values not evaluated until needed
Scala Fundamentals
var variable
val value
lazy val value
def function[(args)]
• Evaluated every time it’s called
• Allows parameters
• A constant, argumentless def ⇆ val
// no args, const, should be a val
def π = 3.14159
// implicit return type
def polar2Cartesian(r: Double, θ: Double) =
(r * math.cos(θ), r * math.sin(θ))
// explicit return type
def area(r: Double): Double =
π * math.pow(r, 2)
val circleA = area(30)
val circleB = area(40)
val circleC = area(30)
Defining things – Functions
Scala Fundamentals
var variable
val value
lazy val value
def function[(args)]
type typealias
• Type aliases
type ItemType = Int
type Point = (Double, Double)
type PointList = List[Point]
val points: PointList = ...
Defining things – Types
Scala Fundamentals
class MyClass
• Public members by default
• Constructor in declaration
class User(name: String) {
def printInfo() =
println(“Hi, I’m ” + name)
}
val user = new User(“Alfonso”)
user.printInfo() // Hi, I’m Alfonso
Object oriented programming – Class basic definition
Scala Fundamentals
class MyClass
• Public members by default
• Constructor in declaration
• Secondary constructors as this methods
class User(name: String, age: Int) {
// secondary constructor
def this(name: String) = this(name, 0)
def this(age: Int) = this(“Anonymous”, age)
def printInfo() =
println(s“$name is $age years old”)
}
val fullUser = new User(“Alfonso”, 36)
val anonUser = new User(23)
Object oriented programming – Class constructors
Scala Fundamentals
class MyClass
• Public members by default
• Constructor in declaration
• Secondary constructors as this methods
• Fields declared in primary constructor
// args with a val become fields
class User(val name: String, val age: Int) {
def printInfo() =
println(s“$name is $age years old”)
}
val user = new User(“Alfonso”, 36)
println(user.name) // Alfonso
println(user.age) // 36
Object oriented programming – Class fields
Scala Fundamentals
class MyClass
• Public members by default
• Constructor in declaration
• Secondary constructors as this methods
• Fields declared in primary constructor
• Can contain virtually any Scala code
class User(val name: String, val age: Int) {
def printInfo() =
println(s“$name is $age years old”)
if (age >= 18) println(“Adult user created”)
else println(“Child user created”)
}
val adult = new User(“Alfonso”, 36)
// Adult user created
val child = new User(“Teresa”, 12)
// Child user created
Object oriented programming – Class body
Scala Fundamentals
class MyClass
• Public members by default
• Constructor in declaration
• Secondary constructors as this methods
• Fields declared in primary constructor
• Can contain virtually any Scala code
• Classes can extend other classes
• Abstract classes are also available
• case class for pattern matching (later)
abstract class Person {
def name: String // abstract
def printInfo() = println(s“Hello $name!”)
}
case class User(val name: String, val age:Int)
extends Person {
}
val user = new User(“Alfonso”, 36)
user.printInfo() // Hello Alfonso!
Object oriented programming – Class inheritance
Scala Fundamentals
class MyInteger (value: Int = 0) {
def << (x: Int): Int = value * math.pow(10, x)
}
val i = new MyInteger(20)
i.<<(3) // Java style
i << 3 // Infix operator
Scala has no operators, they are method invocations
5 + 10 * 6 // Infix notation
5.+(10.*(6)) // What is actually used
Object oriented programming – Calling class methods: infix operators
Scala Fundamentals
class MyClass
object MyObject
• Syntactic sugar to define singletons
object Database {
def connect = { … }
def fetch = { … }
def close = { … }
}
Database connect
Database fetch
Database close
Object oriented programming – Objects as singletons
Scala Fundamentals
class MyClass
object MyObject
• Syntactic sugar to define singletons
• Used alongwithclasses:companionobjects
class User(val name: String)
object User {
def create(name: String) = new User(name)
def apply(name: String) = create(name)
}
val user1 = new User(“Alfonso”)
val user2 = User.create(“Tulio”)
val user3 = User(“Miguel”) // calls apply()
Object oriented programming – Objects as a class’ companion
Scala Fundamentals
class MyClass
object MyObject
trait MyTrait
• Like an abstract class allowing multiple
inheritance
• Like an interface with implemented
members
• Mixins
abstract class Human {}
trait Engineer(role: String) {
val role: String = “Engineer”
def doWork: String // abstract
}
class User(val id: Int, val name: String)
extends Human with Engineer {
override val doWork = “Developing...”
}
val user = new User(1, “Alfonso”)
user doWork
Object oriented programming – Traits
Scala Fundamentals
class MyClass
object MyObject
trait MyTrait
• Like an abstract class allowing multiple
inheritance
• Like an interface with implemented
members
• Mixins
• Traits can extend from traits and classes
• Multiple inheritance unleashed
trait Engineer(val role: String) {
def doWork: String
}
trait ChickenSexer {
def classifySex(c: Chicken): Boolean
}
class User(id: Int, val name: String)
extends Engineer with ChickenSexer
Object oriented programming – Traits
Scala Fundamentals
Functional programming – Mutability vs. Immutability
Scala Fundamentals
[ ] [ 1 ] [ 1|2 ] [ 1|2|3 ]
1 2 3
time
Functional programming – Mutability vs. Immutability
Scala Fundamentals
[ ] [ 1 ] [ 1|2 ] [ 1|2|3 ]
1 2 3
time
[ ] [ 1 ] [ 1|2 ] [ 1|2|3 ]
1 2 3
val age = 36
// age: Int
val point2D = (5, 3)
// point2D: (Int, Int) = (5, 3)
Functional programming – Higher order functions
Scala Fundamentals
Functional programming – Higher order functions
Scala Fundamentals
val age = 36
// age: Int
val point2D = (5, 3)
// point2D: (Int, Int) = (5, 3)
def areEqual(a: Int, b: Int): Boolean = a == b
// areEqual: (Int, Int) => Boolean
Function type generalized: (inType1, inType2…) => (outType1, outType2…)
Functions are first class citizens:
Functions accept functions as parameters
Functions can return other functions
Functional programming – Functions type examples
Scala Fundamentals
def π = 3.14159
// () => Double
def oppositeInverse(x: Int) = (-x, 1.0 / x)
// Int => (Int, Double)
def swap(p: (Int, Int)): (Int, Int) = (p._2, p._1)
// ((Int, Int)) => (Int, Int)
def userInfo(u: User): (Int, String) = (u id, u name)
// User => (Int, String)
def sayHello(name: String) = println(“Hello ” + name)
// String => Unit (Unit ≈ Scala’s void)
Functional programming – Higher order functions
Scala Fundamentals
def magic(x: Int, f: Int => Int): Int = f(x)
// (Int, Int => Int) => Int
// another way to do the same:
type Int2Int = Int => Int
def magic(x: Int, f: Int2Int): Int = f(x)
// (Int, Int2Int) => Int
Functional programming – Higher order functions
Scala Fundamentals
def magic(x: Int, f: Int => Int): Int = f(x)
// (Int, Int => Int) => Int
def double(i: Int) = i * 2
// Int => Int
magic(5, double)
// double(5)
// 10
Functional programming – Anonymous functions
Scala Fundamentals
def double(i: Int) = i * 2
// Int => Int
// explicit parameter types
(i: Int) => i * 2
// Int => Int
// implicit parameter types
i => i * 2
// ? => ?
def concat(a: Int, b: Int) = a * 10 + b
// (Int, Int) => Int
(a: Int, b: Int) => a * 10 + b
// (Int, Int) => Int
(a, b) => a * 10 + b
// (?, ?) => ?
Functional programming – Anonymous functions
Scala Fundamentals
def magic(x: Int, f: Int => Int): Int = f(x)
// (Int, Int => Int) => Int
magic(5, (i: Int) => i * 2)
// 10
magic(5, (b: Boolean) => if (b) 1 else 0)
// type mismatch
magic(5, i => i * 2)
// 10
magic(5, i => i)
// 5
Functional programming – Anonymous functions
Scala Fundamentals
def double(i: Int): Int = i * 2
// Int => Int
(i: Int) => i * 2
// Int => Int
i => i * 2
// ? => ?
_ * 2
// ? => ?
magic(5, _ * 2)
// 10
def concat(a: Int, b: Int) = a * 10 + b
// (Int, Int) => Int
(a: Int, b: Int) => a * 10 + b
// (Int, Int) => Int
(a, b) => a * 10 + b
// (?, ?) => ?
_ * 10 + _
// (?, ?) => ?
Functional programming – Functional thinking
Scala Fundamentals
List<Integer> list = Arrays.asList(2, 7, 9, 8, 10);
List<Integer> evenList = new ArrayList<Integer>();
for (int number: list) {
if (number % 2 == 0) {
evenList.add(number);
}
}
val list = List(2, 7, 9, 8, 10)
def filterEven(list: List[Int]): List[Int] =
if (list.isEmpty) List()
else if (list.head % 2 == 0)
list.head :: filterEven(list.tail)
else filterEven(list.tail)
val eventList = filterEven(list)
Functional programming – Functional thinking
Scala Fundamentals
List<Integer> list = Arrays.asList(2, 7, 9, 8, 10);
List<Integer> evenList = new ArrayList<Integer>();
for (int number: list) {
if (number % 2 == 0) {
evenList.add(number);
}
}
val list = List(2, 7, 9, 8, 10)
val evenList = list filter (_ % 2 == 0)
Parameterized types – Type parameters
Scala Fundamentals
def packInts(a: Int, b: Int)) = ((a, b), (b, a))
// (Int, Int) => ((Int, Int), (Int, Int))
def pack[T](a: T, b: T)) = ((a, b), (b, a))
// (T, T) => ((T, T), (T, T))
def packBoolInt(a: Bool, b: Int)) = ((a, b), (b, a))
// (Bool, Int) => ((Bool, Int), (Int, Bool))
def packv2[T1, T2](a: T1, b: T2)) = ((a, b), (b, a))
// (T1, T2) => ((T1, T2), (T2, T1))
Parameterized types – Variance, covariance, contravariance
Scala Fundamentals
abstract class Animal {
def talk: String
}
class Cat extends Animal {
def talk: String = “meow”
}
def hugAnimal(a: Animal) = a.talk
val nyanCat = new Cat
hugAnimal(nyanCat) // meow
Animal
Cat
Parameterized types – Variance, covariance, contravariance
Scala Fundamentals
class Zoo[A]
def hugAllAnimals(z: Zoo[Animal]) =
z.foreach(_.talk)
val catZoo: Zoo[Cat] = new Zoo(tom, nyanCat)
hugAllAnimals(catZoo) // ?
Animal
Cat
Zoo [Animal]
Zoo [Cat]
?
Parameterized types – Variance, covariance, contravariance
Scala Fundamentals
Animal
Cat
Zoo [Animal]
Zoo [Cat]
class Zoo[A]
Zoo is invariant
Zoo [Animal]
Zoo [Cat]
class Zoo[+A]
Zoo is covariant
Zoo [Animal]
Zoo [Cat]
class Zoo[-A]
Zoo is contravariant
Pattern matching – Switch on steroids
Scala Fundamentals
def whatIs(object: Any): String = {
object match {
case 0 | “” | false => “empty”
case i: Int if i < 0 => “negative”
case i: Int => “integer”
case 5 => “5 value”
case (_: Int, _: Int) => “ints pair”
case (_, _) => “pair”
case _: Double => “double”
case User(“Xavi”, _) => “It’s Xavi!”
case _ => “unknown”
}
}
whatIs(-13) // negative
whatIs(5) // integer
whatIs((“one”, 1)) // pair
whatIs(0) // empty
whatIs(new User(“Xavi”, 32)) // It’s Xavi!
Call by value vs. Call by name – Call by value
Scala Fundamentals
Call by value evaluates all arguments before a function call
def choose(cond: Boolean, output: Integer) =
if (cond) output else Int.MaxValue
choose(false, ackermann(5, 5))
→ choose(false, ackermann(4, ackermann(5, 4))
→ choose(false, ackermann(4, ackermann(4, ackermann(5, 3)))
→ choose(false, ackermann(4, ackermann(4, ackermann(4, ackermann(5, 2)))
…
→→ if (false) ackermann_value else Int.MaxValue
→ Int.MaxValue // so ackermann() was not necessary after all
Call by value vs. Call by name – Call by name
Scala Fundamentals
Call by name delays the evaluation until it’s needed
def choose(cond: Boolean, output: => Integer) =
if (cond) output
else Int.MaxValue
choose(false, ackermann(5, 5))
→ if (false) ackermann(5, 5)
else Int.MaxValue
→ Int.MaxValue // ackermann was not calculated
It’s a lazy value. Beware if you need side-effects (need to call the function)
Partially applied functions – Some arguments provided
Scala Fundamentals
def sum(a: Int, b: Int, c: Int) = a + b + c
val sum5 = sum(5, _: Int, _: Int)
// (Int, Int) => Int
// ≈ def sum5(b: Int, c: Int): Int = 5 + b + c
val sum5Then10 = sum5(10, _: Int)
// Int => Int
// ≈ def sum5Then10(c: Int): Int = 5 + 10 + c
sum(10, 5, 1) // 16
sum5(4, 1) // 10
sum5Then10(-1) // 14
Partial functions – Functions not defined for every possible input value
Scala Fundamentals
val isEvenOrOdd: PartialFunction[Int, String] = {
case x if x % 2 == 0 => “even”
case x if x % 2 == 1 => “odd”
}
// ≈ def isEvenOrOdd(x: Int): String = { x match … }
isEvenOrOdd(8) // even
isEvenOrOdd(5) // odd
isEvenOrOdd(-3) // ?
isEvenOrOdd.isDefinedAt(8) // true
isEvenOrOdd.isDefinedAt(5) // true
isEvenOrOdd.isDefinedAt(-3) // false
Currying – Arguments groups
Scala Fundamentals
def magic(f: Int => Int, x: Int): Int = f(x)
def magicCurry(f: Int => Int)(x: Int): Int = f(x)
// (Int => Int)(Int) => Int
val doubleFunc = magicCurry(_ * 2) _
// Int => Int
// ≈ def doubleFunc(x: Int): Int = x * 2
magic(_ * 2, 3) // 6
magicCurry(_ * 2)(3) // 6
doubleFunc(3) // 6
Implicits – Automatically provided values
Scala Fundamentals
val list = List(5, 2, 9, 6, 0, 1)
implicit def compare(a: Int, b: Int): Int
def sort(l: List[Int])(implicit comparator: (Int, Int) => Int) = ...
sort(list)(compare)
sort(list) // Looks in the scope for an implicit function of type (Int, Int) => Int
Java vs. Scala – Conciseness, verbosity
Scala Fundamentals
public class User {
private String name;
private List<Order> orders;
public User() {
orders = new ArrayList<Order>();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Order> getOrders() {
return orders;
}
public void setOrders(List<Order> orders) {
this.orders = orders;
}
}
class User {
var name: String = _
var orders: List[Order] = Nil
}
Java vs. Scala
Scala Fundamentals
def ++[B >: A, That](that: TraversableOnce [B])(implicit bf: CanBuildFrom[List [A], B, That]): That
def retry(noTimes: Int)(block: => Future[T]): Future[T] = {
val ns = (1 to noTimes).toList
val attempts: = ns.map(_ => () => block)
val failed = Future.failed(new Exception)
val result = attempts.foldRight(() => failed)
((block, a) => () => { block() fallbackTo { a() } })
result ()
}
Java vs. Scala – The not-so-good news
Scala Fundamentals
• Tough learning curve
 Classic OOP/Imperative/Procedural-minded programmers
 Also, new concepts that are no familiar to many programmers
 Multiple right ways to do the same thing
 Language infix operators abuse (foo ~-~> bar <=> baz())
• Sloooow compilation
• Not backward compatible (major versions)
• IDE support sucks improving
• JVM legacy issues
References
Scala Fundamentals
http://www.scala-lang.org
http://www.simplyscala.com
http://www.coursera.org/course/progfun
http://twitter.github.io/scala_school
Scala fundamentals

More Related Content

What's hot

A Brief Intro to Scala
A Brief Intro to ScalaA Brief Intro to Scala
A Brief Intro to ScalaTim Underwood
 
ES6 presentation
ES6 presentationES6 presentation
ES6 presentationritika1
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions WebStackAcademy
 
Type script - advanced usage and practices
Type script  - advanced usage and practicesType script  - advanced usage and practices
Type script - advanced usage and practicesIwan van der Kleijn
 
The Power of Composition
The Power of CompositionThe Power of Composition
The Power of CompositionScott Wlaschin
 
Pipeline oriented programming
Pipeline oriented programmingPipeline oriented programming
Pipeline oriented programmingScott Wlaschin
 
From Java 11 to 17 and beyond.pdf
From Java 11 to 17 and beyond.pdfFrom Java 11 to 17 and beyond.pdf
From Java 11 to 17 and beyond.pdfJosé Paumard
 
JavaScript - Chapter 4 - Types and Statements
 JavaScript - Chapter 4 - Types and Statements JavaScript - Chapter 4 - Types and Statements
JavaScript - Chapter 4 - Types and StatementsWebStackAcademy
 
Domain Driven Design with the F# type System -- NDC London 2013
Domain Driven Design with the F# type System -- NDC London 2013Domain Driven Design with the F# type System -- NDC London 2013
Domain Driven Design with the F# type System -- NDC London 2013Scott Wlaschin
 
Functional Programming Patterns (NDC London 2014)
Functional Programming Patterns (NDC London 2014)Functional Programming Patterns (NDC London 2014)
Functional Programming Patterns (NDC London 2014)Scott Wlaschin
 
Java Foundations: Objects and Classes
Java Foundations: Objects and ClassesJava Foundations: Objects and Classes
Java Foundations: Objects and ClassesSvetlin Nakov
 
A Prelude of Purity: Scaling Back ZIO
A Prelude of Purity: Scaling Back ZIOA Prelude of Purity: Scaling Back ZIO
A Prelude of Purity: Scaling Back ZIOJorge Vásquez
 
Functional Domain Modeling - The ZIO 2 Way
Functional Domain Modeling - The ZIO 2 WayFunctional Domain Modeling - The ZIO 2 Way
Functional Domain Modeling - The ZIO 2 WayDebasish Ghosh
 
Clean pragmatic architecture @ devflix
Clean pragmatic architecture @ devflixClean pragmatic architecture @ devflix
Clean pragmatic architecture @ devflixVictor Rentea
 
Functional Programming 101 with Scala and ZIO @FunctionalWorld
Functional Programming 101 with Scala and ZIO @FunctionalWorldFunctional Programming 101 with Scala and ZIO @FunctionalWorld
Functional Programming 101 with Scala and ZIO @FunctionalWorldJorge Vásquez
 

What's hot (20)

A Brief Intro to Scala
A Brief Intro to ScalaA Brief Intro to Scala
A Brief Intro to Scala
 
ES6 presentation
ES6 presentationES6 presentation
ES6 presentation
 
Lesson 6 php if...else...elseif statements
Lesson 6   php if...else...elseif statementsLesson 6   php if...else...elseif statements
Lesson 6 php if...else...elseif statements
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 
Type script - advanced usage and practices
Type script  - advanced usage and practicesType script  - advanced usage and practices
Type script - advanced usage and practices
 
Scala functions
Scala functionsScala functions
Scala functions
 
The Power of Composition
The Power of CompositionThe Power of Composition
The Power of Composition
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Pipeline oriented programming
Pipeline oriented programmingPipeline oriented programming
Pipeline oriented programming
 
From Java 11 to 17 and beyond.pdf
From Java 11 to 17 and beyond.pdfFrom Java 11 to 17 and beyond.pdf
From Java 11 to 17 and beyond.pdf
 
JavaScript - Chapter 4 - Types and Statements
 JavaScript - Chapter 4 - Types and Statements JavaScript - Chapter 4 - Types and Statements
JavaScript - Chapter 4 - Types and Statements
 
Domain Driven Design with the F# type System -- NDC London 2013
Domain Driven Design with the F# type System -- NDC London 2013Domain Driven Design with the F# type System -- NDC London 2013
Domain Driven Design with the F# type System -- NDC London 2013
 
ECMA Script
ECMA ScriptECMA Script
ECMA Script
 
Functional Programming Patterns (NDC London 2014)
Functional Programming Patterns (NDC London 2014)Functional Programming Patterns (NDC London 2014)
Functional Programming Patterns (NDC London 2014)
 
Java Foundations: Objects and Classes
Java Foundations: Objects and ClassesJava Foundations: Objects and Classes
Java Foundations: Objects and Classes
 
A Prelude of Purity: Scaling Back ZIO
A Prelude of Purity: Scaling Back ZIOA Prelude of Purity: Scaling Back ZIO
A Prelude of Purity: Scaling Back ZIO
 
Functional Domain Modeling - The ZIO 2 Way
Functional Domain Modeling - The ZIO 2 WayFunctional Domain Modeling - The ZIO 2 Way
Functional Domain Modeling - The ZIO 2 Way
 
Clean pragmatic architecture @ devflix
Clean pragmatic architecture @ devflixClean pragmatic architecture @ devflix
Clean pragmatic architecture @ devflix
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
 
Functional Programming 101 with Scala and ZIO @FunctionalWorld
Functional Programming 101 with Scala and ZIO @FunctionalWorldFunctional Programming 101 with Scala and ZIO @FunctionalWorld
Functional Programming 101 with Scala and ZIO @FunctionalWorld
 

Similar to Scala fundamentals

A Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java DevelopersA Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java DevelopersMiles Sabin
 
Miles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java DevelopersMiles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java DevelopersSkills Matter
 
BCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java DevelopersBCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java DevelopersMiles Sabin
 
An Introduction to Scala for Java Developers
An Introduction to Scala for Java DevelopersAn Introduction to Scala for Java Developers
An Introduction to Scala for Java DevelopersMiles Sabin
 
A (too) Short Introduction to Scala
A (too) Short Introduction to ScalaA (too) Short Introduction to Scala
A (too) Short Introduction to ScalaRiccardo Cardin
 
Testing for share
Testing for share Testing for share
Testing for share Rajeev Mehta
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Languageleague
 
Oop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalOop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalMichael Stal
 
Intro to scala
Intro to scalaIntro to scala
Intro to scalaJoe Zulli
 
All Aboard The Scala-to-PureScript Express!
All Aboard The Scala-to-PureScript Express!All Aboard The Scala-to-PureScript Express!
All Aboard The Scala-to-PureScript Express!John De Goes
 
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
 
Scala for Java Programmers
Scala for Java ProgrammersScala for Java Programmers
Scala for Java ProgrammersEric Pederson
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecLoïc Descotte
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scalapramode_ce
 

Similar to Scala fundamentals (20)

Scala for curious
Scala for curiousScala for curious
Scala for curious
 
Scala ntnu
Scala ntnuScala ntnu
Scala ntnu
 
A Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java DevelopersA Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java Developers
 
Miles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java DevelopersMiles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java Developers
 
Scala - brief intro
Scala - brief introScala - brief intro
Scala - brief intro
 
BCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java DevelopersBCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java Developers
 
An Introduction to Scala for Java Developers
An Introduction to Scala for Java DevelopersAn Introduction to Scala for Java Developers
An Introduction to Scala for Java Developers
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
A (too) Short Introduction to Scala
A (too) Short Introduction to ScalaA (too) Short Introduction to Scala
A (too) Short Introduction to Scala
 
C# programming
C# programming C# programming
C# programming
 
Testing for share
Testing for share Testing for share
Testing for share
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Language
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Oop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalOop2010 Scala Presentation Stal
Oop2010 Scala Presentation Stal
 
Intro to scala
Intro to scalaIntro to scala
Intro to scala
 
All Aboard The Scala-to-PureScript Express!
All Aboard The Scala-to-PureScript Express!All Aboard The Scala-to-PureScript Express!
All Aboard The Scala-to-PureScript Express!
 
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
 
Scala for Java Programmers
Scala for Java ProgrammersScala for Java Programmers
Scala for Java Programmers
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar Prokopec
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scala
 

Recently uploaded

Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noidabntitsolutionsrishis
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 

Recently uploaded (20)

Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Advantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your BusinessAdvantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your Business
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 

Scala fundamentals

  • 2. Alfonso Ruzafa Senior Software Engineer @ Softonic About me Scala Fundamentals
  • 3. Java and the JVM Scala Fundamentals Java 1995 Scala 2003 Clojure 2007 JRuby 2001 Groovy 2004 Jython 1997 Quercus 1999 JVM OS Hardware
  • 4. What is Scala? Scala Fundamentals Scala is a programming language for the JVM • Object oriented paradigm  Objects, inheritance, polymorphism, overloading… • Functional paradigm  Immutable state, no side-effects  Application of functions that transform values • VERY strongly typed • Interops with Java
  • 5. var variable • Evaluated once • Reassignable multiple times • Avoid it // explicit type var age: Int = 36 // implicit type var isValid = true // reassignment isValid = false isValid = !isValid // error: type mismatch isValid = “hello” Inferred type : Boolean Defining things – Variable values Scala Fundamentals
  • 6. var variable val value • Evaluated once • No reassignable (~ final) // implicit type val age = 36 val π = 3.14159 val φ = (1 + math.sqrt(5)) / 2 // explicit type val age: Int = 36 // error: reassignment to val age = 42 // error: reassignment to val age = “hello” Defining things – Constant values Scala Fundamentals
  • 7. var variable val value lazy val value • Evaluated once, but only when needed • No reassignable (~ final) • Adds small overhead • There is not a lazy var // not yet evaluated lazy val expensiveOp = ackermann(5, 5) // expensiveOp remains unevaluated val result = if (false) expensiveOp else Int.MaxValue Defining things – Constant values not evaluated until needed Scala Fundamentals
  • 8. var variable val value lazy val value def function[(args)] • Evaluated every time it’s called • Allows parameters • A constant, argumentless def ⇆ val // no args, const, should be a val def π = 3.14159 // implicit return type def polar2Cartesian(r: Double, θ: Double) = (r * math.cos(θ), r * math.sin(θ)) // explicit return type def area(r: Double): Double = π * math.pow(r, 2) val circleA = area(30) val circleB = area(40) val circleC = area(30) Defining things – Functions Scala Fundamentals
  • 9. var variable val value lazy val value def function[(args)] type typealias • Type aliases type ItemType = Int type Point = (Double, Double) type PointList = List[Point] val points: PointList = ... Defining things – Types Scala Fundamentals
  • 10. class MyClass • Public members by default • Constructor in declaration class User(name: String) { def printInfo() = println(“Hi, I’m ” + name) } val user = new User(“Alfonso”) user.printInfo() // Hi, I’m Alfonso Object oriented programming – Class basic definition Scala Fundamentals
  • 11. class MyClass • Public members by default • Constructor in declaration • Secondary constructors as this methods class User(name: String, age: Int) { // secondary constructor def this(name: String) = this(name, 0) def this(age: Int) = this(“Anonymous”, age) def printInfo() = println(s“$name is $age years old”) } val fullUser = new User(“Alfonso”, 36) val anonUser = new User(23) Object oriented programming – Class constructors Scala Fundamentals
  • 12. class MyClass • Public members by default • Constructor in declaration • Secondary constructors as this methods • Fields declared in primary constructor // args with a val become fields class User(val name: String, val age: Int) { def printInfo() = println(s“$name is $age years old”) } val user = new User(“Alfonso”, 36) println(user.name) // Alfonso println(user.age) // 36 Object oriented programming – Class fields Scala Fundamentals
  • 13. class MyClass • Public members by default • Constructor in declaration • Secondary constructors as this methods • Fields declared in primary constructor • Can contain virtually any Scala code class User(val name: String, val age: Int) { def printInfo() = println(s“$name is $age years old”) if (age >= 18) println(“Adult user created”) else println(“Child user created”) } val adult = new User(“Alfonso”, 36) // Adult user created val child = new User(“Teresa”, 12) // Child user created Object oriented programming – Class body Scala Fundamentals
  • 14. class MyClass • Public members by default • Constructor in declaration • Secondary constructors as this methods • Fields declared in primary constructor • Can contain virtually any Scala code • Classes can extend other classes • Abstract classes are also available • case class for pattern matching (later) abstract class Person { def name: String // abstract def printInfo() = println(s“Hello $name!”) } case class User(val name: String, val age:Int) extends Person { } val user = new User(“Alfonso”, 36) user.printInfo() // Hello Alfonso! Object oriented programming – Class inheritance Scala Fundamentals
  • 15. class MyInteger (value: Int = 0) { def << (x: Int): Int = value * math.pow(10, x) } val i = new MyInteger(20) i.<<(3) // Java style i << 3 // Infix operator Scala has no operators, they are method invocations 5 + 10 * 6 // Infix notation 5.+(10.*(6)) // What is actually used Object oriented programming – Calling class methods: infix operators Scala Fundamentals
  • 16. class MyClass object MyObject • Syntactic sugar to define singletons object Database { def connect = { … } def fetch = { … } def close = { … } } Database connect Database fetch Database close Object oriented programming – Objects as singletons Scala Fundamentals
  • 17. class MyClass object MyObject • Syntactic sugar to define singletons • Used alongwithclasses:companionobjects class User(val name: String) object User { def create(name: String) = new User(name) def apply(name: String) = create(name) } val user1 = new User(“Alfonso”) val user2 = User.create(“Tulio”) val user3 = User(“Miguel”) // calls apply() Object oriented programming – Objects as a class’ companion Scala Fundamentals
  • 18. class MyClass object MyObject trait MyTrait • Like an abstract class allowing multiple inheritance • Like an interface with implemented members • Mixins abstract class Human {} trait Engineer(role: String) { val role: String = “Engineer” def doWork: String // abstract } class User(val id: Int, val name: String) extends Human with Engineer { override val doWork = “Developing...” } val user = new User(1, “Alfonso”) user doWork Object oriented programming – Traits Scala Fundamentals
  • 19. class MyClass object MyObject trait MyTrait • Like an abstract class allowing multiple inheritance • Like an interface with implemented members • Mixins • Traits can extend from traits and classes • Multiple inheritance unleashed trait Engineer(val role: String) { def doWork: String } trait ChickenSexer { def classifySex(c: Chicken): Boolean } class User(id: Int, val name: String) extends Engineer with ChickenSexer Object oriented programming – Traits Scala Fundamentals
  • 20. Functional programming – Mutability vs. Immutability Scala Fundamentals [ ] [ 1 ] [ 1|2 ] [ 1|2|3 ] 1 2 3 time
  • 21. Functional programming – Mutability vs. Immutability Scala Fundamentals [ ] [ 1 ] [ 1|2 ] [ 1|2|3 ] 1 2 3 time [ ] [ 1 ] [ 1|2 ] [ 1|2|3 ] 1 2 3
  • 22. val age = 36 // age: Int val point2D = (5, 3) // point2D: (Int, Int) = (5, 3) Functional programming – Higher order functions Scala Fundamentals
  • 23. Functional programming – Higher order functions Scala Fundamentals val age = 36 // age: Int val point2D = (5, 3) // point2D: (Int, Int) = (5, 3) def areEqual(a: Int, b: Int): Boolean = a == b // areEqual: (Int, Int) => Boolean Function type generalized: (inType1, inType2…) => (outType1, outType2…) Functions are first class citizens: Functions accept functions as parameters Functions can return other functions
  • 24. Functional programming – Functions type examples Scala Fundamentals def π = 3.14159 // () => Double def oppositeInverse(x: Int) = (-x, 1.0 / x) // Int => (Int, Double) def swap(p: (Int, Int)): (Int, Int) = (p._2, p._1) // ((Int, Int)) => (Int, Int) def userInfo(u: User): (Int, String) = (u id, u name) // User => (Int, String) def sayHello(name: String) = println(“Hello ” + name) // String => Unit (Unit ≈ Scala’s void)
  • 25. Functional programming – Higher order functions Scala Fundamentals def magic(x: Int, f: Int => Int): Int = f(x) // (Int, Int => Int) => Int // another way to do the same: type Int2Int = Int => Int def magic(x: Int, f: Int2Int): Int = f(x) // (Int, Int2Int) => Int
  • 26. Functional programming – Higher order functions Scala Fundamentals def magic(x: Int, f: Int => Int): Int = f(x) // (Int, Int => Int) => Int def double(i: Int) = i * 2 // Int => Int magic(5, double) // double(5) // 10
  • 27. Functional programming – Anonymous functions Scala Fundamentals def double(i: Int) = i * 2 // Int => Int // explicit parameter types (i: Int) => i * 2 // Int => Int // implicit parameter types i => i * 2 // ? => ? def concat(a: Int, b: Int) = a * 10 + b // (Int, Int) => Int (a: Int, b: Int) => a * 10 + b // (Int, Int) => Int (a, b) => a * 10 + b // (?, ?) => ?
  • 28. Functional programming – Anonymous functions Scala Fundamentals def magic(x: Int, f: Int => Int): Int = f(x) // (Int, Int => Int) => Int magic(5, (i: Int) => i * 2) // 10 magic(5, (b: Boolean) => if (b) 1 else 0) // type mismatch magic(5, i => i * 2) // 10 magic(5, i => i) // 5
  • 29. Functional programming – Anonymous functions Scala Fundamentals def double(i: Int): Int = i * 2 // Int => Int (i: Int) => i * 2 // Int => Int i => i * 2 // ? => ? _ * 2 // ? => ? magic(5, _ * 2) // 10 def concat(a: Int, b: Int) = a * 10 + b // (Int, Int) => Int (a: Int, b: Int) => a * 10 + b // (Int, Int) => Int (a, b) => a * 10 + b // (?, ?) => ? _ * 10 + _ // (?, ?) => ?
  • 30. Functional programming – Functional thinking Scala Fundamentals List<Integer> list = Arrays.asList(2, 7, 9, 8, 10); List<Integer> evenList = new ArrayList<Integer>(); for (int number: list) { if (number % 2 == 0) { evenList.add(number); } } val list = List(2, 7, 9, 8, 10) def filterEven(list: List[Int]): List[Int] = if (list.isEmpty) List() else if (list.head % 2 == 0) list.head :: filterEven(list.tail) else filterEven(list.tail) val eventList = filterEven(list)
  • 31. Functional programming – Functional thinking Scala Fundamentals List<Integer> list = Arrays.asList(2, 7, 9, 8, 10); List<Integer> evenList = new ArrayList<Integer>(); for (int number: list) { if (number % 2 == 0) { evenList.add(number); } } val list = List(2, 7, 9, 8, 10) val evenList = list filter (_ % 2 == 0)
  • 32. Parameterized types – Type parameters Scala Fundamentals def packInts(a: Int, b: Int)) = ((a, b), (b, a)) // (Int, Int) => ((Int, Int), (Int, Int)) def pack[T](a: T, b: T)) = ((a, b), (b, a)) // (T, T) => ((T, T), (T, T)) def packBoolInt(a: Bool, b: Int)) = ((a, b), (b, a)) // (Bool, Int) => ((Bool, Int), (Int, Bool)) def packv2[T1, T2](a: T1, b: T2)) = ((a, b), (b, a)) // (T1, T2) => ((T1, T2), (T2, T1))
  • 33. Parameterized types – Variance, covariance, contravariance Scala Fundamentals abstract class Animal { def talk: String } class Cat extends Animal { def talk: String = “meow” } def hugAnimal(a: Animal) = a.talk val nyanCat = new Cat hugAnimal(nyanCat) // meow Animal Cat
  • 34. Parameterized types – Variance, covariance, contravariance Scala Fundamentals class Zoo[A] def hugAllAnimals(z: Zoo[Animal]) = z.foreach(_.talk) val catZoo: Zoo[Cat] = new Zoo(tom, nyanCat) hugAllAnimals(catZoo) // ? Animal Cat Zoo [Animal] Zoo [Cat] ?
  • 35. Parameterized types – Variance, covariance, contravariance Scala Fundamentals Animal Cat Zoo [Animal] Zoo [Cat] class Zoo[A] Zoo is invariant Zoo [Animal] Zoo [Cat] class Zoo[+A] Zoo is covariant Zoo [Animal] Zoo [Cat] class Zoo[-A] Zoo is contravariant
  • 36. Pattern matching – Switch on steroids Scala Fundamentals def whatIs(object: Any): String = { object match { case 0 | “” | false => “empty” case i: Int if i < 0 => “negative” case i: Int => “integer” case 5 => “5 value” case (_: Int, _: Int) => “ints pair” case (_, _) => “pair” case _: Double => “double” case User(“Xavi”, _) => “It’s Xavi!” case _ => “unknown” } } whatIs(-13) // negative whatIs(5) // integer whatIs((“one”, 1)) // pair whatIs(0) // empty whatIs(new User(“Xavi”, 32)) // It’s Xavi!
  • 37. Call by value vs. Call by name – Call by value Scala Fundamentals Call by value evaluates all arguments before a function call def choose(cond: Boolean, output: Integer) = if (cond) output else Int.MaxValue choose(false, ackermann(5, 5)) → choose(false, ackermann(4, ackermann(5, 4)) → choose(false, ackermann(4, ackermann(4, ackermann(5, 3))) → choose(false, ackermann(4, ackermann(4, ackermann(4, ackermann(5, 2))) … →→ if (false) ackermann_value else Int.MaxValue → Int.MaxValue // so ackermann() was not necessary after all
  • 38. Call by value vs. Call by name – Call by name Scala Fundamentals Call by name delays the evaluation until it’s needed def choose(cond: Boolean, output: => Integer) = if (cond) output else Int.MaxValue choose(false, ackermann(5, 5)) → if (false) ackermann(5, 5) else Int.MaxValue → Int.MaxValue // ackermann was not calculated It’s a lazy value. Beware if you need side-effects (need to call the function)
  • 39. Partially applied functions – Some arguments provided Scala Fundamentals def sum(a: Int, b: Int, c: Int) = a + b + c val sum5 = sum(5, _: Int, _: Int) // (Int, Int) => Int // ≈ def sum5(b: Int, c: Int): Int = 5 + b + c val sum5Then10 = sum5(10, _: Int) // Int => Int // ≈ def sum5Then10(c: Int): Int = 5 + 10 + c sum(10, 5, 1) // 16 sum5(4, 1) // 10 sum5Then10(-1) // 14
  • 40. Partial functions – Functions not defined for every possible input value Scala Fundamentals val isEvenOrOdd: PartialFunction[Int, String] = { case x if x % 2 == 0 => “even” case x if x % 2 == 1 => “odd” } // ≈ def isEvenOrOdd(x: Int): String = { x match … } isEvenOrOdd(8) // even isEvenOrOdd(5) // odd isEvenOrOdd(-3) // ? isEvenOrOdd.isDefinedAt(8) // true isEvenOrOdd.isDefinedAt(5) // true isEvenOrOdd.isDefinedAt(-3) // false
  • 41. Currying – Arguments groups Scala Fundamentals def magic(f: Int => Int, x: Int): Int = f(x) def magicCurry(f: Int => Int)(x: Int): Int = f(x) // (Int => Int)(Int) => Int val doubleFunc = magicCurry(_ * 2) _ // Int => Int // ≈ def doubleFunc(x: Int): Int = x * 2 magic(_ * 2, 3) // 6 magicCurry(_ * 2)(3) // 6 doubleFunc(3) // 6
  • 42. Implicits – Automatically provided values Scala Fundamentals val list = List(5, 2, 9, 6, 0, 1) implicit def compare(a: Int, b: Int): Int def sort(l: List[Int])(implicit comparator: (Int, Int) => Int) = ... sort(list)(compare) sort(list) // Looks in the scope for an implicit function of type (Int, Int) => Int
  • 43. Java vs. Scala – Conciseness, verbosity Scala Fundamentals public class User { private String name; private List<Order> orders; public User() { orders = new ArrayList<Order>(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Order> getOrders() { return orders; } public void setOrders(List<Order> orders) { this.orders = orders; } } class User { var name: String = _ var orders: List[Order] = Nil }
  • 44. Java vs. Scala Scala Fundamentals def ++[B >: A, That](that: TraversableOnce [B])(implicit bf: CanBuildFrom[List [A], B, That]): That def retry(noTimes: Int)(block: => Future[T]): Future[T] = { val ns = (1 to noTimes).toList val attempts: = ns.map(_ => () => block) val failed = Future.failed(new Exception) val result = attempts.foldRight(() => failed) ((block, a) => () => { block() fallbackTo { a() } }) result () }
  • 45. Java vs. Scala – The not-so-good news Scala Fundamentals • Tough learning curve  Classic OOP/Imperative/Procedural-minded programmers  Also, new concepts that are no familiar to many programmers  Multiple right ways to do the same thing  Language infix operators abuse (foo ~-~> bar <=> baz()) • Sloooow compilation • Not backward compatible (major versions) • IDE support sucks improving • JVM legacy issues