The place for sharing ideas and knowledge
Yes Scala Can!

  Amir Moulavi                     Anders Eriksson
       @mamirm                       @anders_eriksson
amir.moulavi@jayway.com           anders.eriksson@jayway.com



                     Jayway Stockholm
                          2012-05-30
Why

● We've found Scala and like it!

● We would like to broaden the interest
  amongst Java developers.

● Hopefully this presentation will inspire you to
  look more into Scala!
Agenda

● History and Background

● Scala vs. Java: Similarities and Differences

● Quick Journey to Scala

● Book Raffle!
It has been a revolution!
Little history and background
● 2001:
  ○ Design of Scala started
    ■ Goals: merge functional + object oriented
      paradigms

● 2004:
  ○ First public release on JVM
Little history and background
● 2006:
  ○ major rewrite (v 2.0)



● 2011:
  ○ Company Typesafe is funded for professional
    support
Java vs. Scala (similarities)
● Runs on JVM

● Statically typed

● Object oriented

● Garbage collected
Java vs. Scala (differences)
● Pure object orientation

● Functional paradigm

● Pattern matching

● Mixin composition
Java vs. Scala (differences)
● No checked exception

● Implicit conversions

● Flexible imports and access modifier

● Read Evaluate Print Loop (REPL) console
Scala is a unifier
Scala is Object-oriented
Scala is Functional
Scala is statically typed
Scala is concise
public class Person {
    private final String name;
    private final int age;
    public Person(String name, int age) {
         this.name = name;
         this.int = int;
    }
    public String getName() {
         return name;                       Java
    }
    public int getAge() {
         return age;
    }
}
Scala is concise
class Person(val name:String, val age:Int)




                                         Sc
                                            ala
Scala is expressive

(1 to 10) filter (_ > 7)
REPL
The command line for Scala
Tuples
(   ,   )
(   ,   ,   )
(   ,   ,   ,   )
(   ,   ,   ,    ,   )
(   ,   ,   ,    ,   ,   )
Tuples
● Saves you the tedium of defining simplistic
  data-heavy classes

● Immutable

● Each value different type

● Can contain up to 22 values

● Indexed from 1
REPL and Tuples
Case classes
Case classes
● Easier instantiation

● Immutability in constructor parameters by
  default

● You get these methods for free
   ○   toString()
   ○   equals()
   ○   hashCode()
   ○   apply()
   ○   copy()
Case classes
Pattern Matching
Pattern Matching
● Similar to Java's switch statement

● Let you match any sort of data

● First-match policy
Pattern Matching
Higher Order Functions
Higher Order Functions
● Functions are first class citizens in Scala

                  F:A        B

● Higher Order Functions are functions that
  take functions as input parameters or return
  them as results

         F : (A     B)       (R      S)
Higher Order Functions
map(f)




filter(f)
Powerful Collections
Powerful Collections

● Easy to use
  ○ A small vocabulary of 20-50 methods to solve most
      collection problems


● Concise
  ○ A single word instead of several loops!
Powerful Collections
● Safe
  ○ Functional nature + Statically typed!


● Fast
  ○ Tuned and optimized collections' operations


● Universal
  ○ The same operation on any type
Powerful Collections


                    Scala
                  Collections




      Immutable                 Mutable
Powerful Collections
● List
   ○ Immutable and ListBuffer is mutable
● Array
   ○ Mutable
● Set
   ○ Immutable and mutable
● Map
   ○ Immutable and mutable
● ... and String!
   ○
Powerful Collections with Higher
Order Functions
Traits
Traits
● Definition:

   ○ Provides a set of methods that implement
     behaviour

   ○ Requires a set of methods that parameterized the
     provided behaviour

   ○ Does not specify any state variables
Traits
● Interfaces with behaviors

● Fundamental unit of code re-use

● Extremely powerful tool in OO design
Rich interfaces



Traits



         Stackable modifications
Traits
Parallel Collections
Parallel Collections
● Supports the same operations as sequential
  ones

● Easily convert sequential to parallel
  collection
Parallel Collections
Scala and Java Integration
● Seamless integration

● Make full use of existing libraries and code



        Scala                 Java
Scala and Java Integration
● Familiar development environments
  ○ Eclipse, IntelliJ, Netbeans, ...


● Familiar building tools like Maven
  ○ SBT

● If you are a Java developer, you're halfway
  there!
What's Scala advantage over Java
     as a language choice?
●   implicit parameters
●   pattern matching
●   case classes
●   type inferencing
●   higher-kinded types
●   monadic for comprehension
●   variance annotation
●   traits
●   default and named argument
●   abstract values
●   by-name terms
●   type aliasing
●   self types
●   path-dependent types
●   structural types
●   ...
Conclusion
● Scala is not complex

● Express your ideas in a concise way

● Better code readability

● Scala = Functional + Object oriented
  paradigms.

● Scala scales with programmers
Where to go from here?
Programming in Scala, 2nd edition
by Martin Odersky, Lex Spoon and
Bill Venners


Scala for the Impatient by
Cay Hortsmann
Where to go from here?



Scala Stockholm User Group http://www.
meetup.com/Scala-Stockholm/
Where to go from here?



Scala course "Fast-Track to Scala" from
Typesafe

18-19 September, 2012
Where to go from here?
Questions?
Thank you!
The place for sharing ideas and knowledge

