SlideShare a Scribd company logo
Intro to
A Next-Gen Architecture "Manifesto"
• There should be a minimal amount of boilerplate
• The most common programming tasks should be
  simple
• It should take advantage of permanent hardware
  trends:
  – Multi-core CPUs – Vertical scalability
  – Cloud-computing – Horizontal scalability
• Preference for JVM and statically typed languages
• It should be enjoyable to use
Introducing Scala
• Functional
   –   Functions are first class entities
   –   Immutability is the default
   –   Deterministic
   –   Lends itself to concurrent programming
• But also, object oriented
   – Can code like Java if you want, but you’re not supposed
     to!!!!
• Very concise – free of much of the boilerplate code
• Type safe – VERY type safe
• Runs on the JVM – Fully interoperable with Java
Intro to scala
Popularity
• 12th most popular language in the world and
  rising quickly
   – Java holding at #2, but slipping on the web
   – Python = 4, Groovy = 20, Clojure = 21
• Becoming popular at large companies
   – Twitter, Amazon, IBM, LinkedIn, LivingSocial, tomtom
• A lot of the same cast of characters
   – James Gosling, Rod Johnson et al. are on the board
POJOs
      Java                                  Scala
public class User {                         case class User(val userName: String,
 private final String userName;                       val password: String)
 private final String password;

    public User(final String userName,
           final String password) {
         this.userName = userName;
         this.password = password;
    }

    public String getUserName() {
         return this.userName;
    }

    public String getPassword() {
                return password;
    }

    // operators
}
Overloading and Polymorphism
   Java                                    Scala
void doSomething(int x, int y, int z)…     def doSomething(x=true, y=true, z=true) = …

void doSomething(int x, int y){            // USAGE
      this.doSomething(x, y, true);        doSomething(y = false)
}

void doSomething(int x){
      this.doSomething(x, true, true)
}

void doSomething(){
      this.doSomething(true, true, true)
}
Traits
trait Ordered[A] {

    /** Result of comparing this with operand that.
     * returns x where
     * x < 0 iff this < that
     * x == 0 iff this == that
     * x < 0 iff this > that
     */
    def compare(that: A): Int

    def < (that: A): Boolean = (this compare that) < 0
    def > (that: A): Boolean = (this compare that) > 0
    def <= (that: A): Boolean = (this compare that) <= 0
    def >= (that: A): Boolean = (this compare that) >= 0
    def compareTo(that: A): Int = compare(that)
}
Type Safety
    Java                                        Scala
// Query for active users whose name            // Query for active users whose name
// is "Joe"                                     // is "Joe"
final String nameParam = "Joe"                  val nameParam = "Joe"
final String hql = "from " +                    userTable.where(user =>
       User.class.getName() +                          user.name === nameParam and
       " where name = :name AND" +                     user.status === Status.Active))
                " and status = :status"

final Query query = createQuery(hql);
query.setParameter("name", "joe");
query.setParameter("status",
            User.Status.ACTIVE.name());

return query.list();
Pattern Matching
class Animal(name: String, age: Int)
case class Cat(name: String, age: Int) extends Animal(name, age)
case class Dog(name: String, age: Int, val breed: String)
      extends Animal(name, age)

val animal: Animal = …

animal match {
 case x: Dog =>
     println("You have a dog of type " + x.breed) // THIS COMPILES

    case Cat(name, age) =>
     println("You have a cat named " + name)
     println(s"He is $age years old") // Fancypants string interpolation

    case _ =>
     println("You have some kind of unknown animal")
}
Higher Order Functions
   Java                                 Scala
// return all photos > 10k              // return all photos > 10k
List<Photo> input=Arrays.asList(...);   val photos = List(...)
List<Photo> output = new ArrayList();   val output =
for (Photo c : input){                       photos.filter(p => p.sizeKb > 10)
     if(c.getSizeInKb() > 10) {
          output.add(c);
     }                                  // OR
}                                       val output=photos.filter(_.sizeKb > 10)
Concurrency
Java               Scala
                   // divide the list into two sub-lists
                   // as determined by some function
                   val myFunc = …
                   val (l1, l2) = photos.partition(myFunc)

                   // but what if myFunc does a big,
                   // expensive operation?
       ???         val (l1, l2) =
                           photos.par.partition(myFunc)
That all sounds great, BUT…….
It's hard to read
*Especially in the beginning


def + + [B>: A, That] (that: TraversableOnce [B])
                    (implicit bf: CanBuildFrom [List [A], B, That]): That




• Functional and imperative programming support =
  2 languages in 1
     • Learning Scala is like learning 2 languages
Power comes at the cost of ease of use


   "Computer programing for everybody"
                -- Guido van Rossum



  "My goal is for Scala to be the language of
         choice for the smart kids"
                 -- Martin Odersky
