Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
scaladla programistów ruby :)   Tymon Tobolski   http://teamon.eu
Scala - ???• OOP + FP• statycznie typowany• kompilowany do JVM• intergacja z Java
scala jest trudnatrait FilterMonadic[+A, +Repr] {  def map[B, That](f: A => B)(implicit bf: CanBuildFrom[Repr, B, That]): ...
scala jest trudnatrait FilterMonadic[+A, +Repr] {  def map[B, That](f: A => B)(implicit bf: CanBuildFrom[Repr, B, That]): ...
serio?List(1,2,3) map { e => e * 2 } // List[Int] = List(2, 4, 6)
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,...
Monkey patching      2.days
Monkey patching  Implicitconversionsimplicit def Int2Time(i: Int) = new {  def days = i * 24* 60 * 60}2.days // 172800
Modules / Traitsmodule T1  def foo    "foo"  endendmodule T2  def bar    "bar"  endendclass A  include T1  include T2enda ...
Modules / Traitsmodule T1  def foo    "foo"  end                   trait T1 {end                     def foo = "foo"      ...
Singletonclass A  def foo    "instance"  end    class << self    def foo      "singleton"    end  endendA.new.foo # => "in...
Singletonclass A  def foo    "instance"               class A {  end                          def foo = "instance"        ...
Duck typingclass Duck  def quack  def walkendclass Dove  def quack  def walkendclass Catenddef quack_and_walk(animal)  ani...
Duck typingclass Duck                               class Duck {  def quack                                def quack  def ...
DSLclass StackSpec extends FlatSpec with ShouldMatchers {  "A Stack" should "pop values in last-in-first-out order" in {  ...
DSLclass FilterExample extends ScalatraFilter {  get("/hello") {    <p>      Hello world    </p>  }  post("/world") {    /...
Immutabilityval list = List(2, 3)val list2 = 1 :: listprintln(list) // List(2, 3)println(list2) // List(1, 2, 3)
Built-in concurencyscala> (1 to 10) foreach println12345678910
Built-in concurencyscala> (1 to 10).par foreach println12345891067
Functional stylescala> Map(1 -> 2)res9: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2)
Functional stylescala> Map(1 -> 2)res9: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2)scala> Map(1 -> 2) get _res10...
Functional stylescala> Map(1 -> 2)res9: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2)scala> Map(1 -> 2) get _res10...
Functional stylescala> Map(1 -> 2)res9: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2)scala> Map(1 -> 2) get _res10...
Functional stylescala> Map(1 -> 2)res9: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2)scala> Map(1 -> 2) get _res10...
Functional stylescala> Map(1 -> 2)res9: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2)scala> Map(1 -> 2) get _res10...
Functional stylescala> Map(1 -> 2)res9: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2)scala> Map(1 -> 2) get _res10...
Functional stylescala> Map(1 -> 2)res9: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2)scala> Map(1 -> 2) get _res10...
Web                  • Play!• Rails                  • Lift• Sinatra                  • Scalatra• ...                  • ...
• Actors• STM• Fault Tolerance• ...
• http://scala-lang.org• http://akka.io• http://typesafe.com• http://scala.playframework.org/• #scala @ irc.freenode.net• ...
Upcoming SlideShare
Loading in …5
×

Scala for ruby programmers

2,124 views

Published on

Presentation from DRUG given to (mostly) Ruby programmers about the Scala programming language

Published in: Technology, Education
  • Be the first to comment

Scala for ruby programmers

  1. 1. scaladla programistów ruby :) Tymon Tobolski http://teamon.eu
  2. 2. Scala - ???• OOP + FP• statycznie typowany• kompilowany do JVM• intergacja z Java
  3. 3. scala jest trudnatrait 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}
  4. 4. scala jest trudnatrait 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}
  5. 5. serio?List(1,2,3) map { e => e * 2 } // List[Int] = List(2, 4, 6)
  6. 6. 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
  7. 7. Monkey patching 2.days
  8. 8. Monkey patching Implicitconversionsimplicit def Int2Time(i: Int) = new { def days = i * 24* 60 * 60}2.days // 172800
  9. 9. Modules / Traitsmodule T1  def foo    "foo"  endendmodule T2  def bar    "bar"  endendclass A  include T1  include T2enda = A.newa.foo # => "foo"a.bar # => "bar"
  10. 10. Modules / Traitsmodule T1  def foo    "foo"  end trait T1 {end   def foo = "foo" }module T2  def bar trait T2 {    "bar"   def bar = "bar"  end }end class A extends T1 with T2class A  include T1 val a = new A  include T2 a.foo // "foo"end a.bar // "bar"a = A.newa.foo # => "foo"a.bar # => "bar"
  11. 11. Singletonclass A  def foo    "instance"  end    class << self    def foo      "singleton"    end  endendA.new.foo # => "instance"A.foo # => "singleton"
  12. 12. Singletonclass A  def foo    "instance" class A {  end   def foo = "instance"   }  class << self    def foo object A {      "singleton"   def foo "singleton"    end }  endend new A().foo // "instance" A.foo // "singleton"A.new.foo # => "instance"A.foo # => "singleton"
  13. 13. Duck typingclass Duck  def quack  def walkendclass Dove  def quack  def walkendclass Catenddef quack_and_walk(animal)  animal.quack  animal.walkendquack_and_walk Duck.newquack_and_walk Dove.newquack_and_walk Cat.new # NoMethodError
  14. 14. Duck typingclass Duck class Duck {  def quack   def quack  def walk   def walkend }class Dove class Dove {  def quack   def quack  def walk   def walkend }class Cat class Catend def quackAndWalk(a: { def quack; def walk }) = {def quack_and_walk(animal)   a.quack  animal.quack   a.walk  animal.walk }end quackAndWalk(new Duck)quack_and_walk Duck.new quackAndWalk(new Dove)quack_and_walk Dove.new quackAndWalk(new Cat) // Compile errorquack_and_walk Cat.new # NoMethodError
  15. 15. DSLclass 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
  16. 16. DSLclass FilterExample extends ScalatraFilter {  get("/hello") {    <p>      Hello world    </p>  }  post("/world") { // ...  }} https://github.com/scalatra/scalatra
  17. 17. Immutabilityval list = List(2, 3)val list2 = 1 :: listprintln(list) // List(2, 3)println(list2) // List(1, 2, 3)
  18. 18. Built-in concurencyscala> (1 to 10) foreach println12345678910
  19. 19. Built-in concurencyscala> (1 to 10).par foreach println12345891067
  20. 20. Functional stylescala> Map(1 -> 2)res9: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2)
  21. 21. Functional stylescala> Map(1 -> 2)res9: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2)scala> Map(1 -> 2) get _res10: (Int) => Option[Int] = <function1>
  22. 22. Functional stylescala> Map(1 -> 2)res9: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2)scala> Map(1 -> 2) get _res10: (Int) => Option[Int] = <function1>scala> Map(1 -> 2) get 1res11: Option[Int] = Some(2)
  23. 23. Functional stylescala> Map(1 -> 2)res9: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2)scala> Map(1 -> 2) get _res10: (Int) => Option[Int] = <function1>scala> Map(1 -> 2) get 1res11: Option[Int] = Some(2)scala> Map(1 -> 2) get 3res12: Option[Int] = None
  24. 24. Functional stylescala> Map(1 -> 2)res9: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2)scala> Map(1 -> 2) get _res10: (Int) => Option[Int] = <function1>scala> Map(1 -> 2) get 1res11: Option[Int] = Some(2)scala> Map(1 -> 2) get 3res12: Option[Int] = Nonescala> Map(1 -> 2) get 1 map (1+)res13: Option[Int] = Some(3)
  25. 25. Functional stylescala> Map(1 -> 2)res9: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2)scala> Map(1 -> 2) get _res10: (Int) => Option[Int] = <function1>scala> Map(1 -> 2) get 1res11: Option[Int] = Some(2)scala> Map(1 -> 2) get 3res12: Option[Int] = Nonescala> Map(1 -> 2) get 1 map (1+)res13: Option[Int] = Some(3)scala> Map(1 -> 2) get 3 map (1+)res14: Option[Int] = None
  26. 26. Functional stylescala> Map(1 -> 2)res9: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2)scala> Map(1 -> 2) get _res10: (Int) => Option[Int] = <function1>scala> Map(1 -> 2) get 1res11: Option[Int] = Some(2)scala> Map(1 -> 2) get 3res12: Option[Int] = Nonescala> Map(1 -> 2) get 1 map (1+)res13: Option[Int] = Some(3)scala> Map(1 -> 2) get 3 map (1+)res14: Option[Int] = Nonescala> Map(1 -> 2) get 1 map (1+) getOrElse 5res15: Int = 3
  27. 27. Functional stylescala> Map(1 -> 2)res9: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2)scala> Map(1 -> 2) get _res10: (Int) => Option[Int] = <function1>scala> Map(1 -> 2) get 1res11: Option[Int] = Some(2)scala> Map(1 -> 2) get 3res12: Option[Int] = Nonescala> Map(1 -> 2) get 1 map (1+)res13: Option[Int] = Some(3)scala> Map(1 -> 2) get 3 map (1+)res14: Option[Int] = Nonescala> Map(1 -> 2) get 1 map (1+) getOrElse 5res15: Int = 3scala> Map(1 -> 2) get 3 map (1+) getOrElse 5res16: Int = 5
  28. 28. Web • Play!• Rails • Lift• Sinatra • Scalatra• ... • ...
  29. 29. • Actors• STM• Fault Tolerance• ...
  30. 30. • http://scala-lang.org• http://akka.io• http://typesafe.com• http://scala.playframework.org/• #scala @ irc.freenode.net• #scala.pl @ irc.freenode.net

×