SlideShare a Scribd company logo
1 of 35
SHORT INTRODUCTION TO SCALA
INGEGNERIA DEL SOFTWARE
Università degli Studi di Padova
Dipartimento di Matematica
Corso di Laurea in Informatica, A.A. 2014 – 2015
rcardin@math.unipd.it
Ingegneria del software mod. B
INTRODUCTION
 Object/functional programming language
 Based on Java Virtual Machine
 Full interoperability with Java libraries
 Pure object orientation (no primitive types)
 Every function is a value
 High order functions (lambdas)
 Promoting immutability
 Currying
 Pattern matching
 Statically typed
 Intelligent type system
 Extensible
2Riccardo Cardin
2003:
internal
use to
EPFL
2004:
version
1.0
2006:
version
2.0
2007:
the
«growth
year»
2014: version
2.11 (JVM 8)
Ingegneria del software mod. B
INTRODUCTION
 Language main features
 Read-Eval-Print-Loop (REPL) interpreter
 An interactive shell to execute statements "on the fly"
 Variables
 UML syntax -> name : type
 val x: Int = 1 + 1 Immutable
 lazy val x: Int = 1 + 1 Immutable and lazy
 var x: Int = 1 + 1 Mutable
 def x: Int = 1 + 1 Executed every time
 Very smart type inferer val x = 1 x: Int
 No semicolon
 Blocks, blocks everywhere...
