Slide 1

Slide 1 text

scala dla programistów ruby :) Tymon Tobolski http://teamon.eu

Slide 2

Slide 2 text

Scala - ??? • OOP + FP • statycznie typowany • kompilowany do JVM • intergacja z Java

Slide 3

Slide 3 text

scala jest trudna trait FilterMonadic[+A, +Repr] { def map[B, That](f: A => B)(implicit bf: CanBuildFrom[Repr, B, That]): That // ... } def zipWithIndexIf[T](list: List[T])(pred: T => Boolean): List[(T, Option[Int])] = { type R = List[(T, Option[Int])] def doZip(zipped: R, left: List[T], index: Int): R = left match { case x :: xs if pred(x) => doZip((x, Some(index)) :: zipped, xs, index + 1) case x :: xs => doZip((x, None) :: zipped, xs, index) case Nil => zipped } doZip(Nil, list, 0).reverse }

Slide 4

Slide 4 text

scala jest trudna trait FilterMonadic[+A, +Repr] { def map[B, That](f: A => B)(implicit bf: CanBuildFrom[Repr, B, That]): That // ... } def zipWithIndexIf[T](list: List[T])(pred: T => Boolean): List[(T, Option[Int])] = { type R = List[(T, Option[Int])] def doZip(zipped: R, left: List[T], index: Int): R = left match { case x :: xs if pred(x) => doZip((x, Some(index)) :: zipped, xs, index + 1) case x :: xs => doZip((x, None) :: zipped, xs, index) case Nil => zipped } doZip(Nil, list, 0).reverse }

Slide 5

Slide 5 text

serio? List(1,2,3) map { e => e * 2 } // List[Int] = List(2, 4, 6)

Slide 6

Slide 6 text

serio? List(1,2,3) map { e => e * 2 } // List[Int] = List(2, 4, 6) List(1,2,3) map (_*2) // List[Int] = List(2, 4, 6) List(1,2,3) map (2*) // List[Int] = List(2, 4, 6) List(1,2,3) filter { _ > 1 } // List[Int] = List(2, 3) (1 to 10) foreach { e => println(e) } (1 to 10) foreach println

Slide 7

Slide 7 text

Monkey patching 2.days

Slide 8

Slide 8 text

Monkey patching Implicit conversions implicit def Int2Time(i: Int) = new { def days = i * 24* 60 * 60 } 2.days // 172800

Slide 9

Slide 9 text

Modules / Traits module T1 def foo "foo" end end module T2 def bar "bar" end end class A include T1 include T2 end a = A.new a.foo # => "foo" a.bar # => "bar"

Slide 10

Slide 10 text

Modules / Traits trait T1 { def foo = "foo" } trait T2 { def bar = "bar" } class A extends T1 with T2 val a = new A a.foo // "foo" a.bar // "bar" module T1 def foo "foo" end end module T2 def bar "bar" end end class A include T1 include T2 end a = A.new a.foo # => "foo" a.bar # => "bar"

Slide 11

Slide 11 text

Singleton class A def foo "instance" end class << self def foo "singleton" end end end A.new.foo # => "instance" A.foo # => "singleton"

Slide 12

Slide 12 text

Singleton class A { def foo = "instance" } object A { def foo "singleton" } new A().foo // "instance" A.foo // "singleton" class A def foo "instance" end class << self def foo "singleton" end end end A.new.foo # => "instance" A.foo # => "singleton"

Slide 13

Slide 13 text

Duck typing class Duck def quack def walk end class Dove def quack def walk end class Cat end def quack_and_walk(animal) animal.quack animal.walk end quack_and_walk Duck.new quack_and_walk Dove.new quack_and_walk Cat.new # NoMethodError

Slide 14

Slide 14 text

Duck typing class Duck { def quack def walk } class Dove { def quack def walk } class Cat def quackAndWalk(a: { def quack; def walk }) = { a.quack a.walk } quackAndWalk(new Duck) quackAndWalk(new Dove) quackAndWalk(new Cat) // Compile error class Duck def quack def walk end class Dove def quack def walk end class Cat end def quack_and_walk(animal) animal.quack animal.walk end quack_and_walk Duck.new quack_and_walk Dove.new quack_and_walk Cat.new # NoMethodError

Slide 15

Slide 15 text

DSL class StackSpec extends FlatSpec with ShouldMatchers { "A Stack" should "pop values in last-in-first-out order" in { val stack = new Stack[Int] stack.push(1) stack.push(2) stack.pop() should equal (2) stack.pop() should equal (1) } it should "throw NoSuchElementException if an empty stack is popped" in { val emptyStack = new Stack[String] evaluating { emptyStack.pop() } should produce [NoSuchElementException] } } http://scalatest.org

Slide 16

Slide 16 text

DSL class FilterExample extends ScalatraFilter { get("/hello") {

Hello world

} post("/world") { // ... } } https://github.com/scalatra/scalatra

Slide 17

Slide 17 text

Immutability val list = List(2, 3) val list2 = 1 :: list println(list) // List(2, 3) println(list2) // List(1, 2, 3)

Slide 18

Slide 18 text

Built-in concurency scala> (1 to 10) foreach println 1 2 3 4 5 6 7 8 9 10

Slide 19

Slide 19 text

Built-in concurency scala> (1 to 10).par foreach println 1 2 3 4 5 8 9 10 6 7

Slide 20

Slide 20 text

Functional style scala> Map(1 -> 2) res9: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2)

Slide 21

Slide 21 text

Functional style scala> Map(1 -> 2) res9: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2) scala> Map(1 -> 2) get _ res10: (Int) => Option[Int] =

Slide 22

Slide 22 text

Functional style scala> Map(1 -> 2) res9: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2) scala> Map(1 -> 2) get _ res10: (Int) => Option[Int] = scala> Map(1 -> 2) get 1 res11: Option[Int] = Some(2)

Slide 23

Slide 23 text

Functional style scala> Map(1 -> 2) res9: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2) scala> Map(1 -> 2) get _ res10: (Int) => Option[Int] = scala> Map(1 -> 2) get 1 res11: Option[Int] = Some(2) scala> Map(1 -> 2) get 3 res12: Option[Int] = None

