SlideShare a Scribd company logo
Scala taxonomy explained
from Java programmer
Radim.Pavlicek@gmail.com
Agenda
● OO Features
● Pattern matching
● Functional Programming
● Actors
● Futures
● Implicits
Data Transfer Objets
class Person {
private String firstName;
String getFirstName() {
return firstName;
}
void setFirstName(String aFirstName) {
this.firstName = aFirstName;
}
}
toString
@Override
public String toString() {
return "Person{" +
"firstName='" + firstName + ''' +
", lastName='" + lastName + ''' +
'}';
}
equals
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Person)) return false;
Person person = (Person) o;
if (!firstName.equals(person.firstName))
return false;
if (!lastName.equals(person.lastName))
return false;
return true
}
hashCode
@Override
public int hashCode() {
int result = firstName.hashCode();
result = 31 * result + lastName.hashCode();
return result;
}
Builder
static class Builder {
String aName = "Radim";
Builder withFirstName(String aName) {
this.name = aName; return this;
}
Person build() {
Person p = new Person; p.setName(aName);
return p;
}
Case Classes
case class Person(firstName: String =
"Bill", lastName :String = "Gates")
val me = Person(lastName = "Crosby")
● Immutable
● toString, equals, hashCode
● person.copy(lastName="Malkin")
Laziness in Java
Integer cached = null;
Integer getExpensiveComputation() {
if (null == cached) {
cached = doCount();
}
return cached;
}
Lazy Definition
lazy val count = doCount()
Imports
import scala.collection.immutable.Map
object HelloWorld {
def main(args: Array[String]) {
import scala.collection.immutable.
{Map=>MobNumbers}
val my : MobNumbers[String, String] =
MobileNumbers("radim" -> "6767982408")
Console.print( my("radim") )
}
}
Java: Singletons
public class Singleton {
private Singleton INSTANCE = null;
private Singleton() {}
public Singleton getInstance() {
if (null == INSTANCE) {
INSTANCE = new Singleton()
}
}
Objects
class CompanionObjects(val id: Long, val
description: String = "")
object CompanionObjects{
def zero = new CompanionObjects(id = 0,
description = "zero")
def apply(id:Int, description:String)= new
CompanionObjets(id,description)
def one = CompanionObjets(1, "one")
val errorMessage = "ID should be positive"
}
Pattern matching
In Java:
Switch statements with String cases have
been implemented in Java SE 7, at least 16
years after they were first requested.
Pattern matching
object Matching extends App {
val name :AnyRef= ...
name match {
case "Radim" => println("me")
case "crosby" | "sidney" => println("NHL")
case Seq("sidney", _) => println("sidney with
someone")
case Seq("sidney", _*) => println("sidney with group")
case x: Int if x < 0 => println("negative number")
case y => println("Unknown " + y)
}
}
scala.annotation.switch
(ch: @switch) match {
case 'a' if eof => println("a with oef")
case 'b' => println("b")
case 'c' => println("c")
}
Functional programming
Immutability
Java
String, BigDecimal
Scala
val immutable = 1
var mutable = 2
mutable = 3
Immutability cont.
val n = "123".reverse
val m = n.reverse
println(n, m) // 321, 123
val o = new StringBuffer("123").reverse()
val p = o.reverse()
println(o, p) // 123, 123
Higher order functions
object Collections extends App{
val c = 4 to 8
val s: IndexedSeq[String] = c map(_+"th")
s map (_.toUpperCase)
println(s) //4th, 5th, 6th, 7th, 8th
}
Parallel collection
object Parralel extends App{
def time[R](block: => R): R = {
val t0 = System.nanoTime()
val result = block // call-by-name
val t1 = System.nanoTime()
println("Elapsed time: " + (t1 - t0)/1000000 + " ms")
result
}
val c = 1 to 10000000
time { c map (_ + 1) } //4935 ms
time { c.par map (_ + 1) } //3723 ms
}
Currying
object Curry extends App{
def product(i: Int)(j: Int) = i * j
def times2 = product(2)_
def times3 = product(3)_
println(times2 (4)) //8
println(times3 (4)) //12
}
Partial functions
val sample = 1 to 10
val isEven: PartialFunction[Int, String] =
{case x if x % 2 == 0 => x+" is even"}
// the method collect can use isDefinedAt
to select which members to collect
val evenNumbers = sample collect isEven
println(evenNumbers)
Actors
Futures
import ExecutionContext.Implicits.global
object Futures extends App {
@volatile var totalA = 0
val text = Future {
Thread.sleep(1000); val a = "a" * 16
}
text onSuccess {
case txt => totalA += txt.count(_ == 'a')
}
println(totalA) // 0
Thread.sleep(1000); println(totalA) //0
Thread.sleep(1000); println(totalA) //16
}
Implicit conversions
Java:
String.valueOf(int i),
Integer.parseInt(String s)
Scala:
(1 to 4).foreach(println)
Implicit parameters
object ImplicitParam extends App{
def max[T](a: T, b: T)(implicit $ev1: Ordering[T]): T =
..
class Timeout(val milis: Int)
object Timeout {
def apply(milis: Int) = new Timeout(milis)
implicit val t: Timeout = Timeout(10)
}
def query(i: Int)(implicit t: Timeout) {
println(t.milis)
}
query(1)
To be continued...

More Related Content

What's hot (20)

PDF
The Ring programming language version 1.7 book - Part 34 of 196
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.6 book - Part 183 of 189
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.2 book - Part 32 of 181
Mahmoud Samir Fayed
 
PDF
Introduction to-scala
Hamid Jafarian
 
PDF
The Ring programming language version 1.6 book - Part 40 of 189
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.2 book - Part 22 of 84
Mahmoud Samir Fayed
 
PDF
Java8 stream
koji lin
 
PDF
The Ring programming language version 1.5.1 book - Part 36 of 180
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.2 book - Part 30 of 181
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.2 book - Part 20 of 84
Mahmoud Samir Fayed
 
PDF
Introduction to functional programming using Ocaml
pramode_ce
 
PDF
The Ring programming language version 1.5.1 book - Part 29 of 180
Mahmoud Samir Fayed
 
PDF
Java cheatsheet
Anass SABANI
 
PDF
Futures e abstração - QCon São Paulo 2015
Leonardo Borges
 
PDF
The Ring programming language version 1.5.4 book - Part 33 of 185
Mahmoud Samir Fayed
 
PDF
Ruslan Shevchenko - Property based testing
Ievgenii Katsan
 
PDF
The Ring programming language version 1.7 book - Part 40 of 196
Mahmoud Samir Fayed
 
PDF
Introduction to Scala for JCConf Taiwan
Jimin Hsieh
 
PDF
The Ring programming language version 1.4 book - Part 29 of 30
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.4 book - Part 31 of 185
Mahmoud Samir Fayed
 
The Ring programming language version 1.7 book - Part 34 of 196
Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 183 of 189
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.2 book - Part 32 of 181
Mahmoud Samir Fayed
 
Introduction to-scala
Hamid Jafarian
 
The Ring programming language version 1.6 book - Part 40 of 189
Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 22 of 84
Mahmoud Samir Fayed
 
Java8 stream
koji lin
 
The Ring programming language version 1.5.1 book - Part 36 of 180
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.2 book - Part 30 of 181
Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 20 of 84
Mahmoud Samir Fayed
 
Introduction to functional programming using Ocaml
pramode_ce
 
The Ring programming language version 1.5.1 book - Part 29 of 180
Mahmoud Samir Fayed
 
Java cheatsheet
Anass SABANI
 
Futures e abstração - QCon São Paulo 2015
Leonardo Borges
 
The Ring programming language version 1.5.4 book - Part 33 of 185
Mahmoud Samir Fayed
 
Ruslan Shevchenko - Property based testing
Ievgenii Katsan
 
The Ring programming language version 1.7 book - Part 40 of 196
Mahmoud Samir Fayed
 
Introduction to Scala for JCConf Taiwan
Jimin Hsieh
 
The Ring programming language version 1.4 book - Part 29 of 30
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.4 book - Part 31 of 185
Mahmoud Samir Fayed
 

Viewers also liked (20)

PDF
Web ui testing
Radim Pavlicek
 
PPTX
Internal combustion engine
Deepak Chaand
 
PPTX
Encontro com escritor joão morgado
joaocarlosoliveiracamurca
 
PDF
Introduction to Functional Programming with Scala
Daniel Cukier
 
TXT
Tjänster.unicode
Sari Salmi
 
ODP
Functions In Scala
Knoldus Inc.
 
PDF
Genetically Modified Soybean Seed Patent Expiry
Beroe Inc - Advantage Procurement
 
PPT
Procurement Intelligence Priming Procurement Leaders for Business Power Play
Beroe Inc - Advantage Procurement
 
PDF
3D Printing: An emerging trend and its impact on the MRO supply chain | Beroe...
Beroe Inc - Advantage Procurement
 
PDF
J unit introduction
Radim Pavlicek
 
PDF
Conflict Minerals - A Supply Chain Check for Electronic OEMs Industry
Beroe Inc - Advantage Procurement
 
PDF
Java patterns in Scala
Radim Pavlicek
 
PPTX
Scala functions
Kunal Cholera
 
PDF
Brazil telecom market next treasure trove for MVNO's
Beroe Inc - Advantage Procurement
 
PDF
Will china remain a low cost country?
Beroe Inc - Advantage Procurement
 
PDF
The Future of Procurement - End of Business as Usual
Beroe Inc - Advantage Procurement
 
PDF
China's Resources Acquisition
Beroe Inc - Advantage Procurement
 
PDF
Genome editing tools article
Beroe Inc - Advantage Procurement
 
PDF
Nagoya Protocol and its Implications on Pharmaceutical Industry
Beroe Inc - Advantage Procurement
 
PDF
Propy-LENE Supply! Go Green! On-Purpose Technologies for the Future
Beroe Inc - Advantage Procurement
 
Web ui testing
Radim Pavlicek
 
Internal combustion engine
Deepak Chaand
 
Encontro com escritor joão morgado
joaocarlosoliveiracamurca
 
Introduction to Functional Programming with Scala
Daniel Cukier
 
Tjänster.unicode
Sari Salmi
 
Functions In Scala
Knoldus Inc.
 
Genetically Modified Soybean Seed Patent Expiry
Beroe Inc - Advantage Procurement
 
Procurement Intelligence Priming Procurement Leaders for Business Power Play
Beroe Inc - Advantage Procurement
 
3D Printing: An emerging trend and its impact on the MRO supply chain | Beroe...
Beroe Inc - Advantage Procurement
 
J unit introduction
Radim Pavlicek
 
Conflict Minerals - A Supply Chain Check for Electronic OEMs Industry
Beroe Inc - Advantage Procurement
 
Java patterns in Scala
Radim Pavlicek
 
Scala functions
Kunal Cholera
 
Brazil telecom market next treasure trove for MVNO's
Beroe Inc - Advantage Procurement
 
Will china remain a low cost country?
Beroe Inc - Advantage Procurement
 
The Future of Procurement - End of Business as Usual
Beroe Inc - Advantage Procurement
 
China's Resources Acquisition
Beroe Inc - Advantage Procurement
 
Genome editing tools article
Beroe Inc - Advantage Procurement
 
Nagoya Protocol and its Implications on Pharmaceutical Industry
Beroe Inc - Advantage Procurement
 
Propy-LENE Supply! Go Green! On-Purpose Technologies for the Future
Beroe Inc - Advantage Procurement
 
Ad

Similar to Scala taxonomy (20)

PDF
Introduction to Scala
Aleksandar Prokopec
 
PPTX
Scala
suraj_atreya
 
PDF
Scala 2013 review
Sagie Davidovich
 
ODP
Introduction to Scala
Lorenzo Dematté
 
PPT
Scala presentation by Aleksandar Prokopec
Loïc Descotte
 
PDF
Scala coated JVM
Stuart Roebuck
 
PDF
An Introduction to Scala (2014)
William Narmontas
 
PPT
SDC - Einführung in Scala
Christian Baranowski
 
PDF
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
PDF
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
PDF
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
PDF
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
ODP
Scala introduction
Alf Kristian Støyle
 
PDF
Scala vs Java 8 in a Java 8 World
BTI360
 
PDF
A bit about Scala
Vladimir Parfinenko
 
KEY
ddd+scala
潤一 加藤
 
PPT
Extractors & Implicit conversions
Knoldus Inc.
 
PDF
ハイブリッド言語Scalaを使う
bpstudy
 
PDF
Scala - en bedre og mere effektiv Java?
Jesper Kamstrup Linnet
 
PDF
여자개발자모임터 6주년 개발 세미나 - Scala Language
Ashal aka JOKER
 
Introduction to Scala
Aleksandar Prokopec
 
Scala 2013 review
Sagie Davidovich
 
Introduction to Scala
Lorenzo Dematté
 
Scala presentation by Aleksandar Prokopec
Loïc Descotte
 
Scala coated JVM
Stuart Roebuck
 
An Introduction to Scala (2014)
William Narmontas
 
SDC - Einführung in Scala
Christian Baranowski
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
Scala introduction
Alf Kristian Støyle
 
Scala vs Java 8 in a Java 8 World
BTI360
 
A bit about Scala
Vladimir Parfinenko
 
ddd+scala
潤一 加藤
 
Extractors & Implicit conversions
Knoldus Inc.
 
ハイブリッド言語Scalaを使う
bpstudy
 
Scala - en bedre og mere effektiv Java?
Jesper Kamstrup Linnet
 
여자개발자모임터 6주년 개발 세미나 - Scala Language
Ashal aka JOKER
 
Ad

Recently uploaded (20)

PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 

Scala taxonomy