SlideShare una empresa de Scribd logo
1 de 30
scala
dla programistów ruby :)



   Tymon Tobolski
   http://teamon.eu
Scala - ???

• OOP + FP
• statycznie typowany
• kompilowany do JVM
• intergacja z Java
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
}
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
}
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,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
Monkey patching


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

2.days // 172800
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"
Modules / Traits
module 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 T2
class A
  include T1
                   val a = new A
  include T2
                   a.foo // "foo"
end
                   a.bar // "bar"
a = A.new
a.foo # => "foo"
a.bar # => "bar"
Singleton
class A
  def foo
    "instance"
  end
  
  class << self
    def foo
      "singleton"
    end
  end
end

A.new.foo # => "instance"
A.foo     # => "singleton"
Singleton
class A
  def foo
    "instance"               class A {
  end                          def foo = "instance"
                             }
  class << self
    def foo                  object A {
      "singleton"              def foo "singleton"
    end                      }
  end
end                          new A().foo // "instance"
                             A.foo       // "singleton"
A.new.foo # => "instance"
A.foo     # => "singleton"
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
Duck typing
class Duck                               class Duck {
  def quack                                def quack
  def walk                                 def walk
end                                      }

class Dove                               class Dove {
  def quack                                def quack
  def walk                                 def walk
end                                      }

class Cat                                class Cat
end
                                         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 error
quack_and_walk Cat.new # NoMethodError
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
DSL
class FilterExample extends ScalatraFilter {
  get("/hello") {
    <p>
      Hello world
    </p>
  }

  post("/world") {
    // ...
  }
}



           https://github.com/scalatra/scalatra
Immutability

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

println(list) // List(2, 3)
println(list2) // List(1, 2, 3)
Built-in concurency
scala> (1 to 10) foreach println
1
2
3
4
5
6
7
8
9
10
Built-in concurency
scala> (1 to 10).par foreach println
1
2
3
4
5
8
9
10
6
7
Functional style
scala> Map(1 -> 2)
res9: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2)
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] = <function1>
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] = <function1>
scala> Map(1 -> 2) get 1
res11: Option[Int] = Some(2)
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] = <function1>
scala> Map(1 -> 2) get 1
res11: Option[Int] = Some(2)
scala> Map(1 -> 2) get 3
res12: Option[Int] = None
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] = <function1>
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)
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] = <function1>
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
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] = <function1>
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
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] = <function1>
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
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
• #scala.pl @ irc.freenode.net

Más contenido relacionado

La actualidad más candente

Monad Transformers In The Wild
Monad Transformers In The WildMonad Transformers In The Wild
Monad Transformers In The Wild
StackMob Inc
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar Prokopec
Loïc Descotte
 
The Death of Final Tagless
The Death of Final TaglessThe Death of Final Tagless
The Death of Final Tagless
John De Goes
 

La actualidad más candente (20)

Scala for Jedi
Scala for JediScala for Jedi
Scala for Jedi
 
Scala for curious
Scala for curiousScala for curious
Scala for curious
 
Introduction to Scala for Java Developers
Introduction to Scala for Java DevelopersIntroduction to Scala for Java Developers
Introduction to Scala for Java Developers
 
Scala collections api expressivity and brevity upgrade from java
Scala collections api  expressivity and brevity upgrade from javaScala collections api  expressivity and brevity upgrade from java
Scala collections api expressivity and brevity upgrade from java
 
Scala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type Classes
 
Exploring ZIO Prelude: The game changer for typeclasses in Scala
Exploring ZIO Prelude: The game changer for typeclasses in ScalaExploring ZIO Prelude: The game changer for typeclasses in Scala
Exploring ZIO Prelude: The game changer for typeclasses in Scala
 
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
 
Joy of scala
Joy of scalaJoy of scala
Joy of scala
 
First-Class Patterns
First-Class PatternsFirst-Class Patterns
First-Class Patterns
 
Type classes 101 - classification beyond inheritance
Type classes 101 - classification beyond inheritanceType classes 101 - classification beyond inheritance
Type classes 101 - classification beyond inheritance
 
Scala for Java Developers - Intro
Scala for Java Developers - IntroScala for Java Developers - Intro
Scala for Java Developers - Intro
 
Intro to Functional Programming in Scala
Intro to Functional Programming in ScalaIntro to Functional Programming in Scala
Intro to Functional Programming in Scala
 
Exploring type level programming in Scala
Exploring type level programming in ScalaExploring type level programming in Scala
Exploring type level programming in Scala
 
Monad Transformers In The Wild
Monad Transformers In The WildMonad Transformers In The Wild
Monad Transformers In The Wild
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar Prokopec
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)
 
Scala Intro
Scala IntroScala Intro
Scala Intro
 
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics
Quark: A Purely-Functional Scala DSL for Data Processing & AnalyticsQuark: A Purely-Functional Scala DSL for Data Processing & Analytics
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics
 
The Death of Final Tagless
The Death of Final TaglessThe Death of Final Tagless
The Death of Final Tagless
 
The Ring programming language version 1.4.1 book - Part 9 of 31
The Ring programming language version 1.4.1 book - Part 9 of 31The Ring programming language version 1.4.1 book - Part 9 of 31
The Ring programming language version 1.4.1 book - Part 9 of 31
 

