SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

Introduction to Scala for Java Developers
Introduction to Scala for Java DevelopersIntroduction to Scala for Java Developers
Introduction to Scala for Java DevelopersMichael Galpin
 
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 javaIndicThreads
 
Scala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesTomer Gabel
 
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 ScalaJorge Vásquez
 
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 WorldBTI360
 
First-Class Patterns
First-Class PatternsFirst-Class Patterns
First-Class PatternsJohn De Goes
 
Type classes 101 - classification beyond inheritance
Type classes 101 - classification beyond inheritanceType classes 101 - classification beyond inheritance
Type classes 101 - classification beyond inheritanceAlexey Raga
 
Scala for Java Developers - Intro
Scala for Java Developers - IntroScala for Java Developers - Intro
Scala for Java Developers - IntroDavid Copeland
 
Intro to Functional Programming in Scala
Intro to Functional Programming in ScalaIntro to Functional Programming in Scala
Intro to Functional Programming in ScalaShai Yallin
 
Exploring type level programming in Scala
Exploring type level programming in ScalaExploring type level programming in Scala
Exploring type level programming in ScalaJorge Vásquez
 
Monad Transformers In The Wild
Monad Transformers In The WildMonad Transformers In The Wild
Monad Transformers In The WildStackMob Inc
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecLoïc Descotte
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Jonas Bonér
 
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 & AnalyticsJohn De Goes
 
The Death of Final Tagless
The Death of Final TaglessThe Death of Final Tagless
The Death of Final TaglessJohn De Goes
 
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 31Mahmoud Samir Fayed
 

Was ist angesagt? (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
 

Ähnlich wie Scala for ruby programmers

Demystifying functional programming with Scala
Demystifying functional programming with ScalaDemystifying functional programming with Scala
Demystifying functional programming with ScalaDenis
 
(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を使う
ハイブリッド言語Scalaを使うハイブリッド言語Scalaを使う
ハイブリッド言語Scalaを使うbpstudy
 
Monadologie
MonadologieMonadologie
Monadologieleague
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meetMario Fusco
 
学生向けScalaハンズオンテキスト
学生向けScalaハンズオンテキスト学生向けScalaハンズオンテキスト
学生向けScalaハンズオンテキストOpt Technologies
 
Scala Functional Patterns
Scala Functional PatternsScala Functional Patterns
Scala Functional Patternsleague
 
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 3Kirill Rozov
 
Scala introduction
Scala introductionScala introduction
Scala introductionvito jeng
 
A Brief Intro to Scala
A Brief Intro to ScalaA Brief Intro to Scala
A Brief Intro to ScalaTim Underwood
 
GeoGebra JavaScript CheatSheet
GeoGebra JavaScript CheatSheetGeoGebra JavaScript CheatSheet
GeoGebra JavaScript CheatSheetJose Perez
 
Scala @ TechMeetup Edinburgh
Scala @ TechMeetup EdinburghScala @ TechMeetup Edinburgh
Scala @ TechMeetup EdinburghStuart Roebuck
 
Are we ready to Go?
Are we ready to Go?Are we ready to Go?
Are we ready to Go?Adam Dudczak
 

Ähnlich wie 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
 

Kürzlich hochgeladen

Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 

Kürzlich hochgeladen (20)

Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 

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

Hinweis der Redaktion

  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