Scala

                             ... a Scalable Language

          06.10.2009
          XPUG Rhein Main
          Mari...
Introduction

          Mario Gleichmann



                   site:   www.mg-informatik.de


                   blog:   '...
Motivation


                        Open your mind ...
               •   Scala vs. Java 9

               •   Functional...
Motivation


                        Open your mind ...
                                              “If Java programmers...
Motivation


                        Open your mind ...
                                            ●   Functions & Closur...
Motivation


                        Open your mind ...
               •   Scala vs. Java 9           “If i were to pick a...
Motivation


                        Open your mind ...
               •   Functional programming for the imperative mind
...
Motivation


                        Open your mind ...
               •   Functional programming for the imperative mind
...
Motivation


                        Open your mind ...
               •   Functional programming for the imperative mind
...
Motivation


                         Open your mind ...
               •    Discover the (new) possibilities ...


      ...
Motivation

                        Open your mind !!!
               •
                   “Scala taught me to program and...
What is Scala ?

                   A programming language ...

      •    Pure Object Oriented


      •    Statically Ty...
What is Scala ?

                        A programming language ...

      •    Pure Object Oriented
                   • ...
What is Scala ?

                   A programming language ...

                                     ''Everything is an Ob...
What is Scala ?

                   A programming language ...

                                     ''Everything is an Ob...
What is Scala ?

                   A programming language ...

                                     ''Everything is an Ob...
What is Scala ?

                     A programming language ...

      •    Pure Object Oriented


      •
              ...
What is Scala ?

                      A programming language ...

      •    Pure Object Oriented


      •    Functional...
What is Scala ?

                      A programming language ...

      •    Pure Object Oriented


      •    Functional...
What is Scala ?

                      A programming language ...

      •    Pure Object Oriented


      •    Functional...
What is Scala ?

                       A programming language ...

      •    Pure Object Oriented


      •    Functiona...
What is Scala ?

                      A programming language ...

      •    Pure Object Oriented


      •    Functional...
What is Scala ?

                         A programming language ...

      •    Pure Object Oriented
                   I...
What is Scala ?

                      A programming language ...

                                               class va...
What is Scala ?

                      A programming language ...

      •    Pure Object Oriented                        ...
What is Scala ?

                      A programming language ...

      •    Pure Object Oriented

                    Ma...
What is Scala ?

                      A programming language ...

      •    Pure Object Oriented


      •    Functional...
What is Scala ?

                      A programming language ...

      •    Pure Object Oriented


      •    Functional...
What is Scala ?

                      A programming language ...

      •    Pure Object Oriented


      •    Functional...
What is Scala ?

                   A programming language ...
                                 Example: Implement your ow...
What is Scala ?

                   A programming language ...
                                 Example: Implement your ow...
What is Scala ?

                   A programming language ...
                                 Example: Implement your ow...
What is Scala ?

                   A programming language ...
                                  Example: Implement your o...
What is Scala ?

                   A programming language ...
                                  Example: Implement your o...
What is Scala ?

                   A programming language ...
                                  Example: Implement your o...
What is Scala ?

                   A programming language ...
                                  Example: Implement your o...
What is Scala ?

                   A programming language ...
                                  Example: Implement your o...
What is Scala ?

                   A programming language ...
                                  Example: Implement your o...
What is Scala ?

                    A programming language ...
                                  Example: Implement your ...
What is Scala ?

                    A programming language ...
                                Example: Implement your ow...
What is Scala ?

                    A programming language ...
                                Example: Implement your ow...
What is Scala ?

                   A programming language ...
                                  Example: Implement your o...
What is Scala ?

                   A programming language ...
                                  Example: Implement your o...
What is Scala ?

                   A programming language ...

      •    Pure Object Oriented


      •    Statically Ty...
What is Scala ?

                   A programming language ...
                                   Refined Type System
    ...
What is Scala ?

                    A programming language ...
                                          Refined Type Sys...
What is Scala ?

                    A programming language ...
                                          Refined Type Sys...
What is Scala ?

                   A programming language ...
                                          Refined Type Syst...
What is Scala ?

                   A programming language ...
                                         Refined Type Syste...
What is Scala ?

                    A programming language ...
                                        Refined Type Syste...
What is Scala ?

                    A programming language ...
                                        Refined Type Syste...
What is Scala ?

                    A programming language ...
                                        Refined Type Syste...
What is Scala ?

                   A programming language ...
                                       Refined Type System
...
What is Scala ?

                   A programming language ...
                                          Refined Type Syst...
What is Scala ?

                   A programming language ...
                                          Refined Type Syst...
What is Scala ?

                   A programming language ...
                                          Refined Type Syst...
What is Scala ?

                   A programming language ...
                                          Refined Type Syst...
What is Scala ?

                    A programming language ...
                                        Refined Type Syste...
What is Scala ?

                    A programming language ...
                                         Refined Type Syst...
What is Scala ?

                    A programming language ...
                                         Refined Type Syst...
What is Scala ?

                    A programming language ...
                                         Refined Type Syst...
What is Scala ?

                   A programming language ...
                                         Refined Type Syste...
What is Scala ?

                   A programming language ...
                                         Refined Type Syste...
What is Scala ?

                   A programming language ...
                                         Refined Type Syste...
What is Scala ?

                   A programming language ...
                                         Refined Type Syste...
What is Scala ?

                   A programming language ...
                                            Refined Type Sy...
What is Scala ?

                   A programming language ...
                                            Refined Type Sy...
What is Scala ?

                   A programming language ...
                                            Refined Type Sy...
What is Scala ?

                   A programming language ...
                                            Refined Type Sy...
What is Scala ?

                   A programming language ...
                                             Refined Type S...
What is Scala ?

                   A programming language ...
                                            Refined Type Sy...
What is Scala ?

                   A programming language ...
                                              Refined Type ...
What is Scala ?

                   A programming language ...
                                       Type inference
     ...
What is Scala ?

                   A programming language ...
                                       Type inference
     ...
What is Scala ?

                   A programming language ...
                                       Type inference
     ...
What is Scala ?

                   A programming language ...
                                       Type inference
     ...
What is Scala ?

                   A programming language ...
                                     Implicit Type conversi...
What is Scala ?

                   A programming language ...
                                        Implicit Type conve...
What is Scala ?

                   A programming language ...
                                        Implicit Type conve...
What is Scala ?

                   A programming language ...
                                        Implicit Type conve...
What is Scala ?

                   A programming language ...

      •    Pure Object Oriented


      •    Statically Ty...
What is Scala ?

                   A programming language ...
                                  ●
                       ...
What is Scala ?

                   A programming language ...
                                  ●
                       ...
What is Scala ?

                   A programming language ...
                                  ●
                       ...
What is Scala ?

                   A programming language ...
                                  ●
                       ...
What is Scala ?

                   A programming language ...
                                  ●
                       ...
What is Scala ?

                   A programming language ...
                                  ●
                       ...
What is Scala ?

                   A programming language ...
                                  ●
                       ...
What is Scala ?

                    A programming language ...
                                                     Closu...