Similar a Scala for ruby programmers

(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
Tomasz Wrobel
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meet
Mario Fusco
 

Similar a Scala for ruby programmers (20)

Demystifying functional programming with Scala
Demystifying functional programming with ScalaDemystifying functional programming with Scala
Demystifying functional programming with Scala
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
 
ハイブリッド言語Scalaを使う
ハイブリッド言語Scalaを使うハイブリッド言語Scalaを使う
ハイブリッド言語Scalaを使う
 
Monadologie
MonadologieMonadologie
Monadologie
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meet
 
学生向けScalaハンズオンテキスト
学生向けScalaハンズオンテキスト学生向けScalaハンズオンテキスト
学生向けScalaハンズオンテキスト
 
An introduction to scala
An introduction to scalaAn introduction to scala
An introduction to scala
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Scala Functional Patterns
Scala Functional PatternsScala Functional Patterns
Scala Functional Patterns
 
Kotlin Advanced - Apalon Kotlin Sprint Part 3
Kotlin Advanced - Apalon Kotlin Sprint Part 3Kotlin Advanced - Apalon Kotlin Sprint Part 3
Kotlin Advanced - Apalon Kotlin Sprint Part 3
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
Scala Paradigms
Scala ParadigmsScala Paradigms
Scala Paradigms
 
Scala
ScalaScala
Scala
 
A Brief Intro to Scala
A Brief Intro to ScalaA Brief Intro to Scala
A Brief Intro to Scala
 
GeoGebra JavaScript CheatSheet
GeoGebra JavaScript CheatSheetGeoGebra JavaScript CheatSheet
GeoGebra JavaScript CheatSheet
 
Meet scala
Meet scalaMeet scala
Meet scala
 
Scala @ TechMeetup Edinburgh
Scala @ TechMeetup EdinburghScala @ TechMeetup Edinburgh
Scala @ TechMeetup Edinburgh
 
Are we ready to Go?
Are we ready to Go?Are we ready to Go?
Are we ready to Go?
 
Hw09 Hadoop + Clojure
Hw09   Hadoop + ClojureHw09   Hadoop + Clojure
Hw09 Hadoop + Clojure
 
Functional sudoku
Functional sudokuFunctional sudoku
Functional sudoku
 

Último

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Último (20)

Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 

Scala for ruby programmers

  • 1. scala dla programistów ruby :) Tymon Tobolski http://teamon.eu
  • 2. Scala - ??? • OOP + FP • statycznie typowany • kompilowany do JVM • intergacja z Java
  • 3. 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 }
  • 4. 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 }
  • 5. serio? List(1,2,3) map { e => e * 2 } // List[Int] = List(2, 4, 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
  • 8. Monkey patching Implicit conversions implicit def Int2Time(i: Int) = new { def days = i * 24* 60 * 60 } 2.days // 172800
  • 9. 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"
  • 10. Modules / Traits module 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 T2 class A   include T1 val a = new A   include T2 a.foo // "foo" end a.bar // "bar" a = A.new a.foo # => "foo" a.bar # => "bar"
  • 11. Singleton class A   def foo     "instance"   end      class << self     def foo       "singleton"     end   end end A.new.foo # => "instance" A.foo # => "singleton"
  • 12. Singleton class A   def foo     "instance" class A {   end   def foo = "instance"    }   class << self     def foo object A {       "singleton"   def foo "singleton"     end }   end end new A().foo // "instance" A.foo // "singleton" A.new.foo # => "instance" A.foo # => "singleton"
  • 13. 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
  • 14. Duck typing class Duck class Duck {   def quack   def quack   def walk   def walk end } class Dove class Dove {   def quack   def quack   def walk   def walk end } class Cat class Cat end 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 error quack_and_walk Cat.new # NoMethodError
  • 15. 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
  • 16. DSL class FilterExample extends ScalatraFilter {   get("/hello") {     <p>       Hello world     </p>   }   post("/world") { // ...   } } https://github.com/scalatra/scalatra
  • 17. Immutability val list = List(2, 3) val list2 = 1 :: list println(list) // List(2, 3) println(list2) // List(1, 2, 3)
  • 18. Built-in concurency scala> (1 to 10) foreach println 1 2 3 4 5 6 7 8 9 10
  • 19. Built-in concurency scala> (1 to 10).par foreach println 1 2 3 4 5 8 9 10 6 7
  • 20. Functional style scala> Map(1 -> 2) res9: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2)
  • 21. 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] = <function1>
  • 22. 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] = <function1> scala> Map(1 -> 2) get 1 res11: Option[Int] = Some(2)
  • 23. 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] = <function1> scala> Map(1 -> 2) get 1 res11: Option[Int] = Some(2) scala> Map(1 -> 2) get 3 res12: Option[Int] = None
  • 24. 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] = <function1> 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)
  • 25. 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] = <function1> 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
  • 26. 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] = <function1> 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
  • 27. 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] = <function1> 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
  • 28. Web • Play! • Rails • Lift • Sinatra • Scalatra • ... • ...
  • 29. • Actors • STM • Fault Tolerance • ...
  • 30. • http://scala-lang.org • http://akka.io • http://typesafe.com • http://scala.playframework.org/ • #scala @ irc.freenode.net • #scala.pl @ irc.freenode.net

Notas del editor

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n