70
SCALA. INCEPTION. @alexkorotkikh

Scala. Inception

Embed Size (px)

Citation preview

Page 1: Scala. Inception

SCALA.INCEPTION.

@alexkorotkikh

Page 2: Scala. Inception

WHY SCALA?

Page 3: Scala. Inception

•Not����������� ������������������  expressive

JAVA IS...

Page 4: Scala. Inception

•Not����������� ������������������  expressive•No����������� ������������������  lambdas

JAVA IS...

Page 5: Scala. Inception

•Not����������� ������������������  expressive•No����������� ������������������  lambdas•No����������� ������������������  mixins

JAVA IS...

Page 6: Scala. Inception

•Not����������� ������������������  expressive•No����������� ������������������  lambdas•No����������� ������������������  mixins•Nothing����������� ������������������  fun����������� ������������������  :(

JAVA IS...

Page 7: Scala. Inception

•Not����������� ������������������  expressive•No����������� ������������������  lambdas•No����������� ������������������  mixins•Nothing����������� ������������������  fun����������� ������������������  :(•Poor����������� ������������������  standard����������� ������������������  library

JAVA IS...

Page 8: Scala. Inception

•Not����������� ������������������  expressive•No����������� ������������������  lambdas•No����������� ������������������  mixins•Nothing����������� ������������������  fun����������� ������������������  :(•Poor����������� ������������������  standard����������� ������������������  library•That’s����������� ������������������  why����������� ������������������  we����������� ������������������  have����������� ������������������  Guava����������� ������������������  and����������� ������������������  Apache����������� ������������������  Commons

JAVA IS...

Page 9: Scala. Inception

•Still����������� ������������������  JVM����������� ������������������  based

BUT SCALA IS...

Page 10: Scala. Inception

•Still����������� ������������������  JVM����������� ������������������  based•World����������� ������������������  class����������� ������������������  performance

BUT SCALA IS...

Page 11: Scala. Inception

•Still����������� ������������������  JVM����������� ������������������  based•World����������� ������������������  class����������� ������������������  performance•Variety����������� ������������������  of����������� ������������������  libraries����������� ������������������  and����������� ������������������  components

BUT SCALA IS...

Page 12: Scala. Inception

•Still����������� ������������������  JVM����������� ������������������  based•World����������� ������������������  class����������� ������������������  performance•Variety����������� ������������������  of����������� ������������������  libraries����������� ������������������  and����������� ������������������  components•Expressive

BUT SCALA IS...

Page 13: Scala. Inception

•Still����������� ������������������  JVM����������� ������������������  based•World����������� ������������������  class����������� ������������������  performance•Variety����������� ������������������  of����������� ������������������  libraries����������� ������������������  and����������� ������������������  components•Expressive•All����������� ������������������  cool����������� ������������������  things����������� ������������������  you’ve����������� ������������������  heard����������� ������������������  about

BUT SCALA IS...

Page 14: Scala. Inception

•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...

Page 15: Scala. Inception

FIRST STEPSFIRST STEPS

Page 16: Scala. Inception

ONCE UPON A TIME...

Page 17: Scala. Inception

ONCE UPON A TIME...We����������� ������������������  had����������� ������������������  a����������� ������������������  module

Page 18: Scala. Inception

ONCE UPON A TIME...We����������� ������������������  had����������� ������������������  a����������� ������������������  modulefull����������� ������������������  of����������� ������������������  magic

Page 19: Scala. Inception

ONCE UPON A TIME...We����������� ������������������  had����������� ������������������  a����������� ������������������  modulefull����������� ������������������  of����������� ������������������  magic

They����������� ������������������  called����������� ������������������  it����������� ������������������  business����������� ������������������  logic

Page 20: Scala. Inception

DROOLS DECISION TABLE

Do����������� ������������������  not����������� ������������������  ever����������� ������������������  try����������� ������������������  to����������� ������������������  repeat����������� ������������������  it����������� ������������������  at����������� ������������������  home

Page 21: Scala. Inception

DROOLS DECISION TABLE

Do����������� ������������������  not����������� ������������������  ever����������� ������������������  try����������� ������������������  to����������� ������������������  repeat����������� ������������������  it����������� ������������������  at����������� ������������������  home

We����������� ������������������  had����������� ������������������  to����������� ������������������  rewrite����������� ������������������  it����������� ������������������  

Page 22: Scala. Inception

DROOLS DECISION TABLE

Do����������� ������������������  not����������� ������������������  ever����������� ������������������  try����������� ������������������  to����������� ������������������  repeat����������� ������������������  it����������� ������������������  at����������� ������������������  home

We����������� ������������������  had����������� ������������������  to����������� ������������������  rewrite����������� ������������������  it����������� ������������������  for����������� ������������������  the����������� ������������������  sake����������� ������������������  of����������� ������������������  readability����������� ������������������  

and����������� ������������������  testability

Page 23: Scala. Inception

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

Page 24: Scala. Inception

FIRST FUN

Page 25: Scala. Inception

trait Logging { val log: Logger = LoggerFactory getLogger getClass}

Page 26: Scala. Inception

class ImportantLogic { self: { val log: Logger } => def doSomething() { log warn "Something important" }}

trait Logging { val log: Logger = LoggerFactory getLogger getClass}

Page 27: Scala. Inception

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()

Page 28: Scala. Inception

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()

Page 29: Scala. Inception

(1 to 20) // [1, 20]

Page 30: Scala. Inception

(1 to 20) // [1, 20] .map(d => d * d) // 1, 4, 9, ..., 400

Page 31: Scala. Inception

(1 to 20) // [1, 20] .map(d => d * d) // 1, 4, 9, ..., 400 .filter(_ % 3 == 0) // 9, 36, 81, ...

Page 32: Scala. Inception

(1 to 20) // [1, 20] .map(d => d * d) // 1, 4, 9, ..., 400 .filter(_ % 3 == 0) // 9, 36, 81, ... .foldLeft(0)(_ + _) // 819

Page 33: Scala. Inception

val x: Int = ...

Page 34: Scala. Inception

val x: Int = ...

(1 to 20) // [1, 20]

Page 35: Scala. Inception

val x: Int = ...

(1 to 20) // [1, 20] .find(_ == x) // Option[Int]

Page 36: Scala. Inception

val x: Int = ...

(1 to 20) // [1, 20] .find(_ == x) // Option[Int] .map(_ / 2) // Option[Int], still

Page 37: Scala. Inception

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

Page 38: Scala. Inception

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

Page 39: Scala. Inception

def patternMatching(x: Any) = x match {

}

Page 40: Scala. Inception

def patternMatching(x: Any) = x match {

}

case 1 | 2 | 3 => “one or two or three”

Page 41: Scala. Inception

def patternMatching(x: Any) = x match {

}

case 1 | 2 | 3 => “one or two or three” case d: Double => “double is here”

Page 42: Scala. Inception

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”

Page 43: Scala. Inception

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”

Page 44: Scala. Inception

•Implicit����������� ������������������  conversions

AND MUCH MORE

Page 45: Scala. Inception

•Implicit����������� ������������������  conversions

•Parallel����������� ������������������  collections

AND MUCH MORE

Page 46: Scala. Inception

•Implicit����������� ������������������  conversions

•Parallel����������� ������������������  collections

•XML����������� ������������������  as����������� ������������������  built-in����������� ������������������  data����������� ������������������  type

AND MUCH MORE

Page 47: Scala. Inception

•Implicit����������� ������������������  conversions

•Parallel����������� ������������������  collections

•XML����������� ������������������  as����������� ������������������  built-in����������� ������������������  data����������� ������������������  type

•Singleton����������� ������������������  as����������� ������������������  build-in����������� ������������������  data����������� ������������������  type

AND MUCH MORE

Page 48: Scala. Inception

•Implicit����������� ������������������  conversions

•Parallel����������� ������������������  collections

•XML����������� ������������������  as����������� ������������������  built-in����������� ������������������  data����������� ������������������  type

•Singleton����������� ������������������  as����������� ������������������  build-in����������� ������������������  data����������� ������������������  type

•Macros����������� ������������������  (from����������� ������������������  2.10.x)

AND MUCH MORE

Page 49: Scala. Inception

FIRST FAILS

Page 50: Scala. Inception

SO EASY...

Page 51: Scala. Inception

Functional code is hard to read at first.

Page 52: Scala. Inception

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.

Page 53: Scala. Inception

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.

Page 54: Scala. Inception

No backward compatibilities.

Page 55: Scala. Inception

No backward compatibilities.

Libraries compiled with 2.9.x compiler may not work with 2.10.x. And vice versa.

Page 56: Scala. Inception

No backward compatibilities.

Libraries compiled with 2.9.x compiler may not work with 2.10.x. And vice versa.

Page 57: Scala. Inception

SUMMARY

Page 58: Scala. Inception

•All����������� ������������������  new����������� ������������������  features����������� ������������������  are����������� ������������������  written����������� ������������������  in����������� ������������������  Scala

WE

Page 59: Scala. Inception

•All����������� ������������������  new����������� ������������������  features����������� ������������������  are����������� ������������������  written����������� ������������������  in����������� ������������������  Scala

•More����������� ������������������  then����������� ������������������  30%����������� ������������������  of����������� ������������������  production����������� ������������������  code

WE

Page 60: Scala. Inception

•All����������� ������������������  new����������� ������������������  features����������� ������������������  are����������� ������������������  written����������� ������������������  in����������� ������������������  Scala

•More����������� ������������������  then����������� ������������������  30%����������� ������������������  of����������� ������������������  production����������� ������������������  code

•We����������� ������������������  became����������� ������������������  more����������� ������������������  productive

WE

Page 61: Scala. Inception

•All����������� ������������������  new����������� ������������������  features����������� ������������������  are����������� ������������������  written����������� ������������������  in����������� ������������������  Scala

•More����������� ������������������  then����������� ������������������  30%����������� ������������������  of����������� ������������������  production����������� ������������������  code

•We����������� ������������������  became����������� ������������������  more����������� ������������������  productive

•And����������� ������������������  we����������� ������������������  enjoy����������� ������������������  it����������� ������������������  =)

WE

Page 62: Scala. Inception

SCALA•Compact,����������� ������������������  expressive����������� ������������������  and����������� ������������������  readable����������� ������������������  code

Page 63: Scala. Inception

SCALA•Compact,����������� ������������������  expressive����������� ������������������  and����������� ������������������  readable����������� ������������������  code

•...ok,����������� ������������������  readable����������� ������������������  after����������� ������������������  some����������� ������������������  time

Page 64: Scala. Inception

SCALA•Compact,����������� ������������������  expressive����������� ������������������  and����������� ������������������  readable����������� ������������������  code

•...ok,����������� ������������������  readable����������� ������������������  after����������� ������������������  some����������� ������������������  time

•All����������� ������������������  the����������� ������������������  power����������� ������������������  of����������� ������������������  JVM����������� ������������������  and����������� ������������������  Java����������� ������������������  libs

Page 65: Scala. Inception

SCALA•Compact,����������� ������������������  expressive����������� ������������������  and����������� ������������������  readable����������� ������������������  code

•...ok,����������� ������������������  readable����������� ������������������  after����������� ������������������  some����������� ������������������  time

•All����������� ������������������  the����������� ������������������  power����������� ������������������  of����������� ������������������  JVM����������� ������������������  and����������� ������������������  Java����������� ������������������  libs

•...altogether����������� ������������������  with����������� ������������������  functional����������� ������������������  approach����������� ������������������  

Page 66: Scala. Inception

•Check����������� ������������������  out����������� ������������������  some����������� ������������������  quick����������� ������������������  tutorial����������� ������������������  (e.g.����������� ������������������  Twitter����������� ������������������  Scala����������� ������������������  School)

YOU

Page 67: Scala. Inception

•Check����������� ������������������  out����������� ������������������  some����������� ������������������  quick����������� ������������������  tutorial����������� ������������������  (e.g.����������� ������������������  Twitter����������� ������������������  Scala����������� ������������������  School)

•Separate����������� ������������������  a����������� ������������������  feature����������� ������������������  (you����������� ������������������  don’t����������� ������������������  need����������� ������������������  to����������� ������������������  rewrite����������� ������������������  whole����������� ������������������  project����������� ������������������  at����������� ������������������  once!)

YOU

Page 68: Scala. Inception

•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

Page 69: Scala. Inception

•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

Page 70: Scala. Inception

THANK YOU!

@alexkorotkikh