SlideShare a Scribd company logo
Scalable Language
JVM
Scala Conventions
Return is almost optional ; is optional
No args functions doesn’t need ()
def sum(a: Int, b: Int) = a + b def sum(a: Int, b: Int) = {
println(a)
println(b)
a+b
}
def count() = {...}
val cnt = list.count
Type Inference
Scala is strongly typed Type is inferred by scala
val a: Int = 1 def funz(a: Int) = a*10.0
def funz2(a: Int):Double = a*10.0
Value vs Variable
A value is immutable A variable is mutable
scala val ~= java final attribute
val a = 1
a = 2 // error: reassignment to val
var a = 1
a = 2 // ok
Object Oriented
Classes
Constructor is class body itself! Costructors override
class Person(name:String) {
var age = 0
println(name)
}
class Person(name:String) {
var age = 0
def this(name:String, anAge:Int) {
this(name)
age = anAge
}
}
Case Classes
Equals() and hashcode() are already
implemented!!
case class Color(name:String, code: Int)
val c = Color("red", 34567)
Embedded singleton pattern
Scala hasn't static
because Scala is object oriented…. allows you define object
Object and class work togheter
object Obj {
def doSomething() = println
}
Obj.doSomething()
class Color(name:String)
object Color { // this is the singleton
private val colors = Map(
"red" -> new Color("red"),
"green" -> new Color("green"),
"blue" -> new Color("blue"))
def mk(name:String) = colors(name)
}
Traits
Similar to interfaces, but much more powerful!
trait Equal {
def isEqual(x: Any): Boolean //Scala allows traits to be partially implemented
def isNotEqual(x: Any): Boolean = !isEqual(x)
}
class Point(xc: Int, yc: Int) extends Equal {
val x = xc
val y = yc
def isEqual(obj: Any) = obj.isInstanceOf[Point] && obj.asInstanceOf[Point].x == x
}
Multiple Inheritance
Trait rules
trait Swimming { def swim() = println("I'm swimming") }
abstract class Bird { def fly() = println("I'm flying") }
class Bird1 extends Bird
class Bird2 extends Bird with Swimming
new Bird1().fly() // I'm flying
new Bird2().swim() //I'm swimming
(new Bird1() with Swimming).swim() //composable objects at runtime
Functional
First class function
def f1(x: Int) = x
def f2(x: Int) = x * 2
def g(f:Int=>Int, x:Int) = f(x) + 1
g(f1, 3) // f1(3) + 1 → 4
g(f2, 3) // f2(3) + 1 → 7
Pattern Matching
can match int, string, List, case class!
Match is a function too
case class Color(name: String, code: String)
val obj : Any = _
obj match {
case "string" => println("obj String")
case a: Int => println("obj is Int with value: "+a)
case l: List[_] => println("obj is a List")
case Color(name, code) => println("obj is the color: "+name)
case "string" :: item :: other => println("")
case _ => println("")
}
Scala vs Java
def products = orders.flatMap(_.products)
.filter(_.category == category)
val list = List(1,2,3)
public List<Product> getProducts() {
List<Product> products = new ArrayList<Product>();
for(Order order : orders) {
for(Product product : order.getProducts()) {
if(category.equals(product.getCategory())){
products.add(product);
}
}
}
return products;
}
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
Scala XML
val foo = <foo>
<bar type="greet">hi</bar>
<bar type="count">1</bar>
<bar type="color">yellow</bar>
</foo>
(foo  "bar").map(_  "@type")
Enterprise Ready
Scala JS
Scala.js compiles Scala code to JavaScript, allowing you to
write your web application entirely in Scala!
Jquery , AngularJS, NodeJS , etc... are already integrated!
Scalable
"Prefer vals, immutable objects,
and methods without side effects.
Reach for them first. Use vars,
mutable objects, and methods with
side effects when you have a
specific need and justification for
them."
Functional is statelessImmutable is stateless
shared or mutable states are scalability killers
Actors react to received messages by
executing a behavior function
Actors never share state and thus
never need to compete for locks for
access to shared data
Scala Features
Lazy val 
Streams 
String interpolation 
Runtime code generation 
Runtime reflection 
Regex 
Macros, quasiquotes, sequence comprehension, lambda, currying, tuple...etc.
lazy val funcVal = { println("executing"); 3 }
lazy val fibs = BigInt(0) #:: BigInt(1)
#:: Stream.empty
val str = s"the user ${username} has ${numOfTokens} tokens"
toolbox.eval(toolbox.parse("case class D(a: Int, b: Int)"))
def getTypeTag[T: ru.TypeTag](obj: T) = ru.typeTag[T]
val theType = getTypeTag(List(1,2,3)).tpe
val pattern = "(S|s)cala".r
pattern findFirstIn "Scala is Scalable and cool"
Any questions ?

