SlideShare a Scribd company logo
1 of 28
Download to read offline
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

The Ring programming language version 1.7 book - Part 34 of 196
The Ring programming language version 1.7 book - Part 34 of 196The Ring programming language version 1.7 book - Part 34 of 196
The Ring programming language version 1.7 book - Part 34 of 196Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 183 of 189
The Ring programming language version 1.6 book - Part 183 of 189The Ring programming language version 1.6 book - Part 183 of 189
The Ring programming language version 1.6 book - Part 183 of 189Mahmoud Samir Fayed
 
The Ring programming language version 1.5.2 book - Part 32 of 181
The Ring programming language version 1.5.2 book - Part 32 of 181The Ring programming language version 1.5.2 book - Part 32 of 181
The Ring programming language version 1.5.2 book - Part 32 of 181Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 40 of 189
The Ring programming language version 1.6 book - Part 40 of 189The Ring programming language version 1.6 book - Part 40 of 189
The Ring programming language version 1.6 book - Part 40 of 189Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 22 of 84
The Ring programming language version 1.2 book - Part 22 of 84The Ring programming language version 1.2 book - Part 22 of 84
The Ring programming language version 1.2 book - Part 22 of 84Mahmoud Samir Fayed
 
Java8 stream
Java8 streamJava8 stream
Java8 streamkoji lin
 
The Ring programming language version 1.5.1 book - Part 36 of 180
The Ring programming language version 1.5.1 book - Part 36 of 180The Ring programming language version 1.5.1 book - Part 36 of 180
The Ring programming language version 1.5.1 book - Part 36 of 180Mahmoud Samir Fayed
 
The Ring programming language version 1.5.2 book - Part 30 of 181
The Ring programming language version 1.5.2 book - Part 30 of 181The Ring programming language version 1.5.2 book - Part 30 of 181
The Ring programming language version 1.5.2 book - Part 30 of 181Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 20 of 84
The Ring programming language version 1.2 book - Part 20 of 84The Ring programming language version 1.2 book - Part 20 of 84
The Ring programming language version 1.2 book - Part 20 of 84Mahmoud Samir Fayed
 
Introduction to functional programming using Ocaml
Introduction to functional programming using OcamlIntroduction to functional programming using Ocaml
Introduction to functional programming using Ocamlpramode_ce
 
The Ring programming language version 1.5.1 book - Part 29 of 180
The Ring programming language version 1.5.1 book - Part 29 of 180The Ring programming language version 1.5.1 book - Part 29 of 180
The Ring programming language version 1.5.1 book - Part 29 of 180Mahmoud Samir Fayed
 
Futures e abstração - QCon São Paulo 2015
Futures e abstração - QCon São Paulo 2015Futures e abstração - QCon São Paulo 2015
Futures e abstração - QCon São Paulo 2015Leonardo Borges
 
The Ring programming language version 1.5.4 book - Part 33 of 185
The Ring programming language version 1.5.4 book - Part 33 of 185The Ring programming language version 1.5.4 book - Part 33 of 185
The Ring programming language version 1.5.4 book - Part 33 of 185Mahmoud Samir Fayed
 
Ruslan Shevchenko - Property based testing
Ruslan Shevchenko - Property based testingRuslan Shevchenko - Property based testing
Ruslan Shevchenko - Property based testingIevgenii Katsan
 
The Ring programming language version 1.7 book - Part 40 of 196
The Ring programming language version 1.7 book - Part 40 of 196The Ring programming language version 1.7 book - Part 40 of 196
The Ring programming language version 1.7 book - Part 40 of 196Mahmoud Samir Fayed
 
Introduction to Scala for JCConf Taiwan
Introduction to Scala for JCConf TaiwanIntroduction to Scala for JCConf Taiwan
Introduction to Scala for JCConf TaiwanJimin Hsieh
 
The Ring programming language version 1.4 book - Part 29 of 30
The Ring programming language version 1.4 book - Part 29 of 30The Ring programming language version 1.4 book - Part 29 of 30
The Ring programming language version 1.4 book - Part 29 of 30Mahmoud Samir Fayed
 
The Ring programming language version 1.5.4 book - Part 31 of 185
The Ring programming language version 1.5.4 book - Part 31 of 185The Ring programming language version 1.5.4 book - Part 31 of 185
The Ring programming language version 1.5.4 book - Part 31 of 185Mahmoud Samir Fayed
 

What's hot (20)

The Ring programming language version 1.7 book - Part 34 of 196
The Ring programming language version 1.7 book - Part 34 of 196The Ring programming language version 1.7 book - Part 34 of 196
The Ring programming language version 1.7 book - Part 34 of 196
 