Other downsides
• Tool support is poor
  – Have to use IntelliJ, Eclipse is unusable
  – SBT leaves a lot to be desired
• A lot of advanced features get abused
  – Largely caused by an immature community
• Functionality intended to provide backwards
  compatibility with Java and/or to ease the
  transition bloats the language
Conclusion
• Simple > Easy
• Scala is an extremely promising language that
  combines the best features of Java with the
  best features of functional languages
• It trades easy for simple. This is good!
• It's becoming very popular
• It’s not perfect

More Related Content

PDF
Starting with Scala : Frontier Developer's Meetup December 2010
PDF
Scala coated JVM
PDF
scala
PDF
Workshop Scala
PDF
Stepping Up : A Brief Intro to Scala
PPT
Scala - brief intro
PPT
Scala introduction
PPTX
All about scala
Starting with Scala : Frontier Developer's Meetup December 2010
Scala coated JVM
scala
Workshop Scala
Stepping Up : A Brief Intro to Scala
Scala - brief intro
Scala introduction
All about scala

What's hot (20)

PDF
Scala at HUJI PL Seminar 2008
PDF
Scala @ TechMeetup Edinburgh
PPTX
Scala fundamentals
PDF
Scala in Practice
PDF
Java7 New Features and Code Examples
KEY
Static or Dynamic Typing? Why not both?
PDF
Java 7 New Features
ODP
A Tour Of Scala
PPT
Scala uma poderosa linguagem para a jvm
PPTX
Intro to Functional Programming in Scala
PPTX
PPTX
Scale up your thinking
PPTX
A Brief Intro to Scala
PDF
Solid and Sustainable Development in Scala
ODP
PDF
Programming in Scala: Notes
PPTX
Lambda functions in java 8
PDF
Alternatives of JPA/Hibernate
PDF
An Introduction to Scala for Java Developers
PDF
Clojure, Plain and Simple
Scala at HUJI PL Seminar 2008
Scala @ TechMeetup Edinburgh
Scala fundamentals
Scala in Practice
Java7 New Features and Code Examples
Static or Dynamic Typing? Why not both?
Java 7 New Features
A Tour Of Scala
Scala uma poderosa linguagem para a jvm
Intro to Functional Programming in Scala
Scale up your thinking
A Brief Intro to Scala
Solid and Sustainable Development in Scala
Programming in Scala: Notes
Lambda functions in java 8
Alternatives of JPA/Hibernate
An Introduction to Scala for Java Developers
Clojure, Plain and Simple
Ad

Viewers also liked (7)

DOCX
Taller tablets
PDF
firma electrónica
PDF
Smh3023 past year
PPTX
Instalación del Portón
PDF
Zurgiin file
PDF
Piia Pekola, Ismo Linnosmaa, Hennamari Mikkola: Hintasääntely kuntoutuspalvel...
PPT
Law Enforcement Administration II
Taller tablets
firma electrónica
Smh3023 past year
Instalación del Portón
Zurgiin file
Piia Pekola, Ismo Linnosmaa, Hennamari Mikkola: Hintasääntely kuntoutuspalvel...
Law Enforcement Administration II
Ad

Similar to Intro to scala (20)

PPTX
Scala for curious
PPT
Scala in a nutshell by venkat
PDF
BCS SPA 2010 - An Introduction to Scala for Java Developers
PDF
A Sceptical Guide to Functional Programming
PPT
PDF
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
PDF
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
PDF
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
PDF
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
PDF
Scala in Places API
PDF
Scala - just good for Java shops?
PPTX
Beyond java8
PDF
Introduction to Scala
PDF
Develop your next app with kotlin @ AndroidMakersFr 2017
PDF
What can be done with Java, but should better be done with Erlang (@pavlobaron)
PDF
Scala - en bedre og mere effektiv Java?
PDF
Scala - core features
PPTX
Core-Java-by-Mahika-Tutor.9459891.powerpoint.pptx
PDF
A Brief Introduction to Scala for Java Developers
Scala for curious
Scala in a nutshell by venkat
BCS SPA 2010 - An Introduction to Scala for Java Developers
A Sceptical Guide to Functional Programming
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Scala in Places API
Scala - just good for Java shops?
Beyond java8
Introduction to Scala
Develop your next app with kotlin @ AndroidMakersFr 2017
What can be done with Java, but should better be done with Erlang (@pavlobaron)
Scala - en bedre og mere effektiv Java?
Scala - core features
Core-Java-by-Mahika-Tutor.9459891.powerpoint.pptx
A Brief Introduction to Scala for Java Developers