What is Scala ?

                    A programming language ...
                                                     Closu...
What is Scala ?

                    A programming language ...
                                                     Closu...
What is Scala ?

                    A programming language ...
                                                   Closure...
What is Scala ?

                    A programming language ...
                                                     Closu...
What is Scala ?

                    A programming language ...
                                                     Closu...
What is Scala ?

                    A programming language ...
                                                 Currying
...
What is Scala ?

                    A programming language ...
                                                 Currying
...
What is Scala ?

                    A programming language ...
                                                Currying
 ...
What is Scala ?

                    A programming language ...
                                                 Currying
...
What is Scala ?

                    A programming language ...
                                                 Currying
...
What is Scala ?

                    A programming language ...
                                                 Currying
...
What is Scala ?

                    A programming language ...
                                                  Currying...
What is Scala ?

                    A programming language ...
                                                 Currying
...
What is Scala ?

                    A programming language ...
                                                  Currying...
What is Scala ?

                         A programming language ...
                                               Currie...
What is Scala ?

                         A programming language ...
                                               Currie...
What is Scala ?

                         A programming language ...
                                               Currie...
What is Scala ?

                         A programming language ...
                                               Currie...
What is Scala ?

                         A programming language ...
                                               Currie...
What is Scala ?

                         A programming language ...
                                               Currie...
What is Scala ?

                         A programming language ...
                                               Currie...
What is Scala ?

                         A programming language ...
                                              Curried...
What is Scala ?

                         A programming language ...
                                              Curried...
What is Scala ?

                         A programming language ...
                                              Curried...
What is Scala ?

                         A programming language ...
                                              Curried...
What is Scala ?

                         A programming language ...
                                              Curried...
What is Scala ?

                   A programming language ...
                                     OO + FP Fusion
       ...
What is Scala ?

                   A programming language ...
                                        OO + FP Fusion
    ...
What is Scala ?

                   A programming language ...
                                        OO + FP Fusion
    ...
What is Scala ?

                   A programming language ...
                                        OO + FP Fusion
    ...
What is Scala ?

                   A programming language ...
                                        OO + FP Fusion
    ...
What is Scala ?

                   A programming language ...
                                        OO + FP Fusion
    ...
What is Scala ?

                   A programming language ...
                                        OO + FP Fusion
    ...
What is Scala ?

                   A programming language ...

      •    Pure Object Oriented


      •    Statically Ty...
What is Scala ?

                   A programming language ...
                                  ... so does Groovy, Cloju...
What is Scala ?

                   A programming language ...
                                  ... so does Groovy, Cloju...
What is Scala ?

                   A programming language ...
                                  ... so does Groovy, Cloju...
What is Scala ?

                   A programming language ...
                                  ... so does Groovy, Cloju...
What is Scala ?

                   A programming language ...
                                  ... so does Groovy, Cloju...
What is Scala ?

                   A programming language ...

                                  “I can honestly say if s...
Characteristics

     •
         Expressive

     •
         High Level

     •
         Concise

     •
         Extensib...
Characteristics

     •
         Expressive
                      case class Person( name: String, age: Int )

     •
    ...
Characteristics

     •
         Expressive
                       case class Person( name: String, age: Int )

     •
   ...
Characteristics

     •
         Expressive
                       case class Person( name: String, age: Int )

     •
   ...
Characteristics

     •
         Expressive
                       case class Person( name: String, age: Int )

     •
   ...
Characteristics

     •
         Expressive
                       case class Person( name: String, age: Int )

     •
   ...