The Ring programming language version 1.6 book - Part 183 of 189
The Ring programming language version 1.6 book - Part 183 of 189The Ring programming language version 1.6 book - Part 183 of 189
The Ring programming language version 1.6 book - Part 183 of 189
 
The Ring programming language version 1.5.2 book - Part 32 of 181
The Ring programming language version 1.5.2 book - Part 32 of 181The Ring programming language version 1.5.2 book - Part 32 of 181
The Ring programming language version 1.5.2 book - Part 32 of 181
 
Introduction to-scala
Introduction to-scalaIntroduction to-scala
Introduction to-scala
 
The Ring programming language version 1.6 book - Part 40 of 189
The Ring programming language version 1.6 book - Part 40 of 189The Ring programming language version 1.6 book - Part 40 of 189
The Ring programming language version 1.6 book - Part 40 of 189
 
The Ring programming language version 1.2 book - Part 22 of 84
The Ring programming language version 1.2 book - Part 22 of 84The Ring programming language version 1.2 book - Part 22 of 84
The Ring programming language version 1.2 book - Part 22 of 84
 
Java8 stream
Java8 streamJava8 stream
Java8 stream
 
The Ring programming language version 1.5.1 book - Part 36 of 180
The Ring programming language version 1.5.1 book - Part 36 of 180The Ring programming language version 1.5.1 book - Part 36 of 180
The Ring programming language version 1.5.1 book - Part 36 of 180
 
The Ring programming language version 1.5.2 book - Part 30 of 181
The Ring programming language version 1.5.2 book - Part 30 of 181The Ring programming language version 1.5.2 book - Part 30 of 181
The Ring programming language version 1.5.2 book - Part 30 of 181
 
The Ring programming language version 1.2 book - Part 20 of 84
The Ring programming language version 1.2 book - Part 20 of 84The Ring programming language version 1.2 book - Part 20 of 84
The Ring programming language version 1.2 book - Part 20 of 84
 
Introduction to functional programming using Ocaml
Introduction to functional programming using OcamlIntroduction to functional programming using Ocaml
Introduction to functional programming using Ocaml
 
The Ring programming language version 1.5.1 book - Part 29 of 180
The Ring programming language version 1.5.1 book - Part 29 of 180The Ring programming language version 1.5.1 book - Part 29 of 180
The Ring programming language version 1.5.1 book - Part 29 of 180
 
Java cheatsheet
Java cheatsheetJava cheatsheet
Java cheatsheet
 
Futures e abstração - QCon São Paulo 2015
Futures e abstração - QCon São Paulo 2015Futures e abstração - QCon São Paulo 2015
Futures e abstração - QCon São Paulo 2015
 
The Ring programming language version 1.5.4 book - Part 33 of 185
The Ring programming language version 1.5.4 book - Part 33 of 185The Ring programming language version 1.5.4 book - Part 33 of 185
The Ring programming language version 1.5.4 book - Part 33 of 185
 
Ruslan Shevchenko - Property based testing
Ruslan Shevchenko - Property based testingRuslan Shevchenko - Property based testing
Ruslan Shevchenko - Property based testing
 
The Ring programming language version 1.7 book - Part 40 of 196
The Ring programming language version 1.7 book - Part 40 of 196The Ring programming language version 1.7 book - Part 40 of 196
The Ring programming language version 1.7 book - Part 40 of 196
 
Introduction to Scala for JCConf Taiwan
Introduction to Scala for JCConf TaiwanIntroduction to Scala for JCConf Taiwan
Introduction to Scala for JCConf Taiwan
 
The Ring programming language version 1.4 book - Part 29 of 30
The Ring programming language version 1.4 book - Part 29 of 30The Ring programming language version 1.4 book - Part 29 of 30
The Ring programming language version 1.4 book - Part 29 of 30
 
The Ring programming language version 1.5.4 book - Part 31 of 185
The Ring programming language version 1.5.4 book - Part 31 of 185The Ring programming language version 1.5.4 book - Part 31 of 185
The Ring programming language version 1.5.4 book - Part 31 of 185
 

Viewers also liked

Internal combustion engine
Internal combustion engineInternal combustion engine
Internal combustion engineDeepak Chaand
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaDaniel Cukier
 
Tjänster.unicode
Tjänster.unicodeTjänster.unicode
Tjänster.unicodeSari Salmi
 
Functions In Scala
Functions In Scala Functions In Scala
Functions In Scala Knoldus Inc.
 