More Related Content

PPTX
Joy of scala
PDF
Scala jargon cheatsheet
ODP
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
PDF
Scala 2013 review
PDF
Few simple-type-tricks in scala
PPT
Scala - brief intro
PDF
Scala cheatsheet
PDF
Scala in Places API
Joy of scala
Scala jargon cheatsheet
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Scala 2013 review
Few simple-type-tricks in scala
Scala - brief intro
Scala cheatsheet
Scala in Places API

What's hot (20)

PPTX
Intro to Functional Programming in Scala
PDF
Workshop Scala
PDF
ODP
JavaScript Web Development
PDF
Scala introduction
PDF
Starting with Scala : Frontier Developer's Meetup December 2010
PDF
First-Class Patterns
PPTX
Scala Back to Basics: Type Classes
PDF
scala
PDF
Stepping Up : A Brief Intro to Scala
ODP
Functional Objects & Function and Closures
PPTX
PDF
Programming in Scala: Notes
PDF
Scala coated JVM
ODP
1.2 scala basics
ODP
A Tour Of Scala
PPTX
Scala fundamentals
KEY
Scalaz
PDF
Metaprogramming in Scala 2.10, Eugene Burmako,
PPTX
Scala for curious
Intro to Functional Programming in Scala
Workshop Scala
JavaScript Web Development
Scala introduction
Starting with Scala : Frontier Developer's Meetup December 2010
First-Class Patterns
Scala Back to Basics: Type Classes
scala
Stepping Up : A Brief Intro to Scala
Functional Objects & Function and Closures
Programming in Scala: Notes
Scala coated JVM
1.2 scala basics
A Tour Of Scala
Scala fundamentals
Scalaz
Metaprogramming in Scala 2.10, Eugene Burmako,
Scala for curious
Ad

Viewers also liked (20)

PDF
Maven c'est bien, SBT c'est mieux
PDF
Universitélang scala tools
PDF
Les monades Scala, Java 8
PDF
Université des langages scala
PDF
Lagom, reactive framework
PDF
Agile Lab_BigData_Meetup_AKKA
PDF
Introduction à Scala - Michel Schinz - January 2010
PDF
Agile Lab_BigData_Meetup
PDF
Scala in Action - Heiko Seeburger
PDF
Getting Functional with Scala
PDF
Paris stormusergroup intrudocution
ODP
Introduction to Spark with Scala
PDF
Hammurabi
PDF
Soutenance ysance
PDF
Scala - A Scalable Language
PDF
Scala at HUJI PL Seminar 2008
PDF
Mémoire de fin d'étude - La big data et les réseaux sociaux
PDF
Lagom, reactive framework(chtijug2016)
PDF
Démystifions le machine learning avec spark par David Martin pour le Salon B...
PDF
BDX 2016 - Tzach zohar @ kenshoo
Maven c'est bien, SBT c'est mieux
Universitélang scala tools
Les monades Scala, Java 8
Université des langages scala
Lagom, reactive framework
Agile Lab_BigData_Meetup_AKKA
Introduction à Scala - Michel Schinz - January 2010
Agile Lab_BigData_Meetup
Scala in Action - Heiko Seeburger
Getting Functional with Scala
Paris stormusergroup intrudocution
Introduction to Spark with Scala
Hammurabi
Soutenance ysance
Scala - A Scalable Language
Scala at HUJI PL Seminar 2008
Mémoire de fin d'étude - La big data et les réseaux sociaux
Lagom, reactive framework(chtijug2016)
Démystifions le machine learning avec spark par David Martin pour le Salon B...
BDX 2016 - Tzach zohar @ kenshoo
Ad