Intro to scala

  • 2. A Next-Gen Architecture "Manifesto" • There should be a minimal amount of boilerplate • The most common programming tasks should be simple • It should take advantage of permanent hardware trends: – Multi-core CPUs – Vertical scalability – Cloud-computing – Horizontal scalability • Preference for JVM and statically typed languages • It should be enjoyable to use
  • 3. Introducing Scala • Functional – Functions are first class entities – Immutability is the default – Deterministic – Lends itself to concurrent programming • But also, object oriented – Can code like Java if you want, but you’re not supposed to!!!! • Very concise – free of much of the boilerplate code • Type safe – VERY type safe • Runs on the JVM – Fully interoperable with Java
  • 5. Popularity • 12th most popular language in the world and rising quickly – Java holding at #2, but slipping on the web – Python = 4, Groovy = 20, Clojure = 21 • Becoming popular at large companies – Twitter, Amazon, IBM, LinkedIn, LivingSocial, tomtom • A lot of the same cast of characters – James Gosling, Rod Johnson et al. are on the board
  • 6. POJOs Java Scala public class User { case class User(val userName: String, private final String userName; val password: String) private final String password; public User(final String userName, final String password) { this.userName = userName; this.password = password; } public String getUserName() { return this.userName; } public String getPassword() { return password; } // operators }
  • 7. Overloading and Polymorphism Java Scala void doSomething(int x, int y, int z)… def doSomething(x=true, y=true, z=true) = … void doSomething(int x, int y){ // USAGE this.doSomething(x, y, true); doSomething(y = false) } void doSomething(int x){ this.doSomething(x, true, true) } void doSomething(){ this.doSomething(true, true, true) }
  • 8. Traits trait Ordered[A] { /** Result of comparing this with operand that. * returns x where * x < 0 iff this < that * x == 0 iff this == that * x < 0 iff this > that */ def compare(that: A): Int def < (that: A): Boolean = (this compare that) < 0 def > (that: A): Boolean = (this compare that) > 0 def <= (that: A): Boolean = (this compare that) <= 0 def >= (that: A): Boolean = (this compare that) >= 0 def compareTo(that: A): Int = compare(that) }
  • 9. Type Safety Java Scala // Query for active users whose name // Query for active users whose name // is "Joe" // is "Joe" final String nameParam = "Joe" val nameParam = "Joe" final String hql = "from " + userTable.where(user => User.class.getName() + user.name === nameParam and " where name = :name AND" + user.status === Status.Active)) " and status = :status" final Query query = createQuery(hql); query.setParameter("name", "joe"); query.setParameter("status", User.Status.ACTIVE.name()); return query.list();
  • 10. Pattern Matching class Animal(name: String, age: Int) case class Cat(name: String, age: Int) extends Animal(name, age) case class Dog(name: String, age: Int, val breed: String) extends Animal(name, age) val animal: Animal = … animal match { case x: Dog => println("You have a dog of type " + x.breed) // THIS COMPILES case Cat(name, age) => println("You have a cat named " + name) println(s"He is $age years old") // Fancypants string interpolation case _ => println("You have some kind of unknown animal") }
  • 11. Higher Order Functions Java Scala // return all photos > 10k // return all photos > 10k List<Photo> input=Arrays.asList(...); val photos = List(...) List<Photo> output = new ArrayList(); val output = for (Photo c : input){ photos.filter(p => p.sizeKb > 10) if(c.getSizeInKb() > 10) { output.add(c); } // OR } val output=photos.filter(_.sizeKb > 10)
  • 12. Concurrency Java Scala // divide the list into two sub-lists // as determined by some function val myFunc = … val (l1, l2) = photos.partition(myFunc) // but what if myFunc does a big, // expensive operation? ??? val (l1, l2) = photos.par.partition(myFunc)
  • 13. That all sounds great, BUT…….
  • 14. It's hard to read *Especially in the beginning def + + [B>: A, That] (that: TraversableOnce [B]) (implicit bf: CanBuildFrom [List [A], B, That]): That • Functional and imperative programming support = 2 languages in 1 • Learning Scala is like learning 2 languages
  • 15. Power comes at the cost of ease of use "Computer programing for everybody" -- Guido van Rossum "My goal is for Scala to be the language of choice for the smart kids" -- Martin Odersky
  • 16. Other downsides • Tool support is poor – Have to use IntelliJ, Eclipse is unusable – SBT leaves a lot to be desired • A lot of advanced features get abused – Largely caused by an immature community • Functionality intended to provide backwards compatibility with Java and/or to ease the transition bloats the language
  • 17. Conclusion • Simple > Easy • Scala is an extremely promising language that combines the best features of Java with the best features of functional languages • It trades easy for simple. This is good! • It's becoming very popular • It’s not perfect

Editor's Notes

  • #4: Deterministic means free of side effectsTwo languages in one means a lot to learn
  • #7: 80% of all .equals methods are implemented incorrectly
  • #8: Mention type inference also
  • #10: What happens if you change name to userName
  • #12: Mention overloading also
  • #13: Mention overloading also