Procurement Intelligence Priming Procurement Leaders for Business Power Play
Procurement Intelligence  Priming Procurement Leaders for Business Power PlayProcurement Intelligence  Priming Procurement Leaders for Business Power Play
Procurement Intelligence Priming Procurement Leaders for Business Power PlayBeroe Inc - Advantage Procurement
 
3D Printing: An emerging trend and its impact on the MRO supply chain | Beroe...
3D Printing: An emerging trend and its impact on the MRO supply chain | Beroe...3D Printing: An emerging trend and its impact on the MRO supply chain | Beroe...
3D Printing: An emerging trend and its impact on the MRO supply chain | Beroe...Beroe Inc - Advantage Procurement
 
Conflict Minerals - A Supply Chain Check for Electronic OEMs Industry
Conflict Minerals - A Supply Chain Check for Electronic OEMs IndustryConflict Minerals - A Supply Chain Check for Electronic OEMs Industry
Conflict Minerals - A Supply Chain Check for Electronic OEMs IndustryBeroe Inc - Advantage Procurement
 
Java patterns in Scala
Java patterns in ScalaJava patterns in Scala
Java patterns in ScalaRadim Pavlicek
 
Nagoya Protocol and its Implications on Pharmaceutical Industry
Nagoya Protocol and its Implications on Pharmaceutical IndustryNagoya Protocol and its Implications on Pharmaceutical Industry
Nagoya Protocol and its Implications on Pharmaceutical IndustryBeroe Inc - Advantage Procurement
 
Propy-LENE Supply! Go Green! On-Purpose Technologies for the Future
Propy-LENE Supply! Go Green! On-Purpose Technologies for the FuturePropy-LENE Supply! Go Green! On-Purpose Technologies for the Future
Propy-LENE Supply! Go Green! On-Purpose Technologies for the FutureBeroe Inc - Advantage Procurement
 

Viewers also liked (20)

Web ui testing
Web ui testingWeb ui testing
Web ui testing
 
Internal combustion engine
Internal combustion engineInternal combustion engine
Internal combustion engine
 
Encontro com escritor joão morgado
Encontro com escritor joão morgadoEncontro com escritor joão morgado
Encontro com escritor joão morgado
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scala
 
Tjänster.unicode
Tjänster.unicodeTjänster.unicode
Tjänster.unicode
 
Functions In Scala
Functions In Scala Functions In Scala
Functions In Scala
 
Genetically Modified Soybean Seed Patent Expiry
Genetically Modified Soybean Seed Patent ExpiryGenetically Modified Soybean Seed Patent Expiry
Genetically Modified Soybean Seed Patent Expiry
 
Procurement Intelligence Priming Procurement Leaders for Business Power Play
Procurement Intelligence  Priming Procurement Leaders for Business Power PlayProcurement Intelligence  Priming Procurement Leaders for Business Power Play
Procurement Intelligence Priming Procurement Leaders for Business Power Play
 
3D Printing: An emerging trend and its impact on the MRO supply chain | Beroe...
3D Printing: An emerging trend and its impact on the MRO supply chain | Beroe...3D Printing: An emerging trend and its impact on the MRO supply chain | Beroe...
3D Printing: An emerging trend and its impact on the MRO supply chain | Beroe...
 
J unit introduction
J unit introductionJ unit introduction
J unit introduction
 
Conflict Minerals - A Supply Chain Check for Electronic OEMs Industry
Conflict Minerals - A Supply Chain Check for Electronic OEMs IndustryConflict Minerals - A Supply Chain Check for Electronic OEMs Industry
Conflict Minerals - A Supply Chain Check for Electronic OEMs Industry
 
Java patterns in Scala
Java patterns in ScalaJava patterns in Scala
Java patterns in Scala
 
Scala functions
Scala functionsScala functions
Scala functions
 
Brazil telecom market next treasure trove for MVNO's
Brazil telecom market next treasure trove for MVNO'sBrazil telecom market next treasure trove for MVNO's
Brazil telecom market next treasure trove for MVNO's
 
Will china remain a low cost country?
Will china remain a low cost country?Will china remain a low cost country?
Will china remain a low cost country?
 
The Future of Procurement - End of Business as Usual
The Future of Procurement - End of Business as UsualThe Future of Procurement - End of Business as Usual
The Future of Procurement - End of Business as Usual
 
China's Resources Acquisition
China's Resources AcquisitionChina's Resources Acquisition
China's Resources Acquisition
 
Genome editing tools article
Genome editing tools   articleGenome editing tools   article
Genome editing tools article
 