Yes scala can!

  • 1.
    The place forsharing ideas and knowledge
  • 2.
    Yes Scala Can! Amir Moulavi Anders Eriksson @mamirm @anders_eriksson amir.moulavi@jayway.com anders.eriksson@jayway.com Jayway Stockholm 2012-05-30
  • 3.
    Why ● We've foundScala and like it! ● We would like to broaden the interest amongst Java developers. ● Hopefully this presentation will inspire you to look more into Scala!
  • 4.
    Agenda ● History andBackground ● Scala vs. Java: Similarities and Differences ● Quick Journey to Scala ● Book Raffle!
  • 5.
    It has beena revolution!
  • 6.
    Little history andbackground ● 2001: ○ Design of Scala started ■ Goals: merge functional + object oriented paradigms ● 2004: ○ First public release on JVM
  • 7.
    Little history andbackground ● 2006: ○ major rewrite (v 2.0) ● 2011: ○ Company Typesafe is funded for professional support
  • 8.
    Java vs. Scala(similarities) ● Runs on JVM ● Statically typed ● Object oriented ● Garbage collected
  • 9.
    Java vs. Scala(differences) ● Pure object orientation ● Functional paradigm ● Pattern matching ● Mixin composition
  • 10.
    Java vs. Scala(differences) ● No checked exception ● Implicit conversions ● Flexible imports and access modifier ● Read Evaluate Print Loop (REPL) console
  • 11.
    Scala is aunifier
  • 12.
  • 13.
  • 14.
  • 15.
    Scala is concise publicclass Person { private final String name; private final int age; public Person(String name, int age) { this.name = name; this.int = int; } public String getName() { return name; Java } public int getAge() { return age; } }
  • 16.
    Scala is concise classPerson(val name:String, val age:Int) Sc ala
  • 17.
    Scala is expressive (1to 10) filter (_ > 7)
  • 19.
  • 20.
    Tuples ( , ) ( , , ) ( , , , ) ( , , , , ) ( , , , , , )
  • 21.
    Tuples ● Saves youthe tedium of defining simplistic data-heavy classes ● Immutable ● Each value different type ● Can contain up to 22 values ● Indexed from 1
  • 22.
  • 23.
  • 24.
    Case classes ● Easierinstantiation ● Immutability in constructor parameters by default ● You get these methods for free ○ toString() ○ equals() ○ hashCode() ○ apply() ○ copy()
  • 25.
  • 26.
  • 27.
    Pattern Matching ● Similarto Java's switch statement ● Let you match any sort of data ● First-match policy
  • 28.
  • 29.
  • 30.
    Higher Order Functions ●Functions are first class citizens in Scala F:A B ● Higher Order Functions are functions that take functions as input parameters or return them as results F : (A B) (R S)
  • 31.
  • 32.
  • 33.
    Powerful Collections ● Easyto use ○ A small vocabulary of 20-50 methods to solve most collection problems ● Concise ○ A single word instead of several loops!
  • 34.
    Powerful Collections ● Safe ○ Functional nature + Statically typed! ● Fast ○ Tuned and optimized collections' operations ● Universal ○ The same operation on any type
  • 35.
    Powerful Collections Scala Collections Immutable Mutable
  • 36.
    Powerful Collections ● List ○ Immutable and ListBuffer is mutable ● Array ○ Mutable ● Set ○ Immutable and mutable ● Map ○ Immutable and mutable ● ... and String! ○
  • 37.
    Powerful Collections withHigher Order Functions
  • 38.
  • 39.
    Traits ● Definition: ○ Provides a set of methods that implement behaviour ○ Requires a set of methods that parameterized the provided behaviour ○ Does not specify any state variables
  • 40.
    Traits ● Interfaces withbehaviors ● Fundamental unit of code re-use ● Extremely powerful tool in OO design
  • 41.
    Rich interfaces Traits Stackable modifications
  • 42.
  • 43.
  • 44.
    Parallel Collections ● Supportsthe same operations as sequential ones ● Easily convert sequential to parallel collection
  • 45.
  • 46.
    Scala and JavaIntegration ● Seamless integration ● Make full use of existing libraries and code Scala Java
  • 47.
    Scala and JavaIntegration ● Familiar development environments ○ Eclipse, IntelliJ, Netbeans, ... ● Familiar building tools like Maven ○ SBT ● If you are a Java developer, you're halfway there!
  • 48.
    What's Scala advantageover Java as a language choice?
  • 49.
    implicit parameters ● pattern matching ● case classes ● type inferencing ● higher-kinded types ● monadic for comprehension ● variance annotation ● traits ● default and named argument ● abstract values ● by-name terms ● type aliasing ● self types ● path-dependent types ● structural types ● ...
  • 50.
    Conclusion ● Scala isnot complex ● Express your ideas in a concise way ● Better code readability ● Scala = Functional + Object oriented paradigms. ● Scala scales with programmers
  • 51.
    Where to gofrom here? Programming in Scala, 2nd edition by Martin Odersky, Lex Spoon and Bill Venners Scala for the Impatient by Cay Hortsmann
  • 52.
    Where to gofrom here? Scala Stockholm User Group http://www. meetup.com/Scala-Stockholm/
  • 53.
    Where to gofrom here? Scala course "Fast-Track to Scala" from Typesafe 18-19 September, 2012
  • 54.
    Where to gofrom here?
  • 55.
  • 56.
  • 57.
    The place forsharing ideas and knowledge