Similar to Scala Intro (20)

PDF
Meet scala
PPTX
An introduction to scala
PDF
Scala: Object-Oriented Meets Functional, by Iulian Dragos
PDF
Getting Started With Scala
PDF
Getting Started With Scala
PPTX
Scala, Play 2.0 & Cloud Foundry
PDF
Scala for Java Programmers
ODP
PDF
Railroading into Scala
PDF
Introduction to Scala
PPTX
Scala introduction
PPT
Scala introduction
PPTX
Intro to Scala
PDF
Scala - core features
PDF
The Scala Programming Language
PDF
From Java to Scala - advantages and possible risks
PDF
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
PDF
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
PDF
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
PDF
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Meet scala
An introduction to scala
Scala: Object-Oriented Meets Functional, by Iulian Dragos
Getting Started With Scala
Getting Started With Scala
Scala, Play 2.0 & Cloud Foundry
Scala for Java Programmers
Railroading into Scala
Introduction to Scala
Scala introduction
Scala introduction
Intro to Scala
Scala - core features
The Scala Programming Language
From Java to Scala - advantages and possible risks
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf

More from Paolo Platter (10)

PPTX
Witboost Platform for decentralization of data management
PPTX
Platform Strategy for decentralization.pptx
PPTX
DAMA Norway - Computational Governance Model
PPTX
The role of Dremio in a data mesh architecture
PPTX
Data Mesh Implementation - a practical journey
PPTX
kafka simplicity and complexity
PDF
Wasp2 - IoT and Streaming Platform
PPTX
Meetup tensorframes
PDF
Bringing Deep Learning into production
PDF
Massive Streaming Analytics with Spark Streaming
Witboost Platform for decentralization of data management
Platform Strategy for decentralization.pptx
DAMA Norway - Computational Governance Model
The role of Dremio in a data mesh architecture
Data Mesh Implementation - a practical journey
kafka simplicity and complexity
Wasp2 - IoT and Streaming Platform
Meetup tensorframes
Bringing Deep Learning into production
Massive Streaming Analytics with Spark Streaming

Recently uploaded (20)

PDF
Digital Systems & Binary Numbers (comprehensive )
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
System and Network Administraation Chapter 3
PPTX
Introduction to Artificial Intelligence
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Understanding Forklifts - TECH EHS Solution
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
Digital Strategies for Manufacturing Companies
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Nekopoi APK 2025 free lastest update
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Designing Intelligence for the Shop Floor.pdf
PPTX
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
PPTX
L1 - Introduction to python Backend.pptx
Digital Systems & Binary Numbers (comprehensive )
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Operating system designcfffgfgggggggvggggggggg
System and Network Administraation Chapter 3
Introduction to Artificial Intelligence
Upgrade and Innovation Strategies for SAP ERP Customers
Understanding Forklifts - TECH EHS Solution
wealthsignaloriginal-com-DS-text-... (1).pdf
Digital Strategies for Manufacturing Companies
Design an Analysis of Algorithms II-SECS-1021-03
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Odoo POS Development Services by CandidRoot Solutions
Nekopoi APK 2025 free lastest update
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Designing Intelligence for the Shop Floor.pdf
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
L1 - Introduction to python Backend.pptx