3Riccardo Cardin
Ingegneria del software mod. B
INTRODUCTION
 Language main features
 Every statement is a "pure" expression
 No return statement needed!
 The if-else statement is a value
 Imperative for statement is not supported
 Use the for-yield statement instead
 Every char can be used to define names (%$#@?...)
 Full integration with every Java library
4Riccardo Cardin
val y = 4
// x has type String
val x = if (y < 3) "minor" else "greater"
// Returns an array of integers between 2 and 101
for (i <- (1 to 100)) yield i + 1
Ingegneria del software mod. B
OBJECT ORIENTED SCALA
 Everything is an object
5Riccardo Cardin
Values
References (Scala and Java)
Ingegneria del software mod. B
OBJECT ORIENTED SCALA
 Classes
6Riccardo Cardin
class Person(val name: String, val surname: String,
val birth: Date) {
// Main Ctor body here
// Every secondary Ctor must call the main Ctor as
// first statement (very restrictive)
def this(name: String) = this(name, "", null)
def age(): Int = { /* return the age */ }
// Override intention must be declared
override def toString = s"My name is $name $surname"
}
Ingegneria del software mod. B
OBJECT ORIENTED SCALA
 Classes
 Main constructor is declared within Class name
 Ctor body is made of statements in class body
 Accessor methods are build automatically
 val immutable value, getter only
 var  mutable value, getter and setter
 def defines methods (and functions too...)
 Use equal "=" iff the method is a function
 No public class
 You can have more than a class into a .scala file
 No static methods (wait a minute...)
7Riccardo Cardin
Ingegneria del software mod. B
OBJECT ORIENTED SCALA
 ...so let’s introduce objects notation!
8Riccardo Cardin
Ingegneria del software mod. B
OBJECT ORIENTED SCALA
 Object
 Native Singleton pattern implementation
 There can be only one instance of ChuckNorris
 Companion object
 An object called as an existing class
 Companion object’s methods are static method of the class
9Riccardo Cardin
object ChuckNorris extends Person("Chuck Norris") {
// This is a static method
def roundhouseKick = "Roundhouse"
}
println ChuckNorris.roundhouseKick
class Person(val name: String, val surname: String)
object Person { // Companion object
def someStupidStaticMethod = "This is a static method"
}
println Person.someStupidStaticMethod
Ingegneria del software mod. B
OBJECT ORIENTED SCALA
 Object
 Companion objects implement also Factory Method
pattern natively
 Every apply method creates an instance of the class
 Syntactic sugar: Class.apply(...)  Class(...)
 It’s like a class application (...function anyone?)
10Riccardo Cardin
class Person(val name: String,
val surname: String)
object Person {
// Factory method
def apply(n: String, s: String) =
new Person(n, s)
}
// Application of the class will return a new instance
val ricky = Person("Riccardo", "Cardin") // No new operator
There can be
more than
one apply!!!!
Ingegneria del software mod. B
OBJECT ORIENTED SCALA
 Object
 Used also to define the entry point of an application
 The Java way
 The Scala way
11Riccardo Cardin
object Hello extends App {
// All the code in the object’s ctor body will be
// executed as our application.
// Arguments are accessible via an ‘args’ variable,
// available in the App trait
println("Hello World!")
}
object Hello {
// Every object containing a main method could be
// Executed. There are no restriction on file name
def main(args: Array[String]) {
println("Hello World!")
}
}
Ingegneria del software mod. B
OBJECT ORIENTED SCALA
 Abstract classes
 Also defines a main constructor
 It has to be called from the derived classes’ main Ctor
12Riccardo Cardin
abstract class Animal(val species: String) {
def walk // Abstract method
}
class Person(val name: String,
val surname: String)
// Calling main ctor directly in class
// definition
extends Animal("Human") {
def walk = { /* Do some stuff */ }
}
val ricky = new Person("Riccardo", "Cardin")
Ingegneria del software mod. B
OBJECT ORIENTED SCALA
 Traits: interfaces or abstract classes?
 Like interfaces, they have not a constructor
 To implement a single trait, use extends
 Otherwise, use with (mixin)
 But, they can have methods with implementation
and attributes
 Similar (but more powerful) to default methods in Java 8
 Multiple inheritance problem
 Class linearization: use the rightmost type (more or less...)
13Riccardo Cardin
trait A { override def toString = "I’m A" }
trait B { override def toString = "I’m B" }
trait C { override def toString = "I’m C" }
class D extends A with B, C
new D().toString // Prints ‘I’m C’
Ingegneria del software mod. B
OBJECT ORIENTED SCALA
 Traits
 Like an interface
 Like an abstract class
14Riccardo Cardin
// No implementation at all
trait Sorter {
def sort(list: List[Int]): List[Int]
}
class QuickSorter extends Sorter {
def sort(list: List[Int]) = { /* Do something */ }
}
trait AbstractDao { // No Ctor (wtf!)
var pool: Array[Datasource]
def getPool = pool // Implemented method
def save // abstract method
def delete // abstract method
}
class UserDao extends AbstractDao {
// Has to implement save and delete
}
Ingegneria del software mod. B
OBJECT ORIENTED SCALA
 Traits
 Inside a mixin the super refers to the previuos trait
in the linearized-type
 Used to implement the Decorator pattern natively
15Riccardo Cardin
trait Employee {
// ...
def whois(): String
}
class Engineer(name: String, office: String) extends Employee
trait ProjectManager extends Employee {
abstract override def whois() {
// super refers to the previuos trait on the left
super.whois(buffer)
println("and I’am a project manager too!")
}
}
new Engineer("Riccardo","Development") with ProjectManager
Statically binded
Decorator pattern
Ingegneria del software mod. B
OBJECT ORIENTED SCALA
 Traits and Objects
 Using the companion object of a trait, we can
implement the Abstract Factory pattern
16Riccardo Cardin
// Abstract factory
trait AbstractFactory
// Private classes’ scope is limited to the
// definition file
private class ConcreteFactory1 extends AbstractFactory { /* ... */ }
private class ConcreteFactory2 extends AbstractFactory { /* ... */ }
object AbstractFactory{
def apply(kind: String) =
kind match {
case “factory1" => new ConcreteFactory1 ()
case “factory2" => new ConcreteFactory1 () factory 2
}
}
val factory = AbstractFactory("factory1")
Ingegneria del software mod. B
OBJECT ORIENTED SCALA
 Case classes and pattern matching
 Scala has a built in pattern matching mechanism
 Case class: regular class that can be pattern-matched
 Turns all ctor params into val (immutable) constants
 Generates a companion object w/ apply methods
 Generates toString, equals and hashCode methods
properly
17Riccardo Cardin
def matchTest(x: Int): String = x match {
case 1 => "one"
case _ => "many"
}
println(matchTest(3)) // Prints ‘many’
Case clauses are
evaulated from
top to bottom
trait Term
case class Var(name: String) extends Term
case class Fun(arg: String, body: Term) extends Term
Var("x") == Var("x") // Prints true
Checks the equality
among attributes
Ingegneria del software mod. B
OBJECT ORIENTED SCALA
 Case classes and pattern matching
 Native implementation of Value Object pattern
 Obviously, case classes can be used in pattern
matching...
 The match is done recursively on the structure of the type
18Riccardo Cardin
trait Term
case class Var(name: String) extends Term
case class Fun(arg: String, body: Term) extends Term
def printTerm(term: Term) { term match {
// The clauses uses placeholders for variables.
// If we don’t need the value, we can use Var(_)
case Var(n) =>
print(“We have a variable!”)
case Fun(x, b) =>
print(“We have a function!”)
}
}
Ingegneria del software mod. B
MISCELLANEA
 Generics: more or less like Java Generics
 Subtyping of generic types is "invariant"
 ...but it can also be used in a variant (+T) o
contravariant (-T) fashion
 There are also upper type bound (T<:A) and lower
type bound (T>:A)
19Riccardo Cardin
class Node[T](elem: T)
val node = new Node(3) // Smart type inference
case class ListNode[+T](h: T, t: ListNode[T]) {
def head: T = h
def tail: ListNode[T] = t
def prepend[U >: T](elem: U): ListNode[U] =
ListNode(elem, this)
}
Ingegneria del software mod. B
MISCELLANEA
 Option[A] type
 A list with zero (None) or one element (Some[A])
 Native implementation of the Null Object Pattern
 No more NullPointerExceptions!!
 Use them with pattern matching: results garanted!
20Riccardo Cardin
trait Option[T] {
def isDefined: Boolean
def get: T // Throws an exception if None
def getOrElse(t: T): T
}
case class Some[T](elem: T) extends Option[T]
case object None extends Option[Nothing]
def negate(x: Option[Int]) = x match {
case None => throw new IllegalArgumentException
case Some(i) => -i
}
Ingegneria del software mod. B
MISCELLANEA
 Implicit classes
 Used to translate object between similar types
 -i.e, from Java types (int) to Scala type (Int)
 Native implementation of Adapter pattern!
21Riccardo Cardin
trait Log {
def warning(message: String)
def error(message: String)
}
final class Logger {
def log(level: Level, message: String) { /* ... */ }
}
implicit class LoggerToLogAdapter(logger: Logger) extends Log {
def warning(message: String) { logger.log(WARNING, message) }
def error(message: String) { logger.log(ERROR, message) }
}
// The instance of Logger is translated to an instance of Log
val log: Log = new Logger()
The main ctor is used to
resolve the conversion
Ingegneria del software mod. B
MISCELLANEA
 Tuples
 Have you ever want a simple couple of value
container, while you were programming in Java?
 Scala has a type, Tuples, that is just a tuple of
values
 Don’t abuse of it: remember that things called classes?
 Used internally by the compilator to treat function’s
arguments
 You can build tuples with «up to 22» values
22Riccardo Cardin
val things = ("a", 1, 3.5) // (java.lang.String, Int, Double)
println(things._1) // Prints ‘a’
println(things._2) // Prints 1
// Create a tuple using the ‘->’ operator
val tuple = 1 -> "a" // (Int, java.lang.String)
Ingegneria del software mod. B
FUNCTIONAL SCALA
23Riccardo Cardin
Ingegneria del software mod. B
FUNCTIONAL SCALA
 FP, a "new" programming paradigm
 Not so young, instead
 Main principles
 Functions are high order, first-class citizen
 Refer to it, pass as argument to another function…
 Pure function
 No side effects, immutability (referential transparency)
 Recursion
24Riccardo Cardin
1930
Lambda
calculus
1958
Lisp
1990
Haskell
2003
Scala
2007
Clojure
Ingegneria del software mod. B
FUNCTIONAL SCALA
 Functions: first-class citizens
 Functions are compiled into object of an anonymous
class extending a trait FunctionX
 X represents the number of parameters
 abstract def apply(v1: T1, v2: T2, …): R
 Strategy Pattern anyone?
25Riccardo Cardin
def name (parameters-list)[: return-type]
parameters-list := name: type
Definizione
val max = (x: Int, y: Int) => if (x < y) y else x
// Compiler builds an anonimous class, extending the trait
// FunctionX, where X is the number of parameters
val anonfun2 = new Function2[Int, Int, Int] {
// Application method, as in companion objects
def apply(x: Int, y: Int): Int = if (x < y) y else x
}
assert(max(0, 1) == anonfun2(0, 1))
Ingegneria del software mod. B
FUNCTIONAL SCALA
 Functions (a.k.a. lambdas)
 Return clause is redundant: last expression of the
function body is the value returned
26Riccardo Cardin
// Complete definition
def hello1(s: String): String = “Hello ” + s
// Inferred return type
def hello2(s: String) = “Hello ” + s
// Using function literal
def hello3 = (s: String) => “Hello ” + s
// Assigning a function to another function, inferred type
def hello4 = hello3 // copy of definition
// ...or assigning a function to a val
val hello5 = hello4 // as val (no difference, unless...)
// Assigning a function to another function, not inferred type
def hello6: String => String = hello1
// For every param it can be use the wildcard ‘_’ if it is used only
// one time in the function body
def hello: String => String = "Hello " + _
Ingegneria del software mod. B
FUNCTIONAL SCALA
27Riccardo Cardin
Ingegneria del software mod. B
FUNCTIONAL SCALA
 Recursion
 Let’s try to define the factorial function
 Every step depends on the local variable n and on a
recursive call of the function
 Every recursion step allocates a new frame in the stack
 Simple recursion  StackOverflowError
28Riccardo Cardin
// Simple recursion
def factorial(n: Int): Int =
if (n == 0) 1 else n * factorial(n – 1)
n * f(n – 1) n * f(n – 2) n * f(n – 3) n * f(n – 4) ...stack:
Ingegneria del software mod. B
FUNCTIONAL SCALA
 Recursion
 We need to limit the use of the stack: tail recursion
 Every step depends only on the variables stored in
the current stack frame
 Stack frame can be reused!!!
 @tailrec  Tells Scala compiler to optimize the
execution using a while loop
29Riccardo Cardin
// Tail recursion
def factorial(n: Int): Int = {
@tailrec
def factAcc(n: Int, acc: Int): Int =
if (n == 0) acc else fastAcc(n – 1, acc * n)
// Initial call
fastAcc(n)
}
We need an
accumulator
variable
Ingegneria del software mod. B
FUNCTIONAL SCALA
 Currying
 f: (A, B) => C, than curry(f) = fc: A => (B => C)
 Used to abstract functions, separating the
“configuration” of a function from its “inputs”
 Can you see the application of the Template Method pattern?
30Riccardo Cardin
Translation of the evaluation of a function that takes multiple arguments into
a sequence of function, each with a single argument (partial application)
// Let’s generalize functions on sequence of integers
def aggregate(id: Int, op: (Int, Int) => Int)(n: Int): Int =
if (n == 0) id
else op(aggregate(neutral, op)(n-1), n)
// Factorial of integers from n to 0
def factorial = aggregate(1, _ * _)_
// Summing integers from n to 0
def sum = aggregate(0, _ + _)_
Do you know
Haskell Curry?
Ingegneria del software mod. B
FUNCTIONAL SCALA
 Call by value / name
 Using the call by value default approach, each
function’s argument is evaluated immediatly
 Scala implements also the call by name approach
 Arguments are evatuated only when needed
 This could lead to multiple evatualtion
31Riccardo Cardin
// Call by value
def firstNonZero(a: Int, b: Int) = if (a != 0) a else b
println(firstNonZero(1, 3)) // Prints 1
def z(): Int = z() // Non terminating function
println(firstNonZero(1, z)) // Non terminating function
// Call by name
def firstNonZeroByName(a: Int, b: => Int) = if (a != 0) a else b
println(firstNonZeroByName(1, z)) // Prints 1
Try to have a look
to streams...
Ingegneria del software mod. B
FUNCTIONAL SCALA
 First order function and call by name
 What if you want to implement the Command
pattern, saving the commands’ history?
32Riccardo Cardin
object Invoker {
// Sequence of executed commands
private var history: Seq[() => Unit] = Seq.empty
// Using call by name approach, to store the function
// inside history w/out executing it
def invoke(command: => Unit) {
command
history :+= command _
}
}
Invoker.invoke(println("foo"))
Invoker.invoke {
println("bar 1")
println("bar 2")
}
// At this point, history contains to functions
Ingegneria del software mod. B
FUNCTIONAL SCALA
 And much more...
 Scala has a very good Collection framework
 Immutability and recursion
 foreach, filter, map, flatMap, foldLeft, ...
 Streams: infinite sequence of values
 Natively dependency injection mechanism
 Cake pattern, Implicit parameters, Reader monad
 Actors model
 Monads
 ...
33Riccardo Cardin
Ingegneria del software mod. B
CONCLUSIONS
34Riccardo Cardin
Ingegneria del software mod. B
RIFERIMENTI
 Functional Programming Principles in Scala (Coursera)
https://www.coursera.org/course/progfun
 Scala School! http://twitter.github.io/scala_school/
 Scala for the Impatient, Cay Horstmann, Addison-Wesley
2012 http://www.horstmann.com/scala/index.html
 The Evolution of Scala, Martin Odersky
http://www.slideshare.net/Odersky/scala-evolution
 Design Patterns in Scala https://pavelfatin.com/design-
patterns-in-scala/
35Riccardo Cardin

More Related Content

What's hot

Java basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiaJava basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini india
Sanjeev Tripathi
 

What's hot (20)

Managing Binary Compatibility in Scala (Scala Lift Off 2011)
Managing Binary Compatibility in Scala (Scala Lift Off 2011)Managing Binary Compatibility in Scala (Scala Lift Off 2011)
Managing Binary Compatibility in Scala (Scala Lift Off 2011)
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Java basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiaJava basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini india
 
Core Java Certification
Core Java CertificationCore Java Certification
Core Java Certification
 
Method Handles in Java
Method Handles in JavaMethod Handles in Java
Method Handles in Java
 
Core java
Core javaCore java
Core java
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Managing Binary Compatibility in Scala (Scala Days 2011)
Managing Binary Compatibility in Scala (Scala Days 2011)Managing Binary Compatibility in Scala (Scala Days 2011)
Managing Binary Compatibility in Scala (Scala Days 2011)
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Introduction to VHDL
Introduction to VHDLIntroduction to VHDL
Introduction to VHDL
 
Unit I Advanced Java Programming Course
Unit I   Advanced Java Programming CourseUnit I   Advanced Java Programming Course
Unit I Advanced Java Programming Course
 
Introduction to-vhdl
Introduction to-vhdlIntroduction to-vhdl
Introduction to-vhdl
 
Basics of reflection in java
Basics of reflection in javaBasics of reflection in java
Basics of reflection in java
 
Advanced Reflection in Java
Advanced Reflection in JavaAdvanced Reflection in Java
Advanced Reflection in Java
 
A Recommender System for Refining Ekeko/X Transformation
A Recommender System for Refining Ekeko/X TransformationA Recommender System for Refining Ekeko/X Transformation
A Recommender System for Refining Ekeko/X Transformation
 
Core java
Core javaCore java
Core java
 
Core java
Core java Core java
Core java
 
A Logic Meta-Programming Foundation for Example-Driven Pattern Detection in O...
A Logic Meta-Programming Foundation for Example-Driven Pattern Detection in O...A Logic Meta-Programming Foundation for Example-Driven Pattern Detection in O...
A Logic Meta-Programming Foundation for Example-Driven Pattern Detection in O...
 
ParaSail
ParaSail  ParaSail
ParaSail
 

Viewers also liked

Viewers also liked (16)

Scala - the good, the bad and the very ugly
Scala - the good, the bad and the very uglyScala - the good, the bad and the very ugly
Scala - the good, the bad and the very ugly
 
SOLID - Principles of Object Oriented Design
SOLID - Principles of Object Oriented DesignSOLID - Principles of Object Oriented Design
SOLID - Principles of Object Oriented Design
 
Software architecture patterns
Software architecture patternsSoftware architecture patterns
Software architecture patterns
 
Scala For Java Programmers
Scala For Java ProgrammersScala For Java Programmers
Scala For Java Programmers
 
Errori comuni nei documenti di Analisi dei Requisiti
Errori comuni nei documenti di Analisi dei RequisitiErrori comuni nei documenti di Analisi dei Requisiti
Errori comuni nei documenti di Analisi dei Requisiti
 
Introduzione ai Design Pattern
Introduzione ai Design PatternIntroduzione ai Design Pattern
Introduzione ai Design Pattern
 
Diagrammi delle Classi
Diagrammi delle ClassiDiagrammi delle Classi
Diagrammi delle Classi
 
Presto updates to 0.178
Presto updates to 0.178Presto updates to 0.178
Presto updates to 0.178
 
Diagrammi di Sequenza
Diagrammi di SequenzaDiagrammi di Sequenza
Diagrammi di Sequenza
 
Java Graphics Programming
Java Graphics ProgrammingJava Graphics Programming
Java Graphics Programming
 
Java - Collections framework
Java - Collections frameworkJava - Collections framework
Java - Collections framework
 
Design Pattern Architetturali - Dependency Injection
Design Pattern Architetturali - Dependency InjectionDesign Pattern Architetturali - Dependency Injection
Design Pattern Architetturali - Dependency Injection
 
Java - Sockets
Java - SocketsJava - Sockets
Java - Sockets
 
Design Pattern Strutturali
Design Pattern StrutturaliDesign Pattern Strutturali
Design Pattern Strutturali
 
Java - Processing input and output
Java - Processing input and outputJava - Processing input and output
Java - Processing input and output
 
Design pattern architetturali Model View Controller, MVP e MVVM
Design pattern architetturali   Model View Controller, MVP e MVVMDesign pattern architetturali   Model View Controller, MVP e MVVM
Design pattern architetturali Model View Controller, MVP e MVVM
 

Similar to A (too) Short Introduction to Scala

A Sceptical Guide to Functional Programming
A Sceptical Guide to Functional ProgrammingA Sceptical Guide to Functional Programming
A Sceptical Guide to Functional Programming
Garth Gilmour
 

Similar to A (too) Short Introduction to Scala (20)

Oop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalOop2010 Scala Presentation Stal
Oop2010 Scala Presentation Stal
 
Qcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scalaQcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scala
 
Scala fundamentals
Scala fundamentalsScala fundamentals
Scala fundamentals
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Language
 
A Sceptical Guide to Functional Programming
A Sceptical Guide to Functional ProgrammingA Sceptical Guide to Functional Programming
A Sceptical Guide to Functional Programming
 
Scala uma poderosa linguagem para a jvm
Scala   uma poderosa linguagem para a jvmScala   uma poderosa linguagem para a jvm
Scala uma poderosa linguagem para a jvm
 
Introduction to Scala for JCConf Taiwan
Introduction to Scala for JCConf TaiwanIntroduction to Scala for JCConf Taiwan
Introduction to Scala for JCConf Taiwan
 
Object-oriented Basics
Object-oriented BasicsObject-oriented Basics
Object-oriented Basics
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With Scala
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With Scala
 
Introduction to c_plus_plus
Introduction to c_plus_plusIntroduction to c_plus_plus
Introduction to c_plus_plus
 
Introduction to c_plus_plus (6)
Introduction to c_plus_plus (6)Introduction to c_plus_plus (6)
Introduction to c_plus_plus (6)
 
Unit 2 notes.pdf
Unit 2 notes.pdfUnit 2 notes.pdf
Unit 2 notes.pdf
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry Level
 
Scala ntnu
Scala ntnuScala ntnu
Scala ntnu
 
Scala in a nutshell by venkat
Scala in a nutshell by venkatScala in a nutshell by venkat
Scala in a nutshell by venkat
 
Programming Android Application in Scala.
Programming Android Application in Scala.Programming Android Application in Scala.
Programming Android Application in Scala.
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
JavaScript(Es5) Interview Questions & Answers
JavaScript(Es5)  Interview Questions & AnswersJavaScript(Es5)  Interview Questions & Answers
JavaScript(Es5) Interview Questions & Answers
 
Dart, unicorns and rainbows
Dart, unicorns and rainbowsDart, unicorns and rainbows
Dart, unicorns and rainbows
 

More from Riccardo Cardin

More from Riccardo Cardin (7)

Design Pattern Comportamentali
Design Pattern ComportamentaliDesign Pattern Comportamentali
Design Pattern Comportamentali
 
Design Pattern Creazionali
Design Pattern CreazionaliDesign Pattern Creazionali
Design Pattern Creazionali
 
Diagrammi di Attività
Diagrammi di AttivitàDiagrammi di Attività
Diagrammi di Attività
 
Diagrammi Use Case
Diagrammi Use CaseDiagrammi Use Case
Diagrammi Use Case
 
Introduzione a UML
Introduzione a UMLIntroduzione a UML
Introduzione a UML
 
Mvc e di spring e angular js
Mvc e di   spring e angular jsMvc e di   spring e angular js
Mvc e di spring e angular js
 
Reactive programming principles
Reactive programming principlesReactive programming principles
Reactive programming principles
 

Recently uploaded

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 

Recently uploaded (20)

Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
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
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
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
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.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
 
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 🔝✔️✔️
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...
 
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
 

A (too) Short Introduction to Scala

  • 1. SHORT INTRODUCTION TO SCALA INGEGNERIA DEL SOFTWARE Università degli Studi di Padova Dipartimento di Matematica Corso di Laurea in Informatica, A.A. 2014 – 2015 rcardin@math.unipd.it
  • 2. Ingegneria del software mod. B INTRODUCTION  Object/functional programming language  Based on Java Virtual Machine  Full interoperability with Java libraries  Pure object orientation (no primitive types)  Every function is a value  High order functions (lambdas)  Promoting immutability  Currying  Pattern matching  Statically typed  Intelligent type system  Extensible 2Riccardo Cardin 2003: internal use to EPFL 2004: version 1.0 2006: version 2.0 2007: the «growth year» 2014: version 2.11 (JVM 8)
  • 3. Ingegneria del software mod. B INTRODUCTION  Language main features  Read-Eval-Print-Loop (REPL) interpreter  An interactive shell to execute statements "on the fly"  Variables  UML syntax -> name : type  val x: Int = 1 + 1 Immutable  lazy val x: Int = 1 + 1 Immutable and lazy  var x: Int = 1 + 1 Mutable  def x: Int = 1 + 1 Executed every time  Very smart type inferer val x = 1 x: Int  No semicolon  Blocks, blocks everywhere... 3Riccardo Cardin
  • 4. Ingegneria del software mod. B INTRODUCTION  Language main features  Every statement is a "pure" expression  No return statement needed!  The if-else statement is a value  Imperative for statement is not supported  Use the for-yield statement instead  Every char can be used to define names (%$#@?...)  Full integration with every Java library 4Riccardo Cardin val y = 4 // x has type String val x = if (y < 3) "minor" else "greater" // Returns an array of integers between 2 and 101 for (i <- (1 to 100)) yield i + 1
  • 5. Ingegneria del software mod. B OBJECT ORIENTED SCALA  Everything is an object 5Riccardo Cardin Values References (Scala and Java)
  • 6. Ingegneria del software mod. B OBJECT ORIENTED SCALA  Classes 6Riccardo Cardin class Person(val name: String, val surname: String, val birth: Date) { // Main Ctor body here // Every secondary Ctor must call the main Ctor as // first statement (very restrictive) def this(name: String) = this(name, "", null) def age(): Int = { /* return the age */ } // Override intention must be declared override def toString = s"My name is $name $surname" }
  • 7. Ingegneria del software mod. B OBJECT ORIENTED SCALA  Classes  Main constructor is declared within Class name  Ctor body is made of statements in class body  Accessor methods are build automatically  val immutable value, getter only  var  mutable value, getter and setter  def defines methods (and functions too...)  Use equal "=" iff the method is a function  No public class  You can have more than a class into a .scala file  No static methods (wait a minute...) 7Riccardo Cardin
  • 8. Ingegneria del software mod. B OBJECT ORIENTED SCALA  ...so let’s introduce objects notation! 8Riccardo Cardin
  • 9. Ingegneria del software mod. B OBJECT ORIENTED SCALA  Object  Native Singleton pattern implementation  There can be only one instance of ChuckNorris  Companion object  An object called as an existing class  Companion object’s methods are static method of the class 9Riccardo Cardin object ChuckNorris extends Person("Chuck Norris") { // This is a static method def roundhouseKick = "Roundhouse" } println ChuckNorris.roundhouseKick class Person(val name: String, val surname: String) object Person { // Companion object def someStupidStaticMethod = "This is a static method" } println Person.someStupidStaticMethod
  • 10. Ingegneria del software mod. B OBJECT ORIENTED SCALA  Object  Companion objects implement also Factory Method pattern natively  Every apply method creates an instance of the class  Syntactic sugar: Class.apply(...)  Class(...)  It’s like a class application (...function anyone?) 10Riccardo Cardin class Person(val name: String, val surname: String) object Person { // Factory method def apply(n: String, s: String) = new Person(n, s) } // Application of the class will return a new instance val ricky = Person("Riccardo", "Cardin") // No new operator There can be more than one apply!!!!
  • 11. Ingegneria del software mod. B OBJECT ORIENTED SCALA  Object  Used also to define the entry point of an application  The Java way  The Scala way 11Riccardo Cardin object Hello extends App { // All the code in the object’s ctor body will be // executed as our application. // Arguments are accessible via an ‘args’ variable, // available in the App trait println("Hello World!") } object Hello { // Every object containing a main method could be // Executed. There are no restriction on file name def main(args: Array[String]) { println("Hello World!") } }
  • 12. Ingegneria del software mod. B OBJECT ORIENTED SCALA  Abstract classes  Also defines a main constructor  It has to be called from the derived classes’ main Ctor 12Riccardo Cardin abstract class Animal(val species: String) { def walk // Abstract method } class Person(val name: String, val surname: String) // Calling main ctor directly in class // definition extends Animal("Human") { def walk = { /* Do some stuff */ } } val ricky = new Person("Riccardo", "Cardin")
  • 13. Ingegneria del software mod. B OBJECT ORIENTED SCALA  Traits: interfaces or abstract classes?  Like interfaces, they have not a constructor  To implement a single trait, use extends  Otherwise, use with (mixin)  But, they can have methods with implementation and attributes  Similar (but more powerful) to default methods in Java 8  Multiple inheritance problem  Class linearization: use the rightmost type (more or less...) 13Riccardo Cardin trait A { override def toString = "I’m A" } trait B { override def toString = "I’m B" } trait C { override def toString = "I’m C" } class D extends A with B, C new D().toString // Prints ‘I’m C’
  • 14. Ingegneria del software mod. B OBJECT ORIENTED SCALA  Traits  Like an interface  Like an abstract class 14Riccardo Cardin // No implementation at all trait Sorter { def sort(list: List[Int]): List[Int] } class QuickSorter extends Sorter { def sort(list: List[Int]) = { /* Do something */ } } trait AbstractDao { // No Ctor (wtf!) var pool: Array[Datasource] def getPool = pool // Implemented method def save // abstract method def delete // abstract method } class UserDao extends AbstractDao { // Has to implement save and delete }
  • 15. Ingegneria del software mod. B OBJECT ORIENTED SCALA  Traits  Inside a mixin the super refers to the previuos trait in the linearized-type  Used to implement the Decorator pattern natively 15Riccardo Cardin trait Employee { // ... def whois(): String } class Engineer(name: String, office: String) extends Employee trait ProjectManager extends Employee { abstract override def whois() { // super refers to the previuos trait on the left super.whois(buffer) println("and I’am a project manager too!") } } new Engineer("Riccardo","Development") with ProjectManager Statically binded Decorator pattern
  • 16. Ingegneria del software mod. B OBJECT ORIENTED SCALA  Traits and Objects  Using the companion object of a trait, we can implement the Abstract Factory pattern 16Riccardo Cardin // Abstract factory trait AbstractFactory // Private classes’ scope is limited to the // definition file private class ConcreteFactory1 extends AbstractFactory { /* ... */ } private class ConcreteFactory2 extends AbstractFactory { /* ... */ } object AbstractFactory{ def apply(kind: String) = kind match { case “factory1" => new ConcreteFactory1 () case “factory2" => new ConcreteFactory1 () factory 2 } } val factory = AbstractFactory("factory1")
  • 17. Ingegneria del software mod. B OBJECT ORIENTED SCALA  Case classes and pattern matching  Scala has a built in pattern matching mechanism  Case class: regular class that can be pattern-matched  Turns all ctor params into val (immutable) constants  Generates a companion object w/ apply methods  Generates toString, equals and hashCode methods properly 17Riccardo Cardin def matchTest(x: Int): String = x match { case 1 => "one" case _ => "many" } println(matchTest(3)) // Prints ‘many’ Case clauses are evaulated from top to bottom trait Term case class Var(name: String) extends Term case class Fun(arg: String, body: Term) extends Term Var("x") == Var("x") // Prints true Checks the equality among attributes
  • 18. Ingegneria del software mod. B OBJECT ORIENTED SCALA  Case classes and pattern matching  Native implementation of Value Object pattern  Obviously, case classes can be used in pattern matching...  The match is done recursively on the structure of the type 18Riccardo Cardin trait Term case class Var(name: String) extends Term case class Fun(arg: String, body: Term) extends Term def printTerm(term: Term) { term match { // The clauses uses placeholders for variables. // If we don’t need the value, we can use Var(_) case Var(n) => print(“We have a variable!”) case Fun(x, b) => print(“We have a function!”) } }
  • 19. Ingegneria del software mod. B MISCELLANEA  Generics: more or less like Java Generics  Subtyping of generic types is "invariant"  ...but it can also be used in a variant (+T) o contravariant (-T) fashion  There are also upper type bound (T<:A) and lower type bound (T>:A) 19Riccardo Cardin class Node[T](elem: T) val node = new Node(3) // Smart type inference case class ListNode[+T](h: T, t: ListNode[T]) { def head: T = h def tail: ListNode[T] = t def prepend[U >: T](elem: U): ListNode[U] = ListNode(elem, this) }
  • 20. Ingegneria del software mod. B MISCELLANEA  Option[A] type  A list with zero (None) or one element (Some[A])  Native implementation of the Null Object Pattern  No more NullPointerExceptions!!  Use them with pattern matching: results garanted! 20Riccardo Cardin trait Option[T] { def isDefined: Boolean def get: T // Throws an exception if None def getOrElse(t: T): T } case class Some[T](elem: T) extends Option[T] case object None extends Option[Nothing] def negate(x: Option[Int]) = x match { case None => throw new IllegalArgumentException case Some(i) => -i }
  • 21. Ingegneria del software mod. B MISCELLANEA  Implicit classes  Used to translate object between similar types  -i.e, from Java types (int) to Scala type (Int)  Native implementation of Adapter pattern! 21Riccardo Cardin trait Log { def warning(message: String) def error(message: String) } final class Logger { def log(level: Level, message: String) { /* ... */ } } implicit class LoggerToLogAdapter(logger: Logger) extends Log { def warning(message: String) { logger.log(WARNING, message) } def error(message: String) { logger.log(ERROR, message) } } // The instance of Logger is translated to an instance of Log val log: Log = new Logger() The main ctor is used to resolve the conversion
  • 22. Ingegneria del software mod. B MISCELLANEA  Tuples  Have you ever want a simple couple of value container, while you were programming in Java?  Scala has a type, Tuples, that is just a tuple of values  Don’t abuse of it: remember that things called classes?  Used internally by the compilator to treat function’s arguments  You can build tuples with «up to 22» values 22Riccardo Cardin val things = ("a", 1, 3.5) // (java.lang.String, Int, Double) println(things._1) // Prints ‘a’ println(things._2) // Prints 1 // Create a tuple using the ‘->’ operator val tuple = 1 -> "a" // (Int, java.lang.String)
  • 23. Ingegneria del software mod. B FUNCTIONAL SCALA 23Riccardo Cardin
  • 24. Ingegneria del software mod. B FUNCTIONAL SCALA  FP, a "new" programming paradigm  Not so young, instead  Main principles  Functions are high order, first-class citizen  Refer to it, pass as argument to another function…  Pure function  No side effects, immutability (referential transparency)  Recursion 24Riccardo Cardin 1930 Lambda calculus 1958 Lisp 1990 Haskell 2003 Scala 2007 Clojure
  • 25. Ingegneria del software mod. B FUNCTIONAL SCALA  Functions: first-class citizens  Functions are compiled into object of an anonymous class extending a trait FunctionX  X represents the number of parameters  abstract def apply(v1: T1, v2: T2, …): R  Strategy Pattern anyone? 25Riccardo Cardin def name (parameters-list)[: return-type] parameters-list := name: type Definizione val max = (x: Int, y: Int) => if (x < y) y else x // Compiler builds an anonimous class, extending the trait // FunctionX, where X is the number of parameters val anonfun2 = new Function2[Int, Int, Int] { // Application method, as in companion objects def apply(x: Int, y: Int): Int = if (x < y) y else x } assert(max(0, 1) == anonfun2(0, 1))
  • 26. Ingegneria del software mod. B FUNCTIONAL SCALA  Functions (a.k.a. lambdas)  Return clause is redundant: last expression of the function body is the value returned 26Riccardo Cardin // Complete definition def hello1(s: String): String = “Hello ” + s // Inferred return type def hello2(s: String) = “Hello ” + s // Using function literal def hello3 = (s: String) => “Hello ” + s // Assigning a function to another function, inferred type def hello4 = hello3 // copy of definition // ...or assigning a function to a val val hello5 = hello4 // as val (no difference, unless...) // Assigning a function to another function, not inferred type def hello6: String => String = hello1 // For every param it can be use the wildcard ‘_’ if it is used only // one time in the function body def hello: String => String = "Hello " + _
  • 27. Ingegneria del software mod. B FUNCTIONAL SCALA 27Riccardo Cardin
  • 28. Ingegneria del software mod. B FUNCTIONAL SCALA  Recursion  Let’s try to define the factorial function  Every step depends on the local variable n and on a recursive call of the function  Every recursion step allocates a new frame in the stack  Simple recursion  StackOverflowError 28Riccardo Cardin // Simple recursion def factorial(n: Int): Int = if (n == 0) 1 else n * factorial(n – 1) n * f(n – 1) n * f(n – 2) n * f(n – 3) n * f(n – 4) ...stack:
  • 29. Ingegneria del software mod. B FUNCTIONAL SCALA  Recursion  We need to limit the use of the stack: tail recursion  Every step depends only on the variables stored in the current stack frame  Stack frame can be reused!!!  @tailrec  Tells Scala compiler to optimize the execution using a while loop 29Riccardo Cardin // Tail recursion def factorial(n: Int): Int = { @tailrec def factAcc(n: Int, acc: Int): Int = if (n == 0) acc else fastAcc(n – 1, acc * n) // Initial call fastAcc(n) } We need an accumulator variable
  • 30. Ingegneria del software mod. B FUNCTIONAL SCALA  Currying  f: (A, B) => C, than curry(f) = fc: A => (B => C)  Used to abstract functions, separating the “configuration” of a function from its “inputs”  Can you see the application of the Template Method pattern? 30Riccardo Cardin Translation of the evaluation of a function that takes multiple arguments into a sequence of function, each with a single argument (partial application) // Let’s generalize functions on sequence of integers def aggregate(id: Int, op: (Int, Int) => Int)(n: Int): Int = if (n == 0) id else op(aggregate(neutral, op)(n-1), n) // Factorial of integers from n to 0 def factorial = aggregate(1, _ * _)_ // Summing integers from n to 0 def sum = aggregate(0, _ + _)_ Do you know Haskell Curry?
  • 31. Ingegneria del software mod. B FUNCTIONAL SCALA  Call by value / name  Using the call by value default approach, each function’s argument is evaluated immediatly  Scala implements also the call by name approach  Arguments are evatuated only when needed  This could lead to multiple evatualtion 31Riccardo Cardin // Call by value def firstNonZero(a: Int, b: Int) = if (a != 0) a else b println(firstNonZero(1, 3)) // Prints 1 def z(): Int = z() // Non terminating function println(firstNonZero(1, z)) // Non terminating function // Call by name def firstNonZeroByName(a: Int, b: => Int) = if (a != 0) a else b println(firstNonZeroByName(1, z)) // Prints 1 Try to have a look to streams...
  • 32. Ingegneria del software mod. B FUNCTIONAL SCALA  First order function and call by name  What if you want to implement the Command pattern, saving the commands’ history? 32Riccardo Cardin object Invoker { // Sequence of executed commands private var history: Seq[() => Unit] = Seq.empty // Using call by name approach, to store the function // inside history w/out executing it def invoke(command: => Unit) { command history :+= command _ } } Invoker.invoke(println("foo")) Invoker.invoke { println("bar 1") println("bar 2") } // At this point, history contains to functions
  • 33. Ingegneria del software mod. B FUNCTIONAL SCALA  And much more...  Scala has a very good Collection framework  Immutability and recursion  foreach, filter, map, flatMap, foldLeft, ...  Streams: infinite sequence of values  Natively dependency injection mechanism  Cake pattern, Implicit parameters, Reader monad  Actors model  Monads  ... 33Riccardo Cardin
  • 34. Ingegneria del software mod. B CONCLUSIONS 34Riccardo Cardin
  • 35. Ingegneria del software mod. B RIFERIMENTI  Functional Programming Principles in Scala (Coursera) https://www.coursera.org/course/progfun  Scala School! http://twitter.github.io/scala_school/  Scala for the Impatient, Cay Horstmann, Addison-Wesley 2012 http://www.horstmann.com/scala/index.html  The Evolution of Scala, Martin Odersky http://www.slideshare.net/Odersky/scala-evolution  Design Patterns in Scala https://pavelfatin.com/design- patterns-in-scala/ 35Riccardo Cardin