Nagoya Protocol and its Implications on Pharmaceutical Industry
Nagoya Protocol and its Implications on Pharmaceutical IndustryNagoya Protocol and its Implications on Pharmaceutical Industry
Nagoya Protocol and its Implications on Pharmaceutical Industry
 
Propy-LENE Supply! Go Green! On-Purpose Technologies for the Future
Propy-LENE Supply! Go Green! On-Purpose Technologies for the FuturePropy-LENE Supply! Go Green! On-Purpose Technologies for the Future
Propy-LENE Supply! Go Green! On-Purpose Technologies for the Future
 

Similar to Scala taxonomy

Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecLoïc Descotte
 
An Introduction to Scala (2014)
An Introduction to Scala (2014)An Introduction to Scala (2014)
An Introduction to Scala (2014)William Narmontas
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
Scala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldScala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldBTI360
 
Extractors & Implicit conversions
Extractors & Implicit conversionsExtractors & Implicit conversions
Extractors & Implicit conversionsKnoldus Inc.
 
ハイブリッド言語Scalaを使う
ハイブリッド言語Scalaを使うハイブリッド言語Scalaを使う
ハイブリッド言語Scalaを使うbpstudy
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Jesper Kamstrup Linnet
 
여자개발자모임터 6주년 개발 세미나 - Scala Language
여자개발자모임터 6주년 개발 세미나 - Scala Language여자개발자모임터 6주년 개발 세미나 - Scala Language
여자개발자모임터 6주년 개발 세미나 - Scala LanguageAshal aka JOKER
 

Similar to Scala taxonomy (20)

Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Scala
ScalaScala
Scala
 
Scala 2013 review
Scala 2013 reviewScala 2013 review
Scala 2013 review
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar Prokopec
 
Scala coated JVM
Scala coated JVMScala coated JVM
Scala coated JVM
 
An Introduction to Scala (2014)
An Introduction to Scala (2014)An Introduction to Scala (2014)
An Introduction to Scala (2014)
 
SDC - Einführung in Scala
SDC - Einführung in ScalaSDC - Einführung in Scala
SDC - Einführung in Scala
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
Scala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldScala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 World
 
A bit about Scala
A bit about ScalaA bit about Scala
A bit about Scala
 
ddd+scala
ddd+scaladdd+scala
ddd+scala
 
Extractors & Implicit conversions
Extractors & Implicit conversionsExtractors & Implicit conversions
Extractors & Implicit conversions
 
ハイブリッド言語Scalaを使う
ハイブリッド言語Scalaを使うハイブリッド言語Scalaを使う
ハイブリッド言語Scalaを使う
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?
 
여자개발자모임터 6주년 개발 세미나 - Scala Language
여자개발자모임터 6주년 개발 세미나 - Scala Language여자개발자모임터 6주년 개발 세미나 - Scala Language
여자개발자모임터 6주년 개발 세미나 - Scala Language
 

Recently uploaded

Why Agile? - A handbook behind Agile Evolution
Why Agile? - A handbook behind Agile EvolutionWhy Agile? - A handbook behind Agile Evolution
Why Agile? - A handbook behind Agile EvolutionDEEPRAJ PATHAK
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkPixlogix Infotech
 
Women in Automation 2024: Career session - explore career paths in automation
Women in Automation 2024: Career session - explore career paths in automationWomen in Automation 2024: Career session - explore career paths in automation
Women in Automation 2024: Career session - explore career paths in automationDianaGray10
 
Arti Languages Pre Seed Pitchdeck 2024.pdf
Arti Languages Pre Seed Pitchdeck 2024.pdfArti Languages Pre Seed Pitchdeck 2024.pdf
Arti Languages Pre Seed Pitchdeck 2024.pdfwill854175
 
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)Mark Simos
 
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Jeffrey Haguewood
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Kaya Weers
 
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...amber724300
 
Which standard is best for your content?
Which standard is best for your content?Which standard is best for your content?
Which standard is best for your content?Rustici Software
 
Deliver Latency Free Customer Experience
Deliver Latency Free Customer ExperienceDeliver Latency Free Customer Experience
Deliver Latency Free Customer ExperienceOpsTree solutions
 
Introduction-to-Wazuh-and-its-integration.pptx
Introduction-to-Wazuh-and-its-integration.pptxIntroduction-to-Wazuh-and-its-integration.pptx
Introduction-to-Wazuh-and-its-integration.pptxmprakaash5
 
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...Karmanjay Verma
 