Scala Intro

  • 2. JVM
  • 3. Scala Conventions Return is almost optional ; is optional No args functions doesn’t need () def sum(a: Int, b: Int) = a + b def sum(a: Int, b: Int) = { println(a) println(b) a+b } def count() = {...} val cnt = list.count
  • 4. Type Inference Scala is strongly typed Type is inferred by scala val a: Int = 1 def funz(a: Int) = a*10.0 def funz2(a: Int):Double = a*10.0
  • 5. Value vs Variable A value is immutable A variable is mutable scala val ~= java final attribute val a = 1 a = 2 // error: reassignment to val var a = 1 a = 2 // ok
  • 7. Classes Constructor is class body itself! Costructors override class Person(name:String) { var age = 0 println(name) } class Person(name:String) { var age = 0 def this(name:String, anAge:Int) { this(name) age = anAge } }
  • 8. Case Classes Equals() and hashcode() are already implemented!! case class Color(name:String, code: Int) val c = Color("red", 34567)
  • 9. Embedded singleton pattern Scala hasn't static because Scala is object oriented…. allows you define object Object and class work togheter object Obj { def doSomething() = println } Obj.doSomething() class Color(name:String) object Color { // this is the singleton private val colors = Map( "red" -> new Color("red"), "green" -> new Color("green"), "blue" -> new Color("blue")) def mk(name:String) = colors(name) }
  • 10. Traits Similar to interfaces, but much more powerful! trait Equal { def isEqual(x: Any): Boolean //Scala allows traits to be partially implemented def isNotEqual(x: Any): Boolean = !isEqual(x) } class Point(xc: Int, yc: Int) extends Equal { val x = xc val y = yc def isEqual(obj: Any) = obj.isInstanceOf[Point] && obj.asInstanceOf[Point].x == x }
  • 11. Multiple Inheritance Trait rules trait Swimming { def swim() = println("I'm swimming") } abstract class Bird { def fly() = println("I'm flying") } class Bird1 extends Bird class Bird2 extends Bird with Swimming new Bird1().fly() // I'm flying new Bird2().swim() //I'm swimming (new Bird1() with Swimming).swim() //composable objects at runtime
  • 13. First class function def f1(x: Int) = x def f2(x: Int) = x * 2 def g(f:Int=>Int, x:Int) = f(x) + 1 g(f1, 3) // f1(3) + 1 → 4 g(f2, 3) // f2(3) + 1 → 7
  • 14. Pattern Matching can match int, string, List, case class! Match is a function too case class Color(name: String, code: String) val obj : Any = _ obj match { case "string" => println("obj String") case a: Int => println("obj is Int with value: "+a) case l: List[_] => println("obj is a List") case Color(name, code) => println("obj is the color: "+name) case "string" :: item :: other => println("") case _ => println("") }
  • 15. Scala vs Java def products = orders.flatMap(_.products) .filter(_.category == category) val list = List(1,2,3) public List<Product> getProducts() { List<Product> products = new ArrayList<Product>(); for(Order order : orders) { for(Product product : order.getProducts()) { if(category.equals(product.getCategory())){ products.add(product); } } } return products; } ArrayList<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3);
  • 16. Scala XML val foo = <foo> <bar type="greet">hi</bar> <bar type="count">1</bar> <bar type="color">yellow</bar> </foo> (foo "bar").map(_ "@type")
  • 18. Scala JS Scala.js compiles Scala code to JavaScript, allowing you to write your web application entirely in Scala! Jquery , AngularJS, NodeJS , etc... are already integrated!
  • 19. Scalable "Prefer vals, immutable objects, and methods without side effects. Reach for them first. Use vars, mutable objects, and methods with side effects when you have a specific need and justification for them." Functional is statelessImmutable is stateless shared or mutable states are scalability killers Actors react to received messages by executing a behavior function Actors never share state and thus never need to compete for locks for access to shared data
  • 20. Scala Features Lazy val  Streams  String interpolation  Runtime code generation  Runtime reflection  Regex  Macros, quasiquotes, sequence comprehension, lambda, currying, tuple...etc. lazy val funcVal = { println("executing"); 3 } lazy val fibs = BigInt(0) #:: BigInt(1) #:: Stream.empty val str = s"the user ${username} has ${numOfTokens} tokens" toolbox.eval(toolbox.parse("case class D(a: Int, b: Int)")) def getTypeTag[T: ru.TypeTag](obj: T) = ru.typeTag[T] val theType = getTypeTag(List(1,2,3)).tpe val pattern = "(S|s)cala".r pattern findFirstIn "Scala is Scalable and cool"