Slide 24

Slide 24 text

Functional style scala> Map(1 -> 2) res9: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2) scala> Map(1 -> 2) get _ res10: (Int) => Option[Int] = scala> Map(1 -> 2) get 1 res11: Option[Int] = Some(2) scala> Map(1 -> 2) get 3 res12: Option[Int] = None scala> Map(1 -> 2) get 1 map (1+) res13: Option[Int] = Some(3)

Slide 25

Slide 25 text

Functional style scala> Map(1 -> 2) res9: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2) scala> Map(1 -> 2) get _ res10: (Int) => Option[Int] = scala> Map(1 -> 2) get 1 res11: Option[Int] = Some(2) scala> Map(1 -> 2) get 3 res12: Option[Int] = None scala> Map(1 -> 2) get 1 map (1+) res13: Option[Int] = Some(3) scala> Map(1 -> 2) get 3 map (1+) res14: Option[Int] = None

Slide 26

Slide 26 text

Functional style scala> Map(1 -> 2) res9: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2) scala> Map(1 -> 2) get _ res10: (Int) => Option[Int] = scala> Map(1 -> 2) get 1 res11: Option[Int] = Some(2) scala> Map(1 -> 2) get 3 res12: Option[Int] = None scala> Map(1 -> 2) get 1 map (1+) res13: Option[Int] = Some(3) scala> Map(1 -> 2) get 3 map (1+) res14: Option[Int] = None scala> Map(1 -> 2) get 1 map (1+) getOrElse 5 res15: Int = 3

Slide 27

Slide 27 text

Functional style scala> Map(1 -> 2) res9: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2) scala> Map(1 -> 2) get _ res10: (Int) => Option[Int] = scala> Map(1 -> 2) get 1 res11: Option[Int] = Some(2) scala> Map(1 -> 2) get 3 res12: Option[Int] = None scala> Map(1 -> 2) get 1 map (1+) res13: Option[Int] = Some(3) scala> Map(1 -> 2) get 3 map (1+) res14: Option[Int] = None scala> Map(1 -> 2) get 1 map (1+) getOrElse 5 res15: Int = 3 scala> Map(1 -> 2) get 3 map (1+) getOrElse 5 res16: Int = 5

Slide 28

Slide 28 text

Web • Rails • Sinatra • ... • Play! • Lift • Scalatra • ...

Slide 29

Slide 29 text

• Actors • STM • Fault Tolerance • ...

Slide 30

Slide 30 text

• http://scala-lang.org • http://akka.io • http://typesafe.com • http://scala.playframework.org/ • #scala @ irc.freenode.net • #scala.pl @ irc.freenode.net