SlideShare a Scribd company logo
1 of 17
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
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

What's hot

Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008
Yardena Meymann
 
Lambda functions in java 8
Lambda functions in java 8Lambda functions in java 8
Lambda functions in java 8
James Brown
 

What's hot (20)

Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008
 
Scala @ TechMeetup Edinburgh
Scala @ TechMeetup EdinburghScala @ TechMeetup Edinburgh
Scala @ TechMeetup Edinburgh
 
Scala fundamentals
Scala fundamentalsScala fundamentals
Scala fundamentals
 
Scala in Practice
Scala in PracticeScala in Practice
Scala in Practice
 
Java7 New Features and Code Examples
Java7 New Features and Code ExamplesJava7 New Features and Code Examples
Java7 New Features and Code Examples
 
Static or Dynamic Typing? Why not both?
Static or Dynamic Typing? Why not both?Static or Dynamic Typing? Why not both?
Static or Dynamic Typing? Why not both?
 
Java 7 New Features
Java 7 New FeaturesJava 7 New Features
Java 7 New Features
 
A Tour Of Scala
A Tour Of ScalaA Tour Of Scala
A Tour Of Scala
 
Scala uma poderosa linguagem para a jvm
Scala   uma poderosa linguagem para a jvmScala   uma poderosa linguagem para a jvm
Scala uma poderosa linguagem para a jvm
 
Intro to Functional Programming in Scala
Intro to Functional Programming in ScalaIntro to Functional Programming in Scala
Intro to Functional Programming in Scala
 
Scala Intro
Scala IntroScala Intro
Scala Intro
 
Scale up your thinking
Scale up your thinkingScale up your thinking
Scale up your thinking
 
A Brief Intro to Scala
A Brief Intro to ScalaA Brief Intro to Scala
A Brief Intro to Scala
 
Solid and Sustainable Development in Scala
Solid and Sustainable Development in ScalaSolid and Sustainable Development in Scala
Solid and Sustainable Development in Scala
 
Scala ntnu
Scala ntnuScala ntnu
Scala ntnu
 
Programming in Scala: Notes
Programming in Scala: NotesProgramming in Scala: Notes
Programming in Scala: Notes
 
Lambda functions in java 8
Lambda functions in java 8Lambda functions in java 8
Lambda functions in java 8
 
Alternatives of JPA/Hibernate
Alternatives of JPA/HibernateAlternatives of JPA/Hibernate
Alternatives of JPA/Hibernate
 
An Introduction to Scala for Java Developers
An Introduction to Scala for Java DevelopersAn Introduction to Scala for Java Developers
An Introduction to Scala for Java Developers
 
Clojure, Plain and Simple
Clojure, Plain and SimpleClojure, Plain and Simple
Clojure, Plain and Simple
 

Viewers also liked (7)

Taller tablets
Taller tabletsTaller tablets
Taller tablets
 
firma electrónica
firma electrónica firma electrónica
firma electrónica
 
Smh3023 past year
Smh3023 past yearSmh3023 past year
Smh3023 past year
 
Instalación del Portón
Instalación del PortónInstalación del Portón
Instalación del Portón
 
Zurgiin file
Zurgiin fileZurgiin file
Zurgiin file
 
Piia Pekola, Ismo Linnosmaa, Hennamari Mikkola: Hintasääntely kuntoutuspalvel...
Piia Pekola, Ismo Linnosmaa, Hennamari Mikkola: Hintasääntely kuntoutuspalvel...Piia Pekola, Ismo Linnosmaa, Hennamari Mikkola: Hintasääntely kuntoutuspalvel...
Piia Pekola, Ismo Linnosmaa, Hennamari Mikkola: Hintasääntely kuntoutuspalvel...
 
Law Enforcement Administration II
Law Enforcement Administration IILaw Enforcement Administration II
Law Enforcement Administration II
 

Similar to Intro to scala

BCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java DevelopersBCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java Developers
Miles Sabin
 
A Sceptical Guide to Functional Programming
A Sceptical Guide to Functional ProgrammingA Sceptical Guide to Functional Programming
A Sceptical Guide to Functional Programming
Garth Gilmour
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
Miles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java DevelopersMiles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java Developers
Skills Matter
 

Similar to Intro to scala (20)

Scala for curious
Scala for curiousScala for curious
Scala for curious
 
Scala in a nutshell by venkat
Scala in a nutshell by venkatScala in a nutshell by venkat
Scala in a nutshell by venkat
 
BCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java DevelopersBCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java Developers
 
A Sceptical Guide to Functional Programming
A Sceptical Guide to Functional ProgrammingA Sceptical Guide to Functional Programming
A Sceptical Guide to Functional Programming
 
Scala
ScalaScala
Scala
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
Scala in Places API
Scala in Places APIScala in Places API
Scala in Places API
 
Scala - just good for Java shops?
Scala - just good for Java shops?Scala - just good for Java shops?
Scala - just good for Java shops?
 
Java
JavaJava
Java
 
Beyond java8
Beyond java8Beyond java8
Beyond java8
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Develop your next app with kotlin @ AndroidMakersFr 2017
Develop your next app with kotlin @ AndroidMakersFr 2017Develop your next app with kotlin @ AndroidMakersFr 2017
Develop your next app with kotlin @ AndroidMakersFr 2017
 
What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?
 
Scala - core features
Scala - core featuresScala - core features
Scala - core features
 
A Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java DevelopersA Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java Developers
 
Miles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java DevelopersMiles Sabin Introduction To Scala For Java Developers
Miles Sabin 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
  • 4.
  • 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

  1. Deterministic means free of side effectsTwo languages in one means a lot to learn
  2. 80% of all .equals methods are implemented incorrectly
  3. Mention type inference also
  4. What happens if you change name to userName
  5. Mention overloading also
  6. Mention overloading also