Software Security in the Real World w/Kelsey Hightower
Software Security in the Real World w/Kelsey HightowerSoftware Security in the Real World w/Kelsey Hightower
Software Security in the Real World w/Kelsey HightowerAnchore
 
All These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFAll These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFMichael Gough
 
Landscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdfLandscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdfAarwolf Industries LLC
 
Tetracrom printing process for packaging with CMYK+
Tetracrom printing process for packaging with CMYK+Tetracrom printing process for packaging with CMYK+
Tetracrom printing process for packaging with CMYK+Antonio de Llamas
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024TopCSSGallery
 
full stack practical assignment msc cs.pdf
full stack practical assignment msc cs.pdffull stack practical assignment msc cs.pdf
full stack practical assignment msc cs.pdfHulkTheDevil
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 

Recently uploaded (20)

Why Agile? - A handbook behind Agile Evolution
Why Agile? - A handbook behind Agile EvolutionWhy Agile? - A handbook behind Agile Evolution
Why Agile? - A handbook behind Agile Evolution
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App Framework
 
Women in Automation 2024: Career session - explore career paths in automation
Women in Automation 2024: Career session - explore career paths in automationWomen in Automation 2024: Career session - explore career paths in automation
Women in Automation 2024: Career session - explore career paths in automation
 
Arti Languages Pre Seed Pitchdeck 2024.pdf
Arti Languages Pre Seed Pitchdeck 2024.pdfArti Languages Pre Seed Pitchdeck 2024.pdf
Arti Languages Pre Seed Pitchdeck 2024.pdf
 
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
 
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)
 
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
 
Which standard is best for your content?
Which standard is best for your content?Which standard is best for your content?
Which standard is best for your content?
 
Deliver Latency Free Customer Experience
Deliver Latency Free Customer ExperienceDeliver Latency Free Customer Experience
Deliver Latency Free Customer Experience
 
Introduction-to-Wazuh-and-its-integration.pptx
Introduction-to-Wazuh-and-its-integration.pptxIntroduction-to-Wazuh-and-its-integration.pptx
Introduction-to-Wazuh-and-its-integration.pptx
 
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
 
Software Security in the Real World w/Kelsey Hightower
Software Security in the Real World w/Kelsey HightowerSoftware Security in the Real World w/Kelsey Hightower
Software Security in the Real World w/Kelsey Hightower
 
All These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFAll These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDF
 
Landscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdfLandscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdf
 
Tetracrom printing process for packaging with CMYK+
Tetracrom printing process for packaging with CMYK+Tetracrom printing process for packaging with CMYK+
Tetracrom printing process for packaging with CMYK+
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024
 
full stack practical assignment msc cs.pdf
full stack practical assignment msc cs.pdffull stack practical assignment msc cs.pdf
full stack practical assignment msc cs.pdf
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 

Scala taxonomy

  • 1. Scala taxonomy explained from Java programmer Radim.Pavlicek@gmail.com
  • 2. Agenda ● OO Features ● Pattern matching ● Functional Programming ● Actors ● Futures ● Implicits
  • 3. Data Transfer Objets class Person { private String firstName; String getFirstName() { return firstName; } void setFirstName(String aFirstName) { this.firstName = aFirstName; } }
  • 4. toString @Override public String toString() { return "Person{" + "firstName='" + firstName + ''' + ", lastName='" + lastName + ''' + '}'; }
  • 5. 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 }
  • 6. hashCode @Override public int hashCode() { int result = firstName.hashCode(); result = 31 * result + lastName.hashCode(); return result; }
  • 7. 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; }
  • 8. Case Classes case class Person(firstName: String = "Bill", lastName :String = "Gates") val me = Person(lastName = "Crosby") ● Immutable ● toString, equals, hashCode ● person.copy(lastName="Malkin")
  • 9. Laziness in Java Integer cached = null; Integer getExpensiveComputation() { if (null == cached) { cached = doCount(); } return cached; }
  • 10. Lazy Definition lazy val count = doCount()
  • 11. 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") ) } }
  • 12. Java: Singletons public class Singleton { private Singleton INSTANCE = null; private Singleton() {} public Singleton getInstance() { if (null == INSTANCE) { INSTANCE = new Singleton() } }
  • 13. 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" }
  • 14. 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.
  • 15. 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) } }
  • 16. scala.annotation.switch (ch: @switch) match { case 'a' if eof => println("a with oef") case 'b' => println("b") case 'c' => println("c") }
  • 19. 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
  • 20. 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 }
  • 21. 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 }
  • 22. 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 }
  • 23. 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)
  • 25. 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 }
  • 27. 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)