scala. inception
TRANSCRIPT
SCALA.INCEPTION.
@alexkorotkikh
WHY SCALA?
•Not����������� ������������������ expressive
JAVA IS...
•Not����������� ������������������ expressive•No����������� ������������������ lambdas
JAVA IS...
•Not����������� ������������������ expressive•No����������� ������������������ lambdas•No����������� ������������������ mixins
JAVA IS...
•Not����������� ������������������ expressive•No����������� ������������������ lambdas•No����������� ������������������ mixins•Nothing����������� ������������������ fun����������� ������������������ :(
JAVA IS...
•Not����������� ������������������ expressive•No����������� ������������������ lambdas•No����������� ������������������ mixins•Nothing����������� ������������������ fun����������� ������������������ :(•Poor����������� ������������������ standard����������� ������������������ library
JAVA IS...
•Not����������� ������������������ expressive•No����������� ������������������ lambdas•No����������� ������������������ mixins•Nothing����������� ������������������ fun����������� ������������������ :(•Poor����������� ������������������ standard����������� ������������������ library•That’s����������� ������������������ why����������� ������������������ we����������� ������������������ have����������� ������������������ Guava����������� ������������������ and����������� ������������������ Apache����������� ������������������ Commons
JAVA IS...
•Still����������� ������������������ JVM����������� ������������������ based
BUT SCALA IS...
•Still����������� ������������������ JVM����������� ������������������ based•World����������� ������������������ class����������� ������������������ performance
BUT SCALA IS...
•Still����������� ������������������ JVM����������� ������������������ based•World����������� ������������������ class����������� ������������������ performance•Variety����������� ������������������ of����������� ������������������ libraries����������� ������������������ and����������� ������������������ components
BUT SCALA IS...
•Still����������� ������������������ JVM����������� ������������������ based•World����������� ������������������ class����������� ������������������ performance•Variety����������� ������������������ of����������� ������������������ libraries����������� ������������������ and����������� ������������������ components•Expressive
BUT SCALA IS...
•Still����������� ������������������ JVM����������� ������������������ based•World����������� ������������������ class����������� ������������������ performance•Variety����������� ������������������ of����������� ������������������ libraries����������� ������������������ and����������� ������������������ components•Expressive•All����������� ������������������ cool����������� ������������������ things����������� ������������������ you’ve����������� ������������������ heard����������� ������������������ about
BUT SCALA IS...
•Still����������� ������������������ JVM����������� ������������������ based•World����������� ������������������ class����������� ������������������ performance•Variety����������� ������������������ of����������� ������������������ libraries����������� ������������������ and����������� ������������������ components•Expressive•All����������� ������������������ cool����������� ������������������ things����������� ������������������ you’ve����������� ������������������ heard����������� ������������������ about•Compact����������� ������������������ while����������� ������������������ still����������� ������������������ readable����������� ������������������ code
BUT SCALA IS...
FIRST STEPSFIRST STEPS
ONCE UPON A TIME...
ONCE UPON A TIME...We����������� ������������������ had����������� ������������������ a����������� ������������������ module
ONCE UPON A TIME...We����������� ������������������ had����������� ������������������ a����������� ������������������ modulefull����������� ������������������ of����������� ������������������ magic
ONCE UPON A TIME...We����������� ������������������ had����������� ������������������ a����������� ������������������ modulefull����������� ������������������ of����������� ������������������ magic
They����������� ������������������ called����������� ������������������ it����������� ������������������ business����������� ������������������ logic
DROOLS DECISION TABLE
Do����������� ������������������ not����������� ������������������ ever����������� ������������������ try����������� ������������������ to����������� ������������������ repeat����������� ������������������ it����������� ������������������ at����������� ������������������ home
DROOLS DECISION TABLE
Do����������� ������������������ not����������� ������������������ ever����������� ������������������ try����������� ������������������ to����������� ������������������ repeat����������� ������������������ it����������� ������������������ at����������� ������������������ home
We����������� ������������������ had����������� ������������������ to����������� ������������������ rewrite����������� ������������������ it����������� ������������������
DROOLS DECISION TABLE
Do����������� ������������������ not����������� ������������������ ever����������� ������������������ try����������� ������������������ to����������� ������������������ repeat����������� ������������������ it����������� ������������������ at����������� ������������������ home
We����������� ������������������ had����������� ������������������ to����������� ������������������ rewrite����������� ������������������ it����������� ������������������ for����������� ������������������ the����������� ������������������ sake����������� ������������������ of����������� ������������������ readability����������� ������������������
and����������� ������������������ testability
DROOLS DECISION TABLE
Do����������� ������������������ not����������� ������������������ ever����������� ������������������ try����������� ������������������ to����������� ������������������ repeat����������� ������������������ it����������� ������������������ at����������� ������������������ home
We����������� ������������������ had����������� ������������������ to����������� ������������������ rewrite����������� ������������������ it����������� ������������������ for����������� ������������������ the����������� ������������������ sake����������� ������������������ of����������� ������������������ readability����������� ������������������
and����������� ������������������ testability
So����������� ������������������ we’ve����������� ������������������ decided����������� ������������������ to����������� ������������������ give����������� ������������������ Scala����������� ������������������ a����������� ������������������ chance
FIRST FUN
trait Logging { val log: Logger = LoggerFactory getLogger getClass}
class ImportantLogic { self: { val log: Logger } => def doSomething() { log warn "Something important" }}
trait Logging { val log: Logger = LoggerFactory getLogger getClass}
class ImportantLogic { self: { val log: Logger } => def doSomething() { log warn "Something important" }}
trait Logging { val log: Logger = LoggerFactory getLogger getClass}
val logic = new ImportantLogic with Logginglogic.doSomething()
class ImportantLogic { self: { val log: Logger } => def doSomething() { log warn "Something important" }}
trait Logging { val log: Logger = LoggerFactory getLogger getClass}
val logic = new ImportantLogic with Logginglogic.doSomething()
val logic2 = new ImportantLogic // compile errorlogic2.doSomething()
(1 to 20) // [1, 20]
(1 to 20) // [1, 20] .map(d => d * d) // 1, 4, 9, ..., 400
(1 to 20) // [1, 20] .map(d => d * d) // 1, 4, 9, ..., 400 .filter(_ % 3 == 0) // 9, 36, 81, ...
(1 to 20) // [1, 20] .map(d => d * d) // 1, 4, 9, ..., 400 .filter(_ % 3 == 0) // 9, 36, 81, ... .foldLeft(0)(_ + _) // 819
val x: Int = ...
val x: Int = ...
(1 to 20) // [1, 20]
val x: Int = ...
(1 to 20) // [1, 20] .find(_ == x) // Option[Int]
val x: Int = ...
(1 to 20) // [1, 20] .find(_ == x) // Option[Int] .map(_ / 2) // Option[Int], still
val x: Int = ...
(1 to 20) // [1, 20] .find(_ == x) // Option[Int] .map(_ / 2) // Option[Int], still .getOrElse(0) // x / 2 if 1 >= x >= 20
val x: Int = ...
(1 to 20) // [1, 20] .find(_ == x) // Option[Int] .map(_ / 2) // Option[Int], still .getOrElse(0) // x / 2 if 1 >= x >= 20 // 0 otherwise
def patternMatching(x: Any) = x match {
}
def patternMatching(x: Any) = x match {
}
case 1 | 2 | 3 => “one or two or three”
def patternMatching(x: Any) = x match {
}
case 1 | 2 | 3 => “one or two or three” case d: Double => “double is here”
def patternMatching(x: Any) = x match {
}
case 1 | 2 | 3 => “one or two or three” case d: Double => “double is here” case s: String if s.size < 5 => “short str”
def patternMatching(x: Any) = x match {
}
case 1 | 2 | 3 => “one or two or three” case d: Double => “double is here” case s: String if s.size < 5 => “short str” case _ => “anything else”
•Implicit����������� ������������������ conversions
AND MUCH MORE
•Implicit����������� ������������������ conversions
•Parallel����������� ������������������ collections
AND MUCH MORE
•Implicit����������� ������������������ conversions
•Parallel����������� ������������������ collections
•XML����������� ������������������ as����������� ������������������ built-in����������� ������������������ data����������� ������������������ type
AND MUCH MORE
•Implicit����������� ������������������ conversions
•Parallel����������� ������������������ collections
•XML����������� ������������������ as����������� ������������������ built-in����������� ������������������ data����������� ������������������ type
•Singleton����������� ������������������ as����������� ������������������ build-in����������� ������������������ data����������� ������������������ type
AND MUCH MORE
•Implicit����������� ������������������ conversions
•Parallel����������� ������������������ collections
•XML����������� ������������������ as����������� ������������������ built-in����������� ������������������ data����������� ������������������ type
•Singleton����������� ������������������ as����������� ������������������ build-in����������� ������������������ data����������� ������������������ type
•Macros����������� ������������������ (from����������� ������������������ 2.10.x)
AND MUCH MORE
FIRST FAILS
SO EASY...
Functional code is hard to read at first.
def sort(list: List[Int]): List[Int] = list match { case Nil => Nil case head :: tail => sort(tail.filter(_ < head)) ::: head :: sort(tail.filter(_ >= head)) }
Functional code is hard to read at first.
def sort(list: List[Int]): List[Int] = list match { case Nil => Nil case head :: tail => sort(tail.filter(_ < head)) ::: head :: sort(tail.filter(_ >= head)) }
Functional code is hard to read at first.
No backward compatibilities.
No backward compatibilities.
Libraries compiled with 2.9.x compiler may not work with 2.10.x. And vice versa.
No backward compatibilities.
Libraries compiled with 2.9.x compiler may not work with 2.10.x. And vice versa.
SUMMARY
•All����������� ������������������ new����������� ������������������ features����������� ������������������ are����������� ������������������ written����������� ������������������ in����������� ������������������ Scala
WE
•All����������� ������������������ new����������� ������������������ features����������� ������������������ are����������� ������������������ written����������� ������������������ in����������� ������������������ Scala
•More����������� ������������������ then����������� ������������������ 30%����������� ������������������ of����������� ������������������ production����������� ������������������ code
WE
•All����������� ������������������ new����������� ������������������ features����������� ������������������ are����������� ������������������ written����������� ������������������ in����������� ������������������ Scala
•More����������� ������������������ then����������� ������������������ 30%����������� ������������������ of����������� ������������������ production����������� ������������������ code
•We����������� ������������������ became����������� ������������������ more����������� ������������������ productive
WE
•All����������� ������������������ new����������� ������������������ features����������� ������������������ are����������� ������������������ written����������� ������������������ in����������� ������������������ Scala
•More����������� ������������������ then����������� ������������������ 30%����������� ������������������ of����������� ������������������ production����������� ������������������ code
•We����������� ������������������ became����������� ������������������ more����������� ������������������ productive
•And����������� ������������������ we����������� ������������������ enjoy����������� ������������������ it����������� ������������������ =)
WE
SCALA•Compact,����������� ������������������ expressive����������� ������������������ and����������� ������������������ readable����������� ������������������ code
SCALA•Compact,����������� ������������������ expressive����������� ������������������ and����������� ������������������ readable����������� ������������������ code
•...ok,����������� ������������������ readable����������� ������������������ after����������� ������������������ some����������� ������������������ time
SCALA•Compact,����������� ������������������ expressive����������� ������������������ and����������� ������������������ readable����������� ������������������ code
•...ok,����������� ������������������ readable����������� ������������������ after����������� ������������������ some����������� ������������������ time
•All����������� ������������������ the����������� ������������������ power����������� ������������������ of����������� ������������������ JVM����������� ������������������ and����������� ������������������ Java����������� ������������������ libs
SCALA•Compact,����������� ������������������ expressive����������� ������������������ and����������� ������������������ readable����������� ������������������ code
•...ok,����������� ������������������ readable����������� ������������������ after����������� ������������������ some����������� ������������������ time
•All����������� ������������������ the����������� ������������������ power����������� ������������������ of����������� ������������������ JVM����������� ������������������ and����������� ������������������ Java����������� ������������������ libs
•...altogether����������� ������������������ with����������� ������������������ functional����������� ������������������ approach����������� ������������������
•Check����������� ������������������ out����������� ������������������ some����������� ������������������ quick����������� ������������������ tutorial����������� ������������������ (e.g.����������� ������������������ Twitter����������� ������������������ Scala����������� ������������������ School)
YOU
•Check����������� ������������������ out����������� ������������������ some����������� ������������������ quick����������� ������������������ tutorial����������� ������������������ (e.g.����������� ������������������ Twitter����������� ������������������ Scala����������� ������������������ School)
•Separate����������� ������������������ a����������� ������������������ feature����������� ������������������ (you����������� ������������������ don’t����������� ������������������ need����������� ������������������ to����������� ������������������ rewrite����������� ������������������ whole����������� ������������������ project����������� ������������������ at����������� ������������������ once!)
YOU
•Check����������� ������������������ out����������� ������������������ some����������� ������������������ quick����������� ������������������ tutorial����������� ������������������ (e.g.����������� ������������������ Twitter����������� ������������������ Scala����������� ������������������ School)
•Separate����������� ������������������ a����������� ������������������ feature����������� ������������������ (you����������� ������������������ don’t����������� ������������������ need����������� ������������������ to����������� ������������������ rewrite����������� ������������������ whole����������� ������������������ project����������� ������������������ at����������� ������������������ once!)
•Start����������� ������������������ with����������� ������������������ tests����������� ������������������ (if����������� ������������������ usually����������� ������������������ you����������� ������������������ don’t)
YOU
•Check����������� ������������������ out����������� ������������������ some����������� ������������������ quick����������� ������������������ tutorial����������� ������������������ (e.g.����������� ������������������ Twitter����������� ������������������ Scala����������� ������������������ School)
•Separate����������� ������������������ a����������� ������������������ feature����������� ������������������ (you����������� ������������������ don’t����������� ������������������ need����������� ������������������ to����������� ������������������ rewrite����������� ������������������ whole����������� ������������������ project����������� ������������������ at����������� ������������������ once!)
•Start����������� ������������������ with����������� ������������������ tests����������� ������������������ (if����������� ������������������ usually����������� ������������������ you����������� ������������������ don’t)
•Enjoy����������� ������������������ it
YOU
THANK YOU!
@alexkorotkikh