Characteristics

     •
         Expressive   val bookPrices = Map(
                                 “Prag. Programmer“ ->...
Characteristics

                                                  Create a new Map
     •
         Expressive   val bookP...
Characteristics

                                                    Create a new Map
     •
         Expressive   val boo...
Characteristics

                                                    Create a new Map
     •
         Expressive   val boo...
Characteristics

     •
         Expressive   val bookPrices = Map(
                                     “Prag. Programmer...
Characteristics

     •
         Expressive   val bookPrices = Map(
                                     “Prag. Programmer...
Characteristics

     •
         Expressive

     •
         High Level

     •
         Concise

     •
         Extensib...
Characteristics

                               Upper Case in given name ?     Jav
     •
         Expressive




        ...
Characteristics

                            Upper Case in given name ?    Scal
     •
         Expressive




           ...
Characteristics

                            Upper Case in given name ?      Scal
     •
         Expressive




         ...
Characteristics

                            Upper Case in given name ?        Scal
     •
         Expressive




       ...
Characteristics

                            Upper Case in given name ?      Scal
     •
         Expressive




         ...
Characteristics
                                 Find maximal Distance       Jav
     •
         Expressive




          ...
Characteristics
                                Find maximal Distance          Scal
     •
         Expressive




       ...
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Scala - A Scalable Language
Upcoming SlideShare
Loading in...5
×

Scala - A Scalable Language

5,227

Published on

Introduction to Scala

Published in: Technology
0 Comments
19 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
5,227
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
303
Comments
0
Likes
19
Embeds 0
No embeds

No notes for slide

Scala - A Scalable Language

  1. 1. Scala ... a Scalable Language 06.10.2009 XPUG Rhein Main Mario Gleichmann Mario Gleichmann XPUG Rhein/Main
  2. 2. Introduction Mario Gleichmann site: www.mg-informatik.de blog: 'brain driven development' gleichmann.wordpress.com mail: mario.gleichmann@mg-informatik.de Mario Gleichmann XPUG Rhein/Main
  3. 3. Motivation Open your mind ... • Scala vs. Java 9 • Functional programming for the imperative mind • Discover the (new) possibilities ... Mario Gleichmann XPUG Rhein/Main
  4. 4. Motivation Open your mind ... “If Java programmers want to • Scala vs. Java 9 use features that aren't present • Functional programming forthe language, I think they're in the imperative mind probably best off using another • Discover the (new) possibilities ... language that targets the JVM, such as Scala and Groovy“ Joshua Bloch Mario Gleichmann XPUG Rhein/Main
  5. 5. Motivation Open your mind ... ● Functions & Closures • Scala vs. Java 9 ● Extended Type System • Functional programming for the imperative mind ● Extended Module System • ● Properties Discover the (new) possibilities ... ● Essence over Ceremony ● Extended Control Structs Mario Gleichmann XPUG Rhein/Main
  6. 6. Motivation Open your mind ... • Scala vs. Java 9 “If i were to pick a language • Functional programming foruse today other than Java, to the imperative mind it would be Scala“ • Discover the (new) possibilities ... James Gosling Mario Gleichmann XPUG Rhein/Main
  7. 7. Motivation Open your mind ... • Functional programming for the imperative mind • Discover the possibilities ... Imparative programming is a programming paradigm that describes computation in terms of statements that change a programs state Mario Gleichmann XPUG Rhein/Main
  8. 8. Motivation Open your mind ... • Functional programming for the imperative mind • Discover the possibilities ... Functional programming is a programming paradigm that describes computation as the evaluation of mathematical functions avoiding state and mutable data Mario Gleichmann XPUG Rhein/Main
  9. 9. Motivation Open your mind ... • Functional programming for the imperative mind • Discover the possibilities ... Lazy Evaluation Continuations Monads Recursion Higher Order Functions Closures Currying Immutable Datatypes Mario Gleichmann XPUG Rhein/Main
  10. 10. Motivation Open your mind ... • Discover the (new) possibilities ... Control Structure Abstraction Composition Traits Pattern Matching Type Variance Modularity Type Extentions / Conversions Mario Gleichmann XPUG Rhein/Main
  11. 11. Motivation Open your mind !!! • “Scala taught me to program and reason about programming differently. I stopped thinking in terms of allocating buffers, structs and objects and of changing those pieces in memory. Instead I learned to think about most of my programs as transforming input to output. This change in thinking has lead to lower defect rates, more modular code, and more testable code“ David Pollak Mario Gleichmann XPUG Rhein/Main
  12. 12. What is Scala ? A programming language ... • Pure Object Oriented • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  13. 13. What is Scala ? A programming language ... • Pure Object Oriented • Martin Odersky (EPFL Switzerland) • Functional • Pizza, Fummel & Co. Statically Typed Java Generic • • • javac Reference Compiler • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  14. 14. What is Scala ? A programming language ... ''Everything is an Object'' • Pure Object Oriented 1 + 2 <=> 1.+( 2 ) • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  15. 15. What is Scala ? A programming language ... ''Everything is an Object'' • Pure Object Oriented 1 + 2 <=> 1.+( 2 ) • Statically Typed ''No primitive Types'' 123.hashCode • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  16. 16. What is Scala ? A programming language ... ''Everything is an Object'' • Pure Object Oriented 1 + 2 <=> 1.+( 2 ) • Statically Typed ''No primitive Types'' 123.hashCode • Functional ''Operations are method calls'' • Runs on the JVM actor ! msg <=> actor.!( msg ) Mario Gleichmann XPUG Rhein/Main
  17. 17. What is Scala ? A programming language ... • Pure Object Oriented • public BigInteger factorial( BigInteger n ){ Functional Jav a if( n.equals( BigInteger.ZERO ) return BigInteger.ONE else • Statically Typed return n.multiply( factorial( n.subtract( BigInteger.ONE ) ) ); • Runs on the JVM } Mario Gleichmann XPUG Rhein/Main
  18. 18. What is Scala ? A programming language ... • Pure Object Oriented • Functional Scal a def factorial( n: BigInt ): BigInt = if (n == 0 ) 1 else n * factorial( n – 1 ) • Statically Typed • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  19. 19. What is Scala ? A programming language ... • Pure Object Oriented • Functionalintroduction Method Result Type Scal a def factorial( n: BigInt ): BigInt = if (n == 0 ) 1 else n * factorial( n – 1 ) • Statically Typed Parameter list Type declaration Definition • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  20. 20. What is Scala ? A programming language ... • Pure Object Oriented • Functional Scal a def factorial( n: BigInt ): BigInt = if (n == 0 ) 1 else n * factorial( n – 1 ) • Statically Typed ● (almost) everything is an expression • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  21. 21. What is Scala ? A programming language ... • Pure Object Oriented • Functional Scal a def factorial( n: BigInt ): BigInt = if (n == 0 ) 1 else n * factorial( n – 1 ) • Statically Typed ● (almost) everything is an expression BigInt 'integrates' like a Built-In type (but it's not!) ● • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  22. 22. What is Scala ? A programming language ... • Pure Object Oriented • Functional class BigInt( val bigInteger: BigInteger ) extends java.lang.Number{ override def hashCode(): Int = this.bigInteger.hashCode() • Statically Typed BigInt = def + (that: BigInt): new BigInt( this.bigInteger.add( that.bigInteger ) ) ... • } Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  23. 23. What is Scala ? A programming language ... • Pure Object Oriented Introduction of class definition • Functional val bigInteger: BigInteger ) extends java.lang.Number{ class BigInt( override def hashCode(): Int = this.bigInteger.hashCode() • Statically Typed BigInt = def + (that: BigInt): new BigInt( this.bigInteger.add( that.bigInteger ) ) ... • } Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  24. 24. What is Scala ? A programming language ... class value parameter • Pure Object Oriented (primary constructor) • Functional val bigInteger: BigInteger ) extends java.lang.Number{ class BigInt( override def hashCode(): Int = this.bigInteger.hashCode() • Statically Typed BigInt = def + (that: BigInt): new BigInt( this.bigInteger.add( that.bigInteger ) ) ... • } Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  25. 25. What is Scala ? A programming language ... • Pure Object Oriented Single Inheritance • Functional val bigInteger: BigInteger ) extends java.lang.Number{ class BigInt( override def hashCode(): Int = this.bigInteger.hashCode() • Statically Typed BigInt = def + (that: BigInt): new BigInt( this.bigInteger.add( that.bigInteger ) ) ... • } Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  26. 26. What is Scala ? A programming language ... • Pure Object Oriented Mandatory ! • Functional class BigInt( val bigInteger: BigInteger ) extends java.lang.Number{ override def hashCode(): Int = this.bigInteger.hashCode() • Statically Typed BigInt = def + (that: BigInt): new BigInt( this.bigInteger.add( that.bigInteger ) ) ... • } Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  27. 27. What is Scala ? A programming language ... • Pure Object Oriented • Functional class BigInt( val bigInteger: BigInteger ) extends java.lang.Number{ override def hashCode(): Int = this.bigInteger.hashCode() • Statically Typed BigInt = def + (that: BigInt): new BigInt( this.bigInteger.add( that.bigInteger ) ) ... • } Runs ordinary Method on the JVM Mario Gleichmann XPUG Rhein/Main
  28. 28. What is Scala ? A programming language ... • Pure Object Oriented • Functional class BigInt( val bigInteger: BigInteger ) extends java.lang.Number{ override def hashCode(): Int = this.bigInteger.hashCode() • Statically Typed BigInt = def + (that: BigInt): new BigInt( this.bigInteger.add( that.bigInteger ) ) ... • } RunsClass Instantiation on the JVM Mario Gleichmann XPUG Rhein/Main
  29. 29. What is Scala ? A programming language ... • Pure Object Oriented • Functional class BigInt( val bigInteger: BigInteger ) extends java.lang.Number{ override def hashCode(): Int = this.bigInteger.hashCode() • Statically Typed BigInt = def + (that: BigInt): new BigInt( this.bigInteger.add( that.bigInteger ) ) ... • } Runs on the JVM Self reference Mario Gleichmann XPUG Rhein/Main
  30. 30. What is Scala ? A programming language ... Example: Implement your own Type • Object Oriented ● Stack of Int values ● Immutable (Functional style) • Functional • Statically Typed • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  31. 31. What is Scala ? A programming language ... Example: Implement your own Type • Object Oriented ● Stack of Int values ● Immutable (Functional style) • Functional Empty Stack : Non Empty Stack : [] • Statically Typed element: Int [ 7 ] Rest: Stack : element: Int [ 23 ] • Runs on the JVM Rest: Stack : [] Mario Gleichmann XPUG Rhein/Main
  32. 32. What is Scala ? A programming language ... Example: Implement your own Type • Object Oriented ● Stack of Int values ● Immutable (Functional style) • Functional Empty Stack : Non Empty Stack : [] element: Int [ 10 ] Rest: Stack : • Statically Typed element: Int [ 7 ] Rest: Stack : element: Int [ 23 ] • Runs on the JVM Rest: Stack : [] Mario Gleichmann XPUG Rhein/Main
  33. 33. What is Scala ? A programming language ... Example: Implement your own Type • Object Oriented abstract class IntStack { def push(x: Int): IntStack = new NonEmptyIntStack( x, this ) • Functional def isEmpty: Boolean • Statically Typed Int def top: def pop: IntStack } • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  34. 34. What is Scala ? A programming language ... Example: Implement your own Type • Abstract class Object Oriented abstract class IntStack { def push(x: Int): IntStack = new NonEmptyIntStack( x, this ) • Functional def isEmpty: Boolean Abstract method • Statically Typed Int def top: Abstract method def pop: IntStack Abstract method } • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  35. 35. What is Scala ? A programming language ... Example: Implement your own Type • Object Oriented class EmptyIntStack extends IntStack { def isEmpty = true • Functional def top = throw new EmptyStackException • Statically Typed = throw new EmptyStackException def pop } • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  36. 36. What is Scala ? A programming language ... Example: Implement your own Type • Object Oriented Inheritance class EmptyIntStack extends IntStack { def isEmpty = true • Functional def top = throw new EmptyStackException • Statically Typed = throw new EmptyStackException def pop } Throwing a 'checked' Exception • Runs on the JVM ... but catching is optional Mario Gleichmann XPUG Rhein/Main
  37. 37. What is Scala ? A programming language ... Example: Implement your own Type • Object Oriented class EmptyIntStack extends IntStack { def isEmpty = true • Functional def top = throw new EmptyStackException • Statically Typed = throw new EmptyStackException def pop } val zeroInts = new EmptyIntStack • Runs on the JVM var noInts = new EmptyIntStack Mario Gleichmann XPUG Rhein/Main
  38. 38. What is Scala ? A programming language ... Example: Implement your own Type • Object Oriented class EmptyIntStack extends IntStack { def isEmpty = true • Functional def top = throw new EmptyStackException • Statically Typed = throw new EmptyStackException def pop } val zeroInts = new EmptyIntStack 'Immutable' value • Runs on the JVM var noInts = new EmptyIntStack 'mutable' variable Mario Gleichmann XPUG Rhein/Main
  39. 39. What is Scala ? A programming language ... Example: Implement your own Type • Object Oriented class EmptyIntStack extends IntStack { def isEmpty = true • Functional def top = throw new EmptyStackException • Statically Typed = throw new EmptyStackExceptionNo need def pop } for multiple val zeroInts = new EmptyIntStack Instances of • Runs on the JVM EmptyIntStack var noInts = new EmptyIntStack Mario Gleichmann XPUG Rhein/Main
  40. 40. What is Scala ? A programming language ... Example: Implement your own Type • Object Oriented object EmptyIntStack extends IntStack { • Functional def isEmpty = true def top = throw new EmptyStackException • Statically Typed = throw new EmptyStackException def pop } • Runs on the Singleton Object ● JVM Mario Gleichmann XPUG Rhein/Main
  41. 41. What is Scala ? A programming language ... Example: Implement your own Type • Object Oriented object EmptyIntStack extends IntStack { • Functional def isEmpty = true def top = throw new EmptyStackException • Statically Typed = throw new EmptyStackException def pop } val zeroInts = new EmptyIntStack • Runs on the JVM var noInts = new EmptyIntStack Mario Gleichmann XPUG Rhein/Main
  42. 42. What is Scala ? A programming language ... Example: Implement your own Type • Object Oriented class NonEmptyIntStack( elem: Int, rest: IntStack ) extends IntStack { • Functional def isEmpty = false def top = elem • Statically Typed def pop = rest } • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  43. 43. What is Scala ? A programming language ... Example: Implement your own Type • Object Oriented class NonEmptyIntStack( elem: Int, rest: IntStack ) extends IntStack { • Functional def isEmpty = false def top = elem • Statically Typed def pop = rest } • Runs on the JVM var s = EmptyIntStack push 23 push 7 push 10 Mario Gleichmann XPUG Rhein/Main
  44. 44. What is Scala ? A programming language ... • Pure Object Oriented • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  45. 45. What is Scala ? A programming language ... Refined Type System • Pure Object Oriented • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  46. 46. What is Scala ? A programming language ... Refined Type System • Pure Object Oriented ● Using Stack with other types than Int ● Concept of stacking elements is generic • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  47. 47. What is Scala ? A programming language ... Refined Type System • Pure Object Oriented ● Using Stack with other types than Int ● Concept of stacking elements is generic • Statically Typed => Generic Types • Functional (Type Parameterization) • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  48. 48. What is Scala ? A programming language ... Refined Type System • Pure Object Oriented abstract class Stack[A] { Type Paramter def push(x: A): Stack[A] = • Statically Typed new NonEmptyStack[A]( x, this ) def isEmpty: Boolean • Functional def top: A def pop: Stack[A] • Runs on the JVM } Mario Gleichmann XPUG Rhein/Main
  49. 49. What is Scala ? A programming language ... Refined Type System • Pure Object Oriented class NonEmptyStack[A]( elem: A, rest: Stack[A] ) extends Stack[A] { • Statically Typed def isEmpty = false • Functional def top = elem def pop = rest } • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  50. 50. What is Scala ? A programming language ... Refined Type System • Pure Object Oriented val s = new EmptyStack[Int] val t = s push 1 push 2 push 3 • Statically Typed t.pop.top => 2 • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  51. 51. What is Scala ? A programming language ... Refined Type System • Pure Object Oriented val s = new EmptyStack[Int] val t = s push 1 push 2 push 3 • Statically Typed t.pop.top => 2 Parameterized Type • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  52. 52. What is Scala ? A programming language ... Refined Type System • Pure Object Oriented val s = new EmptyStack[Int] val t = s push 1 push 2 push 3 • Statically Typed t.pop.top Mandatory => 2 ( no Raw Types ! ) • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  53. 53. What is Scala ? A programming language ... Refined Type System • Pure Object Oriented ● A Stack implementation only for numbers • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  54. 54. What is Scala ? A programming language ... Refined Type System • Pure Object Oriented ● A Stack implementation only for numbers ● Restrict the upper Type to Number • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  55. 55. What is Scala ? A programming language ... Refined Type System • Pure Object Oriented ● A Stack implementation only for numbers ● Restrict the upper Type to Number • Statically Typed ● Upper Type Bound • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  56. 56. What is Scala ? A programming language ... Refined Type System • Pure Object Oriented ● A Stack implementation only for numbers ● Restrict the upper Type to Number • Statically Typed ● Upper Type Bound • Functional abstract class Stack[A <: Number]{ ... } • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  57. 57. What is Scala ? A programming language ... Refined Type System • Pure Object Oriented ● A Stack implementation only for numbers ● Restrict the upper Type to Number • Statically Typed ● Upper Type Bound • Functional abstract class Stack[A <: Number]{ ... } Defined by the implementor, • Runs on the JVM not by the user ! Mario Gleichmann XPUG Rhein/Main
  58. 58. What is Scala ? A programming language ... Refined Type System • Pure Object OrientedIs Stack[String] a super type of Stack[Any] ? • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  59. 59. What is Scala ? A programming language ... Refined Type System • Pure Object OrientedIs Stack[String] a super type of Stack[Any] ? • Statically Typed Jav a List<String> sList = new ArrayList<String>(); List<Object> oList = sList; • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  60. 60. What is Scala ? A programming language ... Refined Type System • Pure Object OrientedIs Stack[String] a super type of Stack[Any] ? • Statically Typed Jav a List<String> sList = new ArrayList<String>(); List<Object> oList = sList; • Functional Compile Error: ''Type mismatch: cannot convert • Runs on the JVM List<String> to List<Object>'' Mario Gleichmann XPUG Rhein/Main
  61. 61. What is Scala ? A programming language ... Refined Type System • Pure Object OrientedIs Stack[String] a super type of Stack[Any] ? • Statically Typed Jav a List<String> sList = new ArrayList<String>(); List<Object> oList = sList; • Functional Generic Types are • Runs on the JVM INVARIANT Mario Gleichmann XPUG Rhein/Main
  62. 62. What is Scala ? A programming language ... Refined Type System • Pure Object Orientedbut ... • Statically Typed Jav a String[] sArray = new String[]{}; Object[] oArray = sArray; • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  63. 63. What is Scala ? A programming language ... Refined Type System • Pure Object Orientedbut ... • Statically Typed Jav a String[] sArray = new String[]{}; Object[] oArray = sArray; • Functional Arrays are • Runs on the JVM COVARIANT Mario Gleichmann XPUG Rhein/Main
  64. 64. What is Scala ? A programming language ... Refined Type System • Pure Object Orientedbut ... • Statically Typed Jav a String[] sArray = new String[]{}; Object[] oArray = sArray; • Functional oArray[0] = BigDecimal.ONE; • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  65. 65. What is Scala ? A programming language ... Refined Type System • Pure Object Orientedbut ... • Statically Typed Jav a String[] sArray = new String[]{}; Object[] oArray = sArray; • Functional oArray[0] = BigDecimal.ONE; ArrayStoreException • Runs on the JVM at Runtime ! Mario Gleichmann XPUG Rhein/Main
  66. 66. What is Scala ? A programming language ... Refined Type System • Pure Object Oriented Scal a abstract class Stack[+A]{ • Statically Typed def push(x: A): Stack[A] = new NonEmptyStack[A]( x, this ) ... • Functional } • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  67. 67. What is Scala ? A programming language ... Refined Type System • Pure Object Oriented Covariant subtyping Scal a abstract class Stack[+A]{ • Statically Typed def push(x: A): Stack[A] = new NonEmptyStack[A]( x, this ) ... • Functional } • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  68. 68. What is Scala ? A programming language ... Refined Type System • Pure Object Oriented Covariant subtyping Scal a abstract class Stack[+A]{ • Statically Typed def push(x: A): Stack[A] = new NonEmptyStack[A]( x, this ) ... • Functional } Compile Error: ''covariant type A occurs in • Runs on the JVM contravariant position in type A of value x'' Mario Gleichmann XPUG Rhein/Main
  69. 69. What is Scala ? A programming language ... Refined Type System • Pure Object Oriented Scal a abstract class Stack[+A]{ • Statically Typed def push[B >: A](x: B): Stack[B] = new NonEmptyStack[B](x, this) ... • Functional } • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  70. 70. What is Scala ? A programming language ... Refined Type System • Pure Object Oriented Scal a abstract class Stack[+A]{ • Statically Typed def push[B >: A](x: B): Stack[B] = new NonEmptyStack[B](x, this) ... • Functional } Lower Type Bound ''Parameter B is restricted to range • Runs on the JVM only over supertypes of type A'' Mario Gleichmann XPUG Rhein/Main
  71. 71. What is Scala ? A programming language ... Refined Type System • Pure Object Oriented Scal a abstract class Stack[+A]{ • Statically Typed def push[B >: A](x: B): Stack[B] = new NonEmptyStack[B](x, this) ... • Functional } val s1 = new EmptyStack[Int] push 1 push 2 • Runs on the JVM val s2 = s1 push "x" Mario Gleichmann XPUG Rhein/Main
  72. 72. What is Scala ? A programming language ... Refined Type System • Pure Object Oriented Scal a abstract class Stack[+A]{ • Statically Typed def push[B >: A](x: B): Stack[B] = new NonEmptyStack[B](x, this) ... • Functional } Stack[Int] val s1 = new EmptyStack[Int] push 1 push 2 • Runs on the JVM val s2 = s1 push "x" Stack[Any] Mario Gleichmann XPUG Rhein/Main
  73. 73. What is Scala ? A programming language ... Type inference • Pure Object Oriented val creator: String = “Odersky“ • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  74. 74. What is Scala ? A programming language ... Type inference • Pure Object Oriented val creator: String = “Odersky“ val creator = “Odersky“ • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  75. 75. What is Scala ? A programming language ... Type inference • Pure Object Oriented val creator: String = “Odersky“ val creator = “Odersky“ • Statically Typed • Functional def add( a: Int, b: Int ): Int = a + b • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  76. 76. What is Scala ? A programming language ... Type inference • Pure Object Oriented val creator: String = “Odersky“ val creator = “Odersky“ • Statically Typed • Functional def add( a: Int, b: Int ): Int = a + b def add( a: Int, b: Int ) = a + b • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  77. 77. What is Scala ? A programming language ... Implicit Type conversion • Pure Object Oriented “Scalable Language“ - 'a' => Sc l ble L ngu ge • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  78. 78. What is Scala ? A programming language ... Implicit Type conversion • Pure Object Oriented “Scalable Language“ - 'a' • Statically Typed class StringExtension( s: String ){ def -( sub: Char ) = s.replace( sub, ' ' ) } • Functional new StringExtension( “Scala“ ).-( 'a' ) • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  79. 79. What is Scala ? A programming language ... Implicit Type conversion • Pure Object Oriented “Scalable Language“ - 'a' • Statically Typed class StringExtension( s: String ){ def -( sub: Char ) = s.replace( sub, ' ' ) } • Functional implicit def toStringExtension( s: String ) = new StringExtension( s ) • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  80. 80. What is Scala ? A programming language ... Implicit Type conversion • Pure Object Oriented “Scalable Language“ - 'a' Implicit conversion to StringExtension • Statically Typed class StringExtension( s: String ){ def -( sub: Char ) = s.replace( sub, ' ' ) } • Functional implicit def toStringExtension( s: String ) = new StringExtension( s ) • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  81. 81. What is Scala ? A programming language ... • Pure Object Oriented • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  82. 82. What is Scala ? A programming language ... ● Lambda Calculus (A. Church) • Pure Object Oriented ● Functions are first class values • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  83. 83. What is Scala ? A programming language ... ● Lambda Calculus (A. Church) • Pure Object Oriented ● Functions are first class values • Statically Typed Function Literals ( x: Int ) => x + 1 • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  84. 84. What is Scala ? A programming language ... ● Lambda Calculus (A. Church) • Pure Object Oriented ● Functions are first class values • Statically Typed Function Literals ( x: Int ) => x + 1 => λ x . x + 1 • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  85. 85. What is Scala ? A programming language ... ● Lambda Calculus (A. Church) • Pure Object Oriented ● Functions are first class values • Statically Typed Function Literals ( x: Int ) => x + 1 => λ x . x + 1 • Functional Argument list Definition • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  86. 86. What is Scala ? A programming language ... ● Lambda Calculus (A. Church) • Pure Object Oriented ● Functions are first class values • Statically Typed Function Literals ( x: Int ) => x + 1 => λ x . x + 1 • Functional val succ = ( x: Int ) => x + 1 • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  87. 87. What is Scala ? A programming language ... ● Lambda Calculus (A. Church) • Pure Object Oriented ● Functions are first class values • Statically Typed Function Literals ( x: Int ) => x + 1 => λ x . x + 1 • Functional val succ = ( x: Int ) => x + 1 succ( 7 ) => 8 • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  88. 88. What is Scala ? A programming language ... ● Lambda Calculus (A. Church) • Pure Object Oriented ● Functions are first class values • Statically Typed Function Literals ( x: Int ) => x + 1 => λ x . x + 1 • Functional val succ = ( x: Int ) => x + 1 succ( 7 ) => 8 • Runs on the JVM type of succ: ( Int ) => Int Mario Gleichmann XPUG Rhein/Main
  89. 89. What is Scala ? A programming language ... Closures • Pure Object Oriented val ages = List( 2, 20, 14, 19, 49, 11, 62 ) var barrier = 18 • Statically Typed val minors = { ( x :Int ) => x < barrier } val germanMinors = ages.filter( minors ) • Functional => List( 2, 14, 11 ) • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  90. 90. What is Scala ? A programming language ... Closures • Pure Object Oriented val ages = List( 2, 20, 14, 19, 49, 11, 62 ) var barrier = 18 List[Int] (Type inference) • Statically Typed val minors = { ( x :Int ) => x < barrier } val germanMinors = ages.filter( minors ) • Functional ... accepting a function which => List( 2, 14, 11 )and results to boolean accepts an Int • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  91. 91. What is Scala ? A programming language ... Closures • Pure Object Oriented val ages = List( 2, 20, 14, 19, 49, 11, 62 ) var barrier = 18 free variable • Statically Typed val minors = { ( x :Int ) => x < barrier } val germanMinors = ages.filter( minors ) • Functional bound variable 'open term' • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  92. 92. What is Scala ? A programming language ... Closures • Pure Object Oriented val ages = List( 2, 20, 14, 19, 49, 11, 62 ) var barrier = 18 capturing • Statically Typed val minors = { ( x :Int ) => x < barrier } val germanMinors = ages.filter( minors ) • Functional ● bound within lexical scope of function • Runs on the JVM => open term is closed Mario Gleichmann XPUG Rhein/Main
  93. 93. What is Scala ? A programming language ... Closures • Pure Object Oriented val ages = List( 2, 20, 14, 19, 49, 11, 62 ) var barrier = 18 • Statically Typed val minors = { ( x :Int ) => x < barrier } val germanMinors = ages.filter( minors ) • Functional barrier = 21 val usMinors = ages.filter( minors ) • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  94. 94. What is Scala ? A programming language ... Closures • Pure Object Oriented val ages = List( 2, 20, 14, 19, 49, 11, 62 ) var barrier = 18 • Statically Typed val minors = { ( x :Int ) => x < barrier } val germanMinors = ages.filter( minors ) 'dynamic' bound • Functional barrier = 21 val usMinors = ages.filter( minors ) • Runs on the JVM => 2, 20, 14, 19, 11 Mario Gleichmann XPUG Rhein/Main
  95. 95. What is Scala ? A programming language ... Currying • Pure Object Oriented val add = ( a: Int, b: Int ) => a + b • Statically Typed A function ... accepting two Args • Functional ... resulting in a value of type Int • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  96. 96. What is Scala ? A programming language ... Currying • Pure Object Oriented val add = ( a: Int, b: Int ) => a + b • Statically Typed type of function add: ( Int, Int ) => Int • Functional 'resulting in ...' • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  97. 97. What is Scala ? A programming language ... Currying • Pure Object Oriented val add = ( a: Int, b: Int ) => a + b • Statically Typed Quiz: ''transform into a function which is accepting • Functional only one single Argument after another'' • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  98. 98. What is Scala ? A programming language ... Currying • Pure Object Oriented val add = ( a: Int ) => ( b: Int ) => a + b • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  99. 99. What is Scala ? A programming language ... Currying • Pure Object Oriented val add = ( a: Int ) => ( b: Int ) => a + b • Statically Typed A function ... accepting one Arg • Functional ... resulting in another function • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  100. 100. What is Scala ? A programming language ... Currying • Pure Object Oriented val add = ( a: Int ) => ( b: Int ) => a + b • Statically Typed ... accepting one Arg • Functional ... resulting in a value of type Int • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  101. 101. What is Scala ? A programming language ... Currying • Pure Object Oriented val add = ( a: Int ) => ( b: Int ) => a + b • Statically Typed type of function add: (Int) => (Int) => Int • Functional 'resulting in ...' 'resulting in ...' • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  102. 102. What is Scala ? A programming language ... Currying • Pure Object Oriented val add = ( a: Int ) => ( b: Int ) => a + b • Statically Typed val succ = add( 1 ) succ( 7 ) => 8 • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  103. 103. What is Scala ? A programming language ... Currying • Pure Object Oriented val add = ( a: Int ) => ( b: Int ) => a + b A function ... val succ = add( 1 ) ... accepting one Arg • Statically Typed ... resulting in another function succ( 7 ) => 8 • Functional ... accepting one Arg ... resulting in a value of type Int • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  104. 104. What is Scala ? A programming language ... Curried Methods • Pure Object Oriented def mult( a: Int )( b: Int ) = a * b • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  105. 105. What is Scala ? A programming language ... Curried Methods • Pure Object Oriented def mult( a: Int )( b: Int ) = a * b • Statically Typed multiple parameter lists • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  106. 106. What is Scala ? A programming language ... Curried Methods • Pure Object Oriented def mult( a: Int )( b: Int ) = a * b • Statically Typed multiple parameter lists • Functional Signature: mult ( Int ) ( Int ) : Int • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  107. 107. What is Scala ? A programming language ... Curried Methods • Pure Object Oriented def mult( a: Int )( b: Int ) = a * b val double = mult( 2 ) _ • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  108. 108. What is Scala ? A programming language ... Curried Methods • Pure Object Oriented def mult( a: Int )( b: Int ) = a * b val double = mult( 2 ) _ • Statically Typed Partially applied 2nd Arg unapplied • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  109. 109. What is Scala ? A programming language ... Curried Methods • Pure Object Oriented def mult( a: Int )( b: Int ) = a * b val double = mult( 2 ) _ • Statically Typed • Functional Coercion into a function of type ( Int ) => Int • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  110. 110. What is Scala ? A programming language ... Curried Methods • Pure Object Oriented def mult( a: Int )( b: Int ) = a * b val double = mult( 2 ) _ • Statically Typed double( 6 ) => 12 • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  111. 111. What is Scala ? A programming language ... Curried Methods • Pure Object Oriented val hours = ( 0 to 23 ).toList def modulo( n: Int )( x: Int ) = ( x % n ) == 0 • Statically Typed hours.filter( modulo( 2 ) _ ) • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  112. 112. What is Scala ? A programming language ... Curried Methods • Pure Object Oriented val hours = ( 0 to 23 ).toList List[Int] def modulo( n: Int )( x: Int ) = ( x % n ) == 0 • Statically Typed hours.filter( modulo( 2 ) _ ) • Functional expects function of type ( Int ) => Boolean • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  113. 113. What is Scala ? A programming language ... Curried Methods • Pure Object Oriented val hours = ( 0 to 23 ).toList def modulo( n: Int )( x: Int ) = ( x % n ) == 0 • Statically Typed hours.filter( modulo( 2 ) _ ) • Functional curried to function of type ( Int ) => Boolean • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  114. 114. What is Scala ? A programming language ... Curried Methods • Pure Object Oriented val hours = ( 0 to 23 ).toList def modulo( n: Int )( x: Int ) = ( x % n ) == 0 • Statically Typed hours.filter( modulo( 2 ) _ ) => List( 0, 2, 4, 6, 8, 10, 12, …, 20, 22 ) • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  115. 115. What is Scala ? A programming language ... Curried Methods • Pure Object Oriented val hours = ( 0 to 23 ).toList def modulo( n: Int )( x: Int ) = ( x % n ) == 0 • Statically Typed hours.filter( modulo( 2 ) _ ) => List( 0, 2, 4, 6, 8, 10, 12, …, 20, 22 ) • Functional hours.filter( modulo( 4 ) ) • Runs on the JVM => List( 0, 2, 4, 8, 12, …, 16, 20 ) Mario Gleichmann XPUG Rhein/Main
  116. 116. What is Scala ? A programming language ... OO + FP Fusion ● • Pure Object Oriented • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  117. 117. What is Scala ? A programming language ... OO + FP Fusion ● • Pure Object Oriented • Everything is an Object • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  118. 118. What is Scala ? A programming language ... OO + FP Fusion ● • Pure Object Oriented • Everything is an Object • Functions are Objects • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  119. 119. What is Scala ? A programming language ... OO + FP Fusion ● • Pure Object Oriented • Everything is an Object • Functions are Objects • Statically Typed val succ = ( x: Int ) => x + 1 • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  120. 120. What is Scala ? A programming language ... OO + FP Fusion ● • Pure Object Oriented • Everything is an Object • Functions are Objects • Statically Typed val succ = new Function1[Int, Int]{ override def apply( x: Int ) = x + 1 • Functional } • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  121. 121. What is Scala ? A programming language ... OO + FP Fusion ● • Pure Object Oriented • Everything is an Object • Functions are Objects • Statically Typed val succ = new Function1[Int, Int]{ override def apply( x: Int ) = x + 1 • Functional } succ( 7 ) • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  122. 122. What is Scala ? A programming language ... OO + FP Fusion ● • Pure Object Oriented • Everything is an Object • Functions are Objects • Statically Typed val succ = new Function1[Int, Int]{ override def apply( x: Int ) = x + 1 • Functional } succ.apply( 7 ) • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  123. 123. What is Scala ? A programming language ... • Pure Object Oriented • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  124. 124. What is Scala ? A programming language ... ... so does Groovy, Clojure, JRuby ... • Pure Object Oriented • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  125. 125. What is Scala ? A programming language ... ... so does Groovy, Clojure, JRuby ... • Pure Object Oriented • Dynamically typed (MOP & Co) • Significant Performance Overhead ! • Statically Typed • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  126. 126. What is Scala ? A programming language ... ... so does Groovy, Clojure, JRuby ... • Pure Object Oriented • Dynamically typed (MOP & Co) • Significant Performance Overhead ! • Statically Typed • Scala is statically typed ! • Functional • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  127. 127. What is Scala ? A programming language ... ... so does Groovy, Clojure, JRuby ... • Pure Object Oriented • Dynamically typed (MOP & Co) • Significant Performance Overhead ! • Statically Typed • Scala is statically typed ! • Functional • Compiles to Bytecode • Seamless Java Interoperability • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  128. 128. What is Scala ? A programming language ... ... so does Groovy, Clojure, JRuby ... • Pure Object Oriented • Dynamically typed (MOP & Co) • Significant Performance Overhead ! • Statically Typed • Scala is statically typed ! • Functional • Compiles to Bytecode • Seamless Java Interoperability • Runs on the JVM • Performance on par with Java Mario Gleichmann XPUG Rhein/Main
  129. 129. What is Scala ? A programming language ... “I can honestly say if someone • Pure Object Oriented had shown me the Programming in Scala book ... back in 2003 • Statically Typed I'd probably have never created Groovy“ • Functional James Strachan • Runs on the JVM Mario Gleichmann XPUG Rhein/Main
  130. 130. Characteristics • Expressive • High Level • Concise • Extensible • Pragmatic Mario Gleichmann XPUG Rhein/Main
  131. 131. Characteristics • Expressive case class Person( name: String, age: Int ) • High Level var persons = List( Person( "Hans", 11 ), Person( "Hugo", 19 ), Person( "Helga", 16 ), Person( "Heinz", 38 ) ) • Concise val (adults, minors) = persons.partition( _.age > 18 ) • Extensible Can you spot the intention ? • Pragmatic Mario Gleichmann XPUG Rhein/Main
  132. 132. Characteristics • Expressive case class Person( name: String, age: Int ) • High Level var persons = List( Person( "Hans", 11 ), Person( "Hugo", 19 ), Person( "Helga", 16 ), Person( "Heinz", 38 ) ) • Concise val (adults, minors) = persons.partition( _.age > 18 ) • Extensible ''Split Persons into minors and adults by their age'' • Pragmatic Mario Gleichmann XPUG Rhein/Main
  133. 133. Characteristics • Expressive case class Person( name: String, age: Int ) • High Level var persons = List( Person( "Hans", 11 ), Person( "Hugo", 19 ), Person( "Helga", 16 ), Results into a Tuple2[List[Person],List[Person]] Person( "Heinz", 38 ) ) • Concise val (adults, minors) = persons.partition( _.age > 18 ) • Extensible ''Split Persons into minors and adults by their age'' • Pragmatic Mario Gleichmann XPUG Rhein/Main
  134. 134. Characteristics • Expressive case class Person( name: String, age: Int ) • High Level var persons = List( Person( "Hans", 11 ), Person( "Hugo", 19 ), Person( "Helga", 16 ), Results into a Tuple2[List[Person],List[Person]] Person( "Heinz", 38 ) ) • Concise val (adults, minors) = persons.partition( _.age > 18 ) • Extensible Pattern Matching: ''Split Persons into minors and adults by their age'' bound to single elements of a Tuple2 • Pragmatic Mario Gleichmann XPUG Rhein/Main
  135. 135. Characteristics • Expressive case class Person( name: String, age: Int ) • High Level var persons = List( Person( "Hans", 11 ), Person( "Hugo", 19 ), Person( "Helga", 16 ), Person( "Heinz", 38 ) ) • Concise val (adults, minors) = persons.partition( _.age > 18 ) • Extensible ''Split Persons into minors and adults by their age'' adults => List(Person(Hugo,19), Person(Heinz,38)) • Pragmatic minors => List(Person(Hans,11), Person(Helga,16)) Mario Gleichmann XPUG Rhein/Main
  136. 136. Characteristics • Expressive val bookPrices = Map( “Prag. Programmer“ -> 20 USD, • High Level “Systems Thinking“ -> 30 EUR, “Code Complete“ -> 25 USD ) • Concise bookPrices += ( “Clean Code“ -> 20 EUR ) • Extensible println( bookPrices( “Systems Thinking“ ) ) • Pragmatic for( (book, price) <- bookPrices ){ if( price in EUR ) println( book ) } Mario Gleichmann XPUG Rhein/Main
  137. 137. Characteristics Create a new Map • Expressive val bookPrices = Map( “Prag. Programmer“ -> 20 USD, • High Level “Systems Thinking“ -> 30 EUR, “Code Complete“ -> 25 USD ) • Concise bookPrices += ( “Clean Code“ -> 20 EUR ) • Extensible println( bookPrices( “Systems Thinking“ ) ) • Pragmatic for( (book, price) <- bookPrices ){ if( price in EUR ) println( book ) } Mario Gleichmann XPUG Rhein/Main
  138. 138. Characteristics Create a new Map • Expressive val bookPrices = Map( “Prag. Programmer“ -> 20 USD, • High Level “Systems Thinking“ -> 30 EUR, “Code Complete“ -> 25 USD ) Implicit Conversion into a Tuple2 • Concise bookPrices += ( “Clean Code“ -> 20 EUR ) • Extensible println( bookPrices( “Systems Thinking“ ) ) • Pragmatic for( (book, price) <- bookPrices ){ if( price in EUR ) println( book ) } Mario Gleichmann XPUG Rhein/Main
  139. 139. Characteristics Create a new Map • Expressive val bookPrices = Map( “Prag. Programmer“ -> 20 USD, • High Level “Systems Thinking“ -> 30 EUR, “Code Complete“ -> 25 USD ) Implicit Conversion into a Currency • Concise bookPrices += ( “Clean Code“ -> 20 EUR ) • Extensible println( bookPrices( “Systems Thinking“ ) ) • Pragmatic for( (book, price) <- bookPrices ){ if( price in EUR ) println( book ) } Mario Gleichmann XPUG Rhein/Main
  140. 140. Characteristics • Expressive val bookPrices = Map( “Prag. Programmer“ -> 20 USD, Compagnion Object • High Level object Map{ “Systems Thinking“ -> 30 EUR, “Code Complete“ -> 25 USD ) def apply[A, B]( elems: (A, B)* ) : Map[A, B] = ... ... • Concise } bookPrices += ( “Clean Code“ -> 20 EUR ) • Extensible println( bookPrices( “Systems Thinking“ ) ) • Pragmatic for( (book, price) <- bookPrices ){ if( price in EUR ) println( book ) } Mario Gleichmann XPUG Rhein/Main
  141. 141. Characteristics • Expressive val bookPrices = Map( “Prag. Programmer“ -> 20 USD, Map instance • High Level “Systems Thinking“ -> 30 EUR, class <xxx>Map[A, B] extends Map[A, B] { “Code Complete“ -> 25 USD ) override def apply(key: A): B = ... ... • Concise } bookPrices += ( “Clean Code“ -> 20 EUR ) • Extensible println( bookPrices( “Systems Thinking“ ) ) • Pragmatic for( (book, price) <- bookPrices ){ if( price in EUR ) println( book ) } Mario Gleichmann XPUG Rhein/Main
  142. 142. Characteristics • Expressive • High Level • Concise • Extensible • Pragmatic Mario Gleichmann XPUG Rhein/Main
  143. 143. Characteristics Upper Case in given name ? Jav • Expressive a boolean hasUpperCase = false; • High Level for( int i=0; i < name.length; i++ ){ if( Character.isUpperCase( name.charAt( i ) { • Concise hasUpperCase = true; break; } • Extensible } • Pragmatic Mario Gleichmann XPUG Rhein/Main
  144. 144. Characteristics Upper Case in given name ? Scal • Expressive a val hasUpperCase = name.exists( c: Char => c.isUpperCase ) • High Level • Concise • Extensible • Pragmatic Mario Gleichmann XPUG Rhein/Main
  145. 145. Characteristics Upper Case in given name ? Scal • Expressive a val hasUpperCase = name.exists( c: Char => c.isUpperCase ) • High Level • Concise 'Higher Order Method' Function • Extensible • Pragmatic Mario Gleichmann XPUG Rhein/Main
  146. 146. Characteristics Upper Case in given name ? Scal • Expressive a val hasUpperCase = name.exists( c: Char => c.isUpperCase ) • High Level • Concise 'Higher Order Method' Function val hasUpperCase = • Extensible name.exists( c => c isUpperCase ) • Pragmatic Type inference Mario Gleichmann XPUG Rhein/Main
  147. 147. Characteristics Upper Case in given name ? Scal • Expressive a val hasUpperCase = name.exists( c: Char => c.isUpperCase ) • High Level • Concise 'Higher Order Method' Function val hasUpperCase = • Extensible name.exists( _ isUpperCase ) • Pragmatic parameter shortcut Mario Gleichmann XPUG Rhein/Main
  148. 148. Characteristics Find maximal Distance Jav • Expressive a List<Integer> distances = new ArrayList<Integer>(); • High Level distances.add( 12 ); distances.add( 17 ); ... • Concise Integer maxDistance = 0; • Extensible for( Integer distance : distances ){ if( distance > maxDistance ){ • Pragmatic } maxDistance = distance } Mario Gleichmann XPUG Rhein/Main
  149. 149. Characteristics Find maximal Distance Scal • Expressive a val distances = List( 12, 17, 14, 21, ...) • High Level val maxDistance = distances.foldLeft( 0 ){ Math.max } • Concise • Extensible • Pragmatic Mario Gleichmann XPUG Rhein/Main
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×