SlideShare una empresa de Scribd logo
1 de 58
Descargar para leer sin conexión
Scala
from “Hello, World” to “Heroku Scale”
Havoc Pennington, Typesafe
@havocp
Safe Harbor
 Safe harbor statement under the Private Securities Litigation Reform Act of 1995:

 This presentation may contain forward-looking statements that involve risks, uncertainties, and assumptions. If any such uncertainties materialize or if
 any of the assumptions proves incorrect, the results of salesforce.com, inc. could differ materially from the results expressed or implied by the forward-
 looking statements we make. All statements other than statements of historical fact could be deemed forward-looking, including any projections of
 product or service availability, subscriber growth, earnings, revenues, or other financial items and any statements regarding strategies or plans of
 management for future operations, statements of belief, any statements concerning new, planned, or upgraded services or technology developments
 and customer contracts or use of our services.

 The risks and uncertainties referred to above include – but are not limited to – risks associated with developing and delivering new functionality for our
 service, new products and services, our new business model, our past operating losses, possible fluctuations in our operating results and rate of growth,
 interruptions or delays in our Web hosting, breach of our security measures, the outcome of intellectual property and other litigation, risks associated
 with possible mergers and acquisitions, the immature market in which we operate, our relatively limited operating history, our ability to expand, retain,
 and motivate our employees and manage our growth, new releases of our service and successful customer deployment, our limited history reselling
 non-salesforce.com products, and utilization and selling to larger enterprise customers. Further information on potential factors that could affect the
 financial results of salesforce.com, inc. is included in our annual report on Form 10-Q for the most recent fiscal quarter ended July 31, 2012. This
 documents and others containing important disclosures are available on the SEC Filings section of the Investor Information section of our Web site.

 Any unreleased services or features referenced in this or other presentations, press releases or public statements are not currently available and may
 not be delivered on time or at all. Customers who purchase our services should make the purchase decisions based upon features that are currently
 available. Salesforce.com, inc. assumes no obligation and does not intend to update these forward-looking statements.
What does Typesafe do?
Modernizing development on the Java Virtual Machine

  – Scala: practical, superior alternative to Java with incremental migration path
  – Akka: proven Actor model gives horizontal scale for both Java and Scala,
    without the pain points of explicit threads
  – Play: popular Rails-style convention-over-configuration lightweight web
    framework for both Java and Scala
  – All supported on Heroku, a cloud platform with the same developer-friendly point
    of view

                     Developer-friendly, horizontally scalable,
                     with a pragmatic path to adoption
This Talk

This one introduces Scala

  – Will assume that you know some Java
  – Will briefly illustrate the Heroku build pack for Scala
  – Tomorrow’s talk will cover Akka, including its Java API
     – Friday 9 am
A Sampler

No way to teach you Scala in 45 minutes.
But you might be inspired to learn more based on what you see.
Getting to Know You

How many of you are already familiar with

  – Scala
  – Functional programming
  – Heroku

=> Any specific topics you hope to hear about in this talk?
Scala: Who Cares?
History and Motivation
Where it comes from
 Scala has established itself as one of the main alternative languages on the
 JVM.

 Created by Martin Odersky, who also designed generics in Java 5 and
 implemented the javac compiler. He works with a team at EPFL (École
 polytechnique fédérale de Lausanne) in addition to the team at Typesafe.

 1996 – 1997: Pizza
 1998 – 2000: GJ, Java generics, javac ( “make Java better” )
 2003 – 2006: The Scala “Experiment”
 2006 – 2009: An industrial strength programming language
              ( “make a better Java” )
Quick Scala Facts
•   Vibrant open source community
•   Wide commercial adoption
•   Catching fire last couple years, but has been maturing many years
•   Supports both object-oriented and functional styles
•   Great interoperability with Java
•   Static type safety
•   Type inference makes it concise like a dynamic language
•   “Scala” implies a “scalable language”
     – Horizontal scale across cores
     – Developer scale: manage complex projects
Select Commercial Users

                       Migrated core messaging service from Ruby        Scala drives its social graph service: 380-
                       to sustain                                       400 M transactions/day
                       3 orders of magnitude growth




                                                                        EU’s largest energy firm migrated a 300K lines
                          Entire web site and all services written in
                                                                        contract modeling platform from Java to Scala
                          Scala


Approved for general production
use
Community Traction
Open source ecosystem with
• Hundreds of contributors
• 20 books
• 40+ active user groups
• Plenty of github/stackoverflow activity
RedMonk GitHub / StackOverflow Analysis




StackOverflow Popularity




                           GitHub Popularity
Why Scala?

You've seen that Scala has a vibrant community and hockey-stick growth curve. Here's
  what I hope you'll see as we look at some Scala code:


•   Beautiful interoperability with Java means this is a practical way to move to a
    more modern language
•   Conciseness means dramatic improvement in maintainability and productivity
•   Support for immutable data structures (functional programming style) eliminates
    whole classes of concurrency bugs
•   Fun to do your work in a less tedious and more elegant way
Less To Type, So What?




  What matters is reading, rather than writing.
Less Code
When going from Java to Scala, expect at least a factor of 2 reduction in LOC.
                                                   Guy Steele: “Switching from Java to Scala reduced
                                                   size of Fortress typechecker by a factor of 4”.


But does it matter?
Doesn’t Eclipse write these extra lines for me?

This does matter. Eye-tracking experiments* show that for program
comprehension, average time spent per word of source code is constant.

So, roughly, half the code means half the time necessary to understand it.

       *G. Dubochet. Computer Code as a Medium for Human Communication: Are Programming Languages Improving?
       In 21st Annual Psychology of Programming Interest Group Conference, pages 174-187, Limerick, Ireland, 2009.
Let's See Some Scala
Hello.scala



              object Hello extends App {
                println("Hello, World")
              }
Hello.java

      public class Hello {
          public static void main(String[] args) {
              System.out.println("Hello, World");
          }
      }




                   Red = noise that adds no value

               Imagine reading a book with 50% irrelevant words!
Noise Reduction

                     object Hello extends App {
                       println("Hello, World")
                     }



      Notice anything missing?

            println is a standalone function so no object to invoke it on
            “object” keyword creates a singleton, no need for singleton machinery or
             “static”
            “App” provides main() boilerplate
            Class itself can have a body, in this case run from main()
            “public” is the default
            No semicolons
Something More Involved

A simple “data” class (bean?) to store a Person with “name” and “age” fields.
A Class ...
                public class Person {
                    public final String name;
                    public final int age;
                    Person(String name, int age) {
... in Java:            this.name = name;
                        this.age = age;
                    }
                }




                case class Person(name: String, age: Int)
... in Scala:
Don't Repeat Yourself?
         public class Person {
             public final String name;
             public final int age;
             Person(String name, int age) {
                  this.name = name;
                  this.age = age;
             }
         }

                We had to say “name” and “age” four times each.
                We had to say “Person” two times.
                We had to say “public” three times and “final” twice.
                We had to say “String” and “int” twice each.
Scala Says It Once

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



          A case class is an immutable (final) set of fields
          It automatically defines equals, hashCode, toString
           sensibly, the Java version does not (without even more
           code...)


                               Side note: Scala puts the type after the name
Pattern Matching

      Why is it called a case class?


 def selectCategory(person: Person) = person match {
   case Person("Havoc", _) => "Slow Race"
   case Person(_, age) if age > 60 => "Masters"
   case _ => "Regular"
 }
  (Stuff to notice: “_” as wildcard, no “return” keyword, implicit return type)
Working With Collections
                import java.util.ArrayList;
                ...
                Person[] people;
                Person[] minors;
                Person[] adults;
                {    ArrayList<Person> minorsList = new ArrayList<Person>();
                     ArrayList<Person> adultsList = new ArrayList<Person>();
... in Java:         for (int i = 0; i < people.length; i++)
                         (people[i].age < 18 ? minorsList : adultsList)
                            .add(people[i]);
                     minors = minorsList.toArray(people);
                     adults = adultsList.toArray(people);
                }




... in Scala:       val people: Array[Person]
                    val (minors, adults) = people partition (_.age < 18)
Anonymous Functions

     val people: Array[Person]
     val (minors, adults) = people partition (_.age < 18)



     // the short form
     _.age < 18

     // with named parameter
     person => person.age < 18

     // non-anonymous version
     def isMinor(person: Person): Boolean =
        person.age < 18
Less Is More




          Greatly Reducing Useless Noise =
           Much More Maintainable Code
Touring a Few More Tricks

           Type Inference
           Traits
           Flexible Method Call Syntax
           Implicits
           Java interoperability
Type Inference

        •   Omit explicit types when it's obvious


            // type String is inferred
            val s = “hello”

            // but spell it out if you want to
            val s2: String = “hello”
Traits: Interface + Code
         •   Scala has “traits” rather than interfaces; traits optionally contain some implementation
         •   With no implementation, they compile to interfaces
         •   Multiple inheritance Just Works (Google “Scala class linearization” for gory details that
             don't matter)


        trait Quacker {
          def quack = println("Quack")
        }

        trait Barker {
          def bark = println("Woof")
        }

        class DuckDog extends Quacker with Barker
Flexible Method Call Syntax
          •   To read Scala code you need to know: the dot and the parens can be
              left out when there's no ambiguity


       val s = "Hello"

       val len1 = s.length()
       val len2 = s.length
       val len3 = s length

       val s1 = s.substring(1)
       val s2 = s substring 1
Few Restrictions on Method Names
       •   You can name a method with any Unicode characters
       •   Some people call this “operator overloading” but it is NOT like C++ where “operators” are
           special. In Scala, it's just that Scala doesn't restrict method names.
       •   Yes you can name a method “⁋‡◾” but please use common sense!



       val a = 1
       val b = 2
       // “+” with method syntax
       println(a.+(b))
       // “+” with dot and parens omitted
       println(a + b)
Implicit Conversion

For example, the standard library contains:
 // Convert a      Byte to a wider numeric type
 implicit def      byte2short(x: Byte): Short = x.toShort
 implicit def      byte2int(x: Byte): Int = x.toInt
 implicit def      byte2long(x: Byte): Long = x.toLong
 implicit def      byte2float(x: Byte): Float = x.toFloat
 implicit def      byte2double(x: Byte): Double = x.toDouble



   This mechanism is available for any user-defined type.
Implicit Conversion to Find a Method

If a method doesn't exist, maybe a conversion can make it exist.
   Safe alternative to “monkey patching” (as in Ruby, JavaScript).
// Silly example

class Quacker {
  def quack = println("quack")
}

implicit def stringToQuacker(s: String): Quacker = new Quacker

"hello".quack
Practical Migration and Interoperability
• Scala differs from Java only on the     Java:    import java.net.URL;
  source code level                                Import java.io.InputStream;

• Once you have a .jar or .war, it just
                                                   URL u = new URL(“http://foo.com”);
  looks like Java                                  InputStream s = u.openStream();
                                                   s.close();
• Scala code can seamlessly import
  and use any Java class
• Projects can have a mix of Java
                                          Scala:   import java.net.URL

  and Scala files                                  val u = new URL(“http://foo.com”)
                                                   val s = u.openStream()
• Deploy Scala to any cloud or                     s.close()

  container that supports Java
Scala Has a Java Core
While Scala improves on Java, it keeps Java as the core
              the syntax is in the same general C++-like family
              on JVM level, classes are classes, methods are methods, etc.
              support for object-oriented programming in exactly Java style
              objects have toString, equals
              even annotations, generics, and other “advanced” features correspond between Java and
               Scala
              collections are different but trivial no-copy conversions are provided
              Static types
You can implement the same API in Scala that you would in Java; all the same once it's
  compiled to class files.
Scala can be used as “concise Java” right away, short learning curve.
Two Questions
Immutable Data



       •   Concise, readable code is one advantage of
           Scala
       •   Another huge one: support for immutable
           data
Immutability: Key to Functional Programming

• Emphasizes transformation (take a value, return a new value)
  over mutable state (take a value, change the value in-place)
• Think ƒ(x)
• Advantages include:
   – Inherently parallelizable and thread-safe
   – Enables many optimizations, such as lazy evaluation
   – Can make code more flexible and generic, for example by supporting
     composition
Mutable Data, Imperative Style (Java)

       public static void addOneToAll(ArrayList<Integer> items) {
         for (int i = 0; i < items.size(); ++i) {
            items.set(i, items.get(i) + 1);
         }
       }



         Mutates (modifies) the list in-place
Immutable Data, Functional Style (Java)

      public static List<Integer> addOneToAll(List<Integer> items) {
        ArrayList<Integer> result = new ArrayList<Integer>();
        for (int i : items) {
           result.add(i + 1);
        }
        return result;
      }


          Returns a new list, leaving original untouched
Mutable Data, Imperative Style (Scala)

     def addOneToAll(items : mutable.IndexedSeq[Int]) = {
       var i = 0
       while (i < items.length) {
          items.update(i, items(i) + 1)
          i += 1
       }
     }

          Mutates (modifies) the list in-place
Immutable Data, Functional Style (Scala)

        def addOneToAll(items : Seq[Int]) = items map { _ + 1 }



                                       Anonymous function applied to
                                       each item in the list




        Returns a new list, leaving original untouched
Language and library support

           Most importantly, library APIs all support use of immutable
            collections and other data
           But many small language features support immutability.



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

            // case classes automatically have “copy”
            def celebrateBirthday(person: Person) =
              person.copy(age=person.age+1)
Enabling Composition

       public static List<Integer> addTwoToAll(List<Integer> items) {
           return addOneToAll(addOneToAll(items));
       }




  (Composition is great for HTTP request handlers, by the way.)
Automatic Thread Correctness

The official Oracle tutorial on concurrency recommends immutable
 data, but Java APIs don't support it, and doing it properly in Java
 means lots of ceremony.


In Scala it's the default, as it should be.


Processors have more cores every day.
Switch to Immutable Style!

Even if you don't switch to Scala, this will improve your Java.


Reserve mutability for hotspots revealed by profiling.
Scala in Summary
• Beautiful interoperability with Java: mix Scala and Java as desired,
  no huge rewrites
• Conciseness means dramatic improvement in maintainability and
  productivity
• Functional style with immutable data seamlessly supports parallel
  computation and thread safety
• Vibrant community, hockey-stick growth curve, and available
  commercial support
Recommended: Read the Book

• Very clearly-written, by
  Scala's creator and designer
• Highly recommended to start
  here
Two Questions
Heroku's Scala Support

We have to solve three problems to run on Heroku


          •   Build the project
          •   Run the project
          •   Listen for HTTP requests
A Build Pack for SBT


        •   SBT = Simple Build Tool
        •   https://github.com/heroku/heroku-buildpack-scala
        •   Build pack is mis-named; it supports Java
            projects built with SBT, and you'd use another
            build pack for a Maven-built Scala project.
Project => Executable

    •   You need some way to build a Java command line with
        the proper classpath
    •   Simple solution: “Start Script Plugin” generates a script
        called “start” that just runs your main class with the
        proper classpath
    •   https://github.com/typesafehub/xsbt-start-script-plugin
Listening on HTTP

        •   Any HTTP server for the JVM is fine
        •   You need to pass it the PORT environment variable
            provided by Heroku
        •   We'll use Netty for an example
            •   You may be familiar with it already
            •   It illustrates using a Java API from Scala

        •   (Real web projects might use something like Play
            Framework instead)
Putting it together

Follow along at: https://github.com/havocp/dreamforce-heroku-
 example


Less than 200 lines to a deployed, scalable web server including
 build configuration and Netty boilerplate (around 100 lines is
 Netty stuff)
•   Follow us @Typesafe on Twitter, or typesafe.com
•   Read Programming in Scala by Martin Odersky
•   Try writing a discrete module or two in Scala
Scala from "Hello, World" to "Heroku Scale

Más contenido relacionado

Destacado

Spice up Your Internal Portal with Visualforce and Twitter Bootstrap
Spice up Your Internal Portal with Visualforce and Twitter BootstrapSpice up Your Internal Portal with Visualforce and Twitter Bootstrap
Spice up Your Internal Portal with Visualforce and Twitter BootstrapSalesforce Developers
 
Agile Development with Heroku webinar
Agile Development with Heroku webinarAgile Development with Heroku webinar
Agile Development with Heroku webinarSalesforce Developers
 
Preparing for Lightning: Replacing URL Hacks with Actions
Preparing for Lightning: Replacing URL Hacks with ActionsPreparing for Lightning: Replacing URL Hacks with Actions
Preparing for Lightning: Replacing URL Hacks with ActionsMike White
 
Team Development on Force.com with Github and Ant
Team Development on Force.com with Github and AntTeam Development on Force.com with Github and Ant
Team Development on Force.com with Github and AntSalesforce Developers
 
Easy REST Integrations with Lightning Components and Salesforce1
Easy REST Integrations with Lightning Components and Salesforce1Easy REST Integrations with Lightning Components and Salesforce1
Easy REST Integrations with Lightning Components and Salesforce1Salesforce Developers
 
Lightning Components - Advanced Features
Lightning Components - Advanced FeaturesLightning Components - Advanced Features
Lightning Components - Advanced FeaturesSalesforce Developers
 
Secure Development on the Salesforce Platform - Part 3
Secure Development on the Salesforce Platform - Part 3Secure Development on the Salesforce Platform - Part 3
Secure Development on the Salesforce Platform - Part 3Mark Adcock
 
Build, Manage, and Deploy Mobile Apps Faster with App Cloud Mobile
Build, Manage, and Deploy Mobile Apps Faster with App Cloud MobileBuild, Manage, and Deploy Mobile Apps Faster with App Cloud Mobile
Build, Manage, and Deploy Mobile Apps Faster with App Cloud MobileSalesforce Developers
 
Advanced Platform Series - OAuth and Social Authentication
Advanced Platform Series - OAuth and Social AuthenticationAdvanced Platform Series - OAuth and Social Authentication
Advanced Platform Series - OAuth and Social AuthenticationSalesforce Developers
 
Javascript Security and Lightning Locker Service
Javascript Security and Lightning Locker ServiceJavascript Security and Lightning Locker Service
Javascript Security and Lightning Locker ServiceSalesforce Developers
 
Secure Development on the Salesforce Platform - Part 2
Secure Development on the Salesforce Platform - Part 2Secure Development on the Salesforce Platform - Part 2
Secure Development on the Salesforce Platform - Part 2Salesforce Developers
 
Building apps faster with lightning and winter '17
Building apps faster with lightning and winter '17Building apps faster with lightning and winter '17
Building apps faster with lightning and winter '17Salesforce Developers
 
Lightning Experience with Visualforce Best Practices
Lightning Experience with Visualforce Best PracticesLightning Experience with Visualforce Best Practices
Lightning Experience with Visualforce Best PracticesSalesforce Developers
 

Destacado (15)

Spice up Your Internal Portal with Visualforce and Twitter Bootstrap
Spice up Your Internal Portal with Visualforce and Twitter BootstrapSpice up Your Internal Portal with Visualforce and Twitter Bootstrap
Spice up Your Internal Portal with Visualforce and Twitter Bootstrap
 
Agile Development with Heroku webinar
Agile Development with Heroku webinarAgile Development with Heroku webinar
Agile Development with Heroku webinar
 
Preparing for Lightning: Replacing URL Hacks with Actions
Preparing for Lightning: Replacing URL Hacks with ActionsPreparing for Lightning: Replacing URL Hacks with Actions
Preparing for Lightning: Replacing URL Hacks with Actions
 
Team Development on Force.com with Github and Ant
Team Development on Force.com with Github and AntTeam Development on Force.com with Github and Ant
Team Development on Force.com with Github and Ant
 
Lightning Components Workshop
Lightning Components WorkshopLightning Components Workshop
Lightning Components Workshop
 
Using Visualforce in Salesforce1
Using Visualforce in Salesforce1Using Visualforce in Salesforce1
Using Visualforce in Salesforce1
 
Easy REST Integrations with Lightning Components and Salesforce1
Easy REST Integrations with Lightning Components and Salesforce1Easy REST Integrations with Lightning Components and Salesforce1
Easy REST Integrations with Lightning Components and Salesforce1
 
Lightning Components - Advanced Features
Lightning Components - Advanced FeaturesLightning Components - Advanced Features
Lightning Components - Advanced Features
 
Secure Development on the Salesforce Platform - Part 3
Secure Development on the Salesforce Platform - Part 3Secure Development on the Salesforce Platform - Part 3
Secure Development on the Salesforce Platform - Part 3
 
Build, Manage, and Deploy Mobile Apps Faster with App Cloud Mobile
Build, Manage, and Deploy Mobile Apps Faster with App Cloud MobileBuild, Manage, and Deploy Mobile Apps Faster with App Cloud Mobile
Build, Manage, and Deploy Mobile Apps Faster with App Cloud Mobile
 
Advanced Platform Series - OAuth and Social Authentication
Advanced Platform Series - OAuth and Social AuthenticationAdvanced Platform Series - OAuth and Social Authentication
Advanced Platform Series - OAuth and Social Authentication
 
Javascript Security and Lightning Locker Service
Javascript Security and Lightning Locker ServiceJavascript Security and Lightning Locker Service
Javascript Security and Lightning Locker Service
 
Secure Development on the Salesforce Platform - Part 2
Secure Development on the Salesforce Platform - Part 2Secure Development on the Salesforce Platform - Part 2
Secure Development on the Salesforce Platform - Part 2
 
Building apps faster with lightning and winter '17
Building apps faster with lightning and winter '17Building apps faster with lightning and winter '17
Building apps faster with lightning and winter '17
 
Lightning Experience with Visualforce Best Practices
Lightning Experience with Visualforce Best PracticesLightning Experience with Visualforce Best Practices
Lightning Experience with Visualforce Best Practices
 

Similar a Scala from "Hello, World" to "Heroku Scale

Scala and jvm_languages_praveen_technologist
Scala and jvm_languages_praveen_technologistScala and jvm_languages_praveen_technologist
Scala and jvm_languages_praveen_technologistpmanvi
 
Martin Odersky: What's next for Scala
Martin Odersky: What's next for ScalaMartin Odersky: What's next for Scala
Martin Odersky: What's next for ScalaMarakana Inc.
 
Infographic on Scala Programming Language
Infographic on Scala Programming LanguageInfographic on Scala Programming Language
Infographic on Scala Programming LanguagePaddy Lock
 
Refactoring to Scala DSLs and LiftOff 2009 Recap
Refactoring to Scala DSLs and LiftOff 2009 RecapRefactoring to Scala DSLs and LiftOff 2009 Recap
Refactoring to Scala DSLs and LiftOff 2009 RecapDave Orme
 
Scala Days San Francisco
Scala Days San FranciscoScala Days San Francisco
Scala Days San FranciscoMartin Odersky
 
Rafael Bagmanov «Scala in a wild enterprise»
Rafael Bagmanov «Scala in a wild enterprise»Rafael Bagmanov «Scala in a wild enterprise»
Rafael Bagmanov «Scala in a wild enterprise»e-Legion
 
Sviluppare applicazioni nell'era dei "Big Data" con Scala e Spark - Mario Car...
Sviluppare applicazioni nell'era dei "Big Data" con Scala e Spark - Mario Car...Sviluppare applicazioni nell'era dei "Big Data" con Scala e Spark - Mario Car...
Sviluppare applicazioni nell'era dei "Big Data" con Scala e Spark - Mario Car...Codemotion
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming LanguageHaim Michael
 
Sviluppare applicazioni nell'era dei "Big Data" con Scala e Spark - Mario Car...
Sviluppare applicazioni nell'era dei "Big Data" con Scala e Spark - Mario Car...Sviluppare applicazioni nell'era dei "Big Data" con Scala e Spark - Mario Car...
Sviluppare applicazioni nell'era dei "Big Data" con Scala e Spark - Mario Car...Codemotion
 
The Why and How of Scala at Twitter
The Why and How of Scala at TwitterThe Why and How of Scala at Twitter
The Why and How of Scala at TwitterAlex Payne
 
Scala final ppt vinay
Scala final ppt vinayScala final ppt vinay
Scala final ppt vinayViplav Jain
 
Why scala - executive overview
Why scala - executive overviewWhy scala - executive overview
Why scala - executive overviewRazvan Cojocaru
 
Oscon keynote: Working hard to keep it simple
Oscon keynote: Working hard to keep it simpleOscon keynote: Working hard to keep it simple
Oscon keynote: Working hard to keep it simpleMartin Odersky
 
Introduction to Scala JS
Introduction to Scala JSIntroduction to Scala JS
Introduction to Scala JSKnoldus Inc.
 
Scala overview
Scala overviewScala overview
Scala overviewSteve Min
 

Similar a Scala from "Hello, World" to "Heroku Scale (20)

Scala and jvm_languages_praveen_technologist
Scala and jvm_languages_praveen_technologistScala and jvm_languages_praveen_technologist
Scala and jvm_languages_praveen_technologist
 
Devoxx
DevoxxDevoxx
Devoxx
 
Martin Odersky: What's next for Scala
Martin Odersky: What's next for ScalaMartin Odersky: What's next for Scala
Martin Odersky: What's next for Scala
 
Infographic on Scala Programming Language
Infographic on Scala Programming LanguageInfographic on Scala Programming Language
Infographic on Scala Programming Language
 
Refactoring to Scala DSLs and LiftOff 2009 Recap
Refactoring to Scala DSLs and LiftOff 2009 RecapRefactoring to Scala DSLs and LiftOff 2009 Recap
Refactoring to Scala DSLs and LiftOff 2009 Recap
 
Scala Days San Francisco
Scala Days San FranciscoScala Days San Francisco
Scala Days San Francisco
 
Rafael Bagmanov «Scala in a wild enterprise»
Rafael Bagmanov «Scala in a wild enterprise»Rafael Bagmanov «Scala in a wild enterprise»
Rafael Bagmanov «Scala in a wild enterprise»
 
Sviluppare applicazioni nell'era dei "Big Data" con Scala e Spark - Mario Car...
Sviluppare applicazioni nell'era dei "Big Data" con Scala e Spark - Mario Car...Sviluppare applicazioni nell'era dei "Big Data" con Scala e Spark - Mario Car...
Sviluppare applicazioni nell'era dei "Big Data" con Scala e Spark - Mario Car...
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Language
 
Sviluppare applicazioni nell'era dei "Big Data" con Scala e Spark - Mario Car...
Sviluppare applicazioni nell'era dei "Big Data" con Scala e Spark - Mario Car...Sviluppare applicazioni nell'era dei "Big Data" con Scala e Spark - Mario Car...
Sviluppare applicazioni nell'era dei "Big Data" con Scala e Spark - Mario Car...
 
Quick introduction to scala
Quick introduction to scalaQuick introduction to scala
Quick introduction to scala
 
The Why and How of Scala at Twitter
The Why and How of Scala at TwitterThe Why and How of Scala at Twitter
The Why and How of Scala at Twitter
 
Scala final ppt vinay
Scala final ppt vinayScala final ppt vinay
Scala final ppt vinay
 
Why scala - executive overview
Why scala - executive overviewWhy scala - executive overview
Why scala - executive overview
 
Scala Introduction
Scala IntroductionScala Introduction
Scala Introduction
 
Oscon keynote: Working hard to keep it simple
Oscon keynote: Working hard to keep it simpleOscon keynote: Working hard to keep it simple
Oscon keynote: Working hard to keep it simple
 
Introduction to Scala JS
Introduction to Scala JSIntroduction to Scala JS
Introduction to Scala JS
 
Scala overview
Scala overviewScala overview
Scala overview
 
Preparing for Scala 3
Preparing for Scala 3Preparing for Scala 3
Preparing for Scala 3
 
Scala-Ls1
Scala-Ls1Scala-Ls1
Scala-Ls1
 

Más de Salesforce Developers

Sample Gallery: Reference Code and Best Practices for Salesforce Developers
Sample Gallery: Reference Code and Best Practices for Salesforce DevelopersSample Gallery: Reference Code and Best Practices for Salesforce Developers
Sample Gallery: Reference Code and Best Practices for Salesforce DevelopersSalesforce Developers
 
Maximizing Salesforce Lightning Experience and Lightning Component Performance
Maximizing Salesforce Lightning Experience and Lightning Component PerformanceMaximizing Salesforce Lightning Experience and Lightning Component Performance
Maximizing Salesforce Lightning Experience and Lightning Component PerformanceSalesforce Developers
 
Local development with Open Source Base Components
Local development with Open Source Base ComponentsLocal development with Open Source Base Components
Local development with Open Source Base ComponentsSalesforce Developers
 
TrailheaDX India : Developer Highlights
TrailheaDX India : Developer HighlightsTrailheaDX India : Developer Highlights
TrailheaDX India : Developer HighlightsSalesforce Developers
 
Why developers shouldn’t miss TrailheaDX India
Why developers shouldn’t miss TrailheaDX IndiaWhy developers shouldn’t miss TrailheaDX India
Why developers shouldn’t miss TrailheaDX IndiaSalesforce Developers
 
CodeLive: Build Lightning Web Components faster with Local Development
CodeLive: Build Lightning Web Components faster with Local DevelopmentCodeLive: Build Lightning Web Components faster with Local Development
CodeLive: Build Lightning Web Components faster with Local DevelopmentSalesforce Developers
 
CodeLive: Converting Aura Components to Lightning Web Components
CodeLive: Converting Aura Components to Lightning Web ComponentsCodeLive: Converting Aura Components to Lightning Web Components
CodeLive: Converting Aura Components to Lightning Web ComponentsSalesforce Developers
 
Enterprise-grade UI with open source Lightning Web Components
Enterprise-grade UI with open source Lightning Web ComponentsEnterprise-grade UI with open source Lightning Web Components
Enterprise-grade UI with open source Lightning Web ComponentsSalesforce Developers
 
TrailheaDX and Summer '19: Developer Highlights
TrailheaDX and Summer '19: Developer HighlightsTrailheaDX and Summer '19: Developer Highlights
TrailheaDX and Summer '19: Developer HighlightsSalesforce Developers
 
Lightning web components - Episode 4 : Security and Testing
Lightning web components  - Episode 4 : Security and TestingLightning web components  - Episode 4 : Security and Testing
Lightning web components - Episode 4 : Security and TestingSalesforce Developers
 
LWC Episode 3- Component Communication and Aura Interoperability
LWC Episode 3- Component Communication and Aura InteroperabilityLWC Episode 3- Component Communication and Aura Interoperability
LWC Episode 3- Component Communication and Aura InteroperabilitySalesforce Developers
 
Lightning web components episode 2- work with salesforce data
Lightning web components   episode 2- work with salesforce dataLightning web components   episode 2- work with salesforce data
Lightning web components episode 2- work with salesforce dataSalesforce Developers
 
Lightning web components - Episode 1 - An Introduction
Lightning web components - Episode 1 - An IntroductionLightning web components - Episode 1 - An Introduction
Lightning web components - Episode 1 - An IntroductionSalesforce Developers
 
Migrating CPQ to Advanced Calculator and JSQCP
Migrating CPQ to Advanced Calculator and JSQCPMigrating CPQ to Advanced Calculator and JSQCP
Migrating CPQ to Advanced Calculator and JSQCPSalesforce Developers
 
Scale with Large Data Volumes and Big Objects in Salesforce
Scale with Large Data Volumes and Big Objects in SalesforceScale with Large Data Volumes and Big Objects in Salesforce
Scale with Large Data Volumes and Big Objects in SalesforceSalesforce Developers
 
Replicate Salesforce Data in Real Time with Change Data Capture
Replicate Salesforce Data in Real Time with Change Data CaptureReplicate Salesforce Data in Real Time with Change Data Capture
Replicate Salesforce Data in Real Time with Change Data CaptureSalesforce Developers
 
Modern Development with Salesforce DX
Modern Development with Salesforce DXModern Development with Salesforce DX
Modern Development with Salesforce DXSalesforce Developers
 
Integrate CMS Content Into Lightning Communities with CMS Connect
Integrate CMS Content Into Lightning Communities with CMS ConnectIntegrate CMS Content Into Lightning Communities with CMS Connect
Integrate CMS Content Into Lightning Communities with CMS ConnectSalesforce Developers
 

Más de Salesforce Developers (20)

Sample Gallery: Reference Code and Best Practices for Salesforce Developers
Sample Gallery: Reference Code and Best Practices for Salesforce DevelopersSample Gallery: Reference Code and Best Practices for Salesforce Developers
Sample Gallery: Reference Code and Best Practices for Salesforce Developers
 
Maximizing Salesforce Lightning Experience and Lightning Component Performance
Maximizing Salesforce Lightning Experience and Lightning Component PerformanceMaximizing Salesforce Lightning Experience and Lightning Component Performance
Maximizing Salesforce Lightning Experience and Lightning Component Performance
 
Local development with Open Source Base Components
Local development with Open Source Base ComponentsLocal development with Open Source Base Components
Local development with Open Source Base Components
 
TrailheaDX India : Developer Highlights
TrailheaDX India : Developer HighlightsTrailheaDX India : Developer Highlights
TrailheaDX India : Developer Highlights
 
Why developers shouldn’t miss TrailheaDX India
Why developers shouldn’t miss TrailheaDX IndiaWhy developers shouldn’t miss TrailheaDX India
Why developers shouldn’t miss TrailheaDX India
 
CodeLive: Build Lightning Web Components faster with Local Development
CodeLive: Build Lightning Web Components faster with Local DevelopmentCodeLive: Build Lightning Web Components faster with Local Development
CodeLive: Build Lightning Web Components faster with Local Development
 
CodeLive: Converting Aura Components to Lightning Web Components
CodeLive: Converting Aura Components to Lightning Web ComponentsCodeLive: Converting Aura Components to Lightning Web Components
CodeLive: Converting Aura Components to Lightning Web Components
 
Enterprise-grade UI with open source Lightning Web Components
Enterprise-grade UI with open source Lightning Web ComponentsEnterprise-grade UI with open source Lightning Web Components
Enterprise-grade UI with open source Lightning Web Components
 
TrailheaDX and Summer '19: Developer Highlights
TrailheaDX and Summer '19: Developer HighlightsTrailheaDX and Summer '19: Developer Highlights
TrailheaDX and Summer '19: Developer Highlights
 
Live coding with LWC
Live coding with LWCLive coding with LWC
Live coding with LWC
 
Lightning web components - Episode 4 : Security and Testing
Lightning web components  - Episode 4 : Security and TestingLightning web components  - Episode 4 : Security and Testing
Lightning web components - Episode 4 : Security and Testing
 
LWC Episode 3- Component Communication and Aura Interoperability
LWC Episode 3- Component Communication and Aura InteroperabilityLWC Episode 3- Component Communication and Aura Interoperability
LWC Episode 3- Component Communication and Aura Interoperability
 
Lightning web components episode 2- work with salesforce data
Lightning web components   episode 2- work with salesforce dataLightning web components   episode 2- work with salesforce data
Lightning web components episode 2- work with salesforce data
 
Lightning web components - Episode 1 - An Introduction
Lightning web components - Episode 1 - An IntroductionLightning web components - Episode 1 - An Introduction
Lightning web components - Episode 1 - An Introduction
 
Migrating CPQ to Advanced Calculator and JSQCP
Migrating CPQ to Advanced Calculator and JSQCPMigrating CPQ to Advanced Calculator and JSQCP
Migrating CPQ to Advanced Calculator and JSQCP
 
Scale with Large Data Volumes and Big Objects in Salesforce
Scale with Large Data Volumes and Big Objects in SalesforceScale with Large Data Volumes and Big Objects in Salesforce
Scale with Large Data Volumes and Big Objects in Salesforce
 
Replicate Salesforce Data in Real Time with Change Data Capture
Replicate Salesforce Data in Real Time with Change Data CaptureReplicate Salesforce Data in Real Time with Change Data Capture
Replicate Salesforce Data in Real Time with Change Data Capture
 
Modern Development with Salesforce DX
Modern Development with Salesforce DXModern Development with Salesforce DX
Modern Development with Salesforce DX
 
Get Into Lightning Flow Development
Get Into Lightning Flow DevelopmentGet Into Lightning Flow Development
Get Into Lightning Flow Development
 
Integrate CMS Content Into Lightning Communities with CMS Connect
Integrate CMS Content Into Lightning Communities with CMS ConnectIntegrate CMS Content Into Lightning Communities with CMS Connect
Integrate CMS Content Into Lightning Communities with CMS Connect
 

Scala from "Hello, World" to "Heroku Scale

  • 1. Scala from “Hello, World” to “Heroku Scale” Havoc Pennington, Typesafe @havocp
  • 2. Safe Harbor Safe harbor statement under the Private Securities Litigation Reform Act of 1995: This presentation may contain forward-looking statements that involve risks, uncertainties, and assumptions. If any such uncertainties materialize or if any of the assumptions proves incorrect, the results of salesforce.com, inc. could differ materially from the results expressed or implied by the forward- looking statements we make. All statements other than statements of historical fact could be deemed forward-looking, including any projections of product or service availability, subscriber growth, earnings, revenues, or other financial items and any statements regarding strategies or plans of management for future operations, statements of belief, any statements concerning new, planned, or upgraded services or technology developments and customer contracts or use of our services. The risks and uncertainties referred to above include – but are not limited to – risks associated with developing and delivering new functionality for our service, new products and services, our new business model, our past operating losses, possible fluctuations in our operating results and rate of growth, interruptions or delays in our Web hosting, breach of our security measures, the outcome of intellectual property and other litigation, risks associated with possible mergers and acquisitions, the immature market in which we operate, our relatively limited operating history, our ability to expand, retain, and motivate our employees and manage our growth, new releases of our service and successful customer deployment, our limited history reselling non-salesforce.com products, and utilization and selling to larger enterprise customers. Further information on potential factors that could affect the financial results of salesforce.com, inc. is included in our annual report on Form 10-Q for the most recent fiscal quarter ended July 31, 2012. This documents and others containing important disclosures are available on the SEC Filings section of the Investor Information section of our Web site. Any unreleased services or features referenced in this or other presentations, press releases or public statements are not currently available and may not be delivered on time or at all. Customers who purchase our services should make the purchase decisions based upon features that are currently available. Salesforce.com, inc. assumes no obligation and does not intend to update these forward-looking statements.
  • 3. What does Typesafe do? Modernizing development on the Java Virtual Machine – Scala: practical, superior alternative to Java with incremental migration path – Akka: proven Actor model gives horizontal scale for both Java and Scala, without the pain points of explicit threads – Play: popular Rails-style convention-over-configuration lightweight web framework for both Java and Scala – All supported on Heroku, a cloud platform with the same developer-friendly point of view Developer-friendly, horizontally scalable, with a pragmatic path to adoption
  • 4. This Talk This one introduces Scala – Will assume that you know some Java – Will briefly illustrate the Heroku build pack for Scala – Tomorrow’s talk will cover Akka, including its Java API – Friday 9 am
  • 5. A Sampler No way to teach you Scala in 45 minutes. But you might be inspired to learn more based on what you see.
  • 6. Getting to Know You How many of you are already familiar with – Scala – Functional programming – Heroku => Any specific topics you hope to hear about in this talk?
  • 7. Scala: Who Cares? History and Motivation
  • 8. Where it comes from Scala has established itself as one of the main alternative languages on the JVM. Created by Martin Odersky, who also designed generics in Java 5 and implemented the javac compiler. He works with a team at EPFL (École polytechnique fédérale de Lausanne) in addition to the team at Typesafe. 1996 – 1997: Pizza 1998 – 2000: GJ, Java generics, javac ( “make Java better” ) 2003 – 2006: The Scala “Experiment” 2006 – 2009: An industrial strength programming language ( “make a better Java” )
  • 9. Quick Scala Facts • Vibrant open source community • Wide commercial adoption • Catching fire last couple years, but has been maturing many years • Supports both object-oriented and functional styles • Great interoperability with Java • Static type safety • Type inference makes it concise like a dynamic language • “Scala” implies a “scalable language” – Horizontal scale across cores – Developer scale: manage complex projects
  • 10.
  • 11. Select Commercial Users Migrated core messaging service from Ruby Scala drives its social graph service: 380- to sustain 400 M transactions/day 3 orders of magnitude growth EU’s largest energy firm migrated a 300K lines Entire web site and all services written in contract modeling platform from Java to Scala Scala Approved for general production use
  • 12. Community Traction Open source ecosystem with • Hundreds of contributors • 20 books • 40+ active user groups • Plenty of github/stackoverflow activity
  • 13. RedMonk GitHub / StackOverflow Analysis StackOverflow Popularity GitHub Popularity
  • 14. Why Scala? You've seen that Scala has a vibrant community and hockey-stick growth curve. Here's what I hope you'll see as we look at some Scala code: • Beautiful interoperability with Java means this is a practical way to move to a more modern language • Conciseness means dramatic improvement in maintainability and productivity • Support for immutable data structures (functional programming style) eliminates whole classes of concurrency bugs • Fun to do your work in a less tedious and more elegant way
  • 15. Less To Type, So What? What matters is reading, rather than writing.
  • 16. Less Code When going from Java to Scala, expect at least a factor of 2 reduction in LOC. Guy Steele: “Switching from Java to Scala reduced size of Fortress typechecker by a factor of 4”. But does it matter? Doesn’t Eclipse write these extra lines for me? This does matter. Eye-tracking experiments* show that for program comprehension, average time spent per word of source code is constant. So, roughly, half the code means half the time necessary to understand it. *G. Dubochet. Computer Code as a Medium for Human Communication: Are Programming Languages Improving? In 21st Annual Psychology of Programming Interest Group Conference, pages 174-187, Limerick, Ireland, 2009.
  • 17. Let's See Some Scala
  • 18. Hello.scala object Hello extends App { println("Hello, World") }
  • 19. Hello.java public class Hello { public static void main(String[] args) { System.out.println("Hello, World"); } } Red = noise that adds no value Imagine reading a book with 50% irrelevant words!
  • 20. Noise Reduction object Hello extends App { println("Hello, World") } Notice anything missing?  println is a standalone function so no object to invoke it on  “object” keyword creates a singleton, no need for singleton machinery or “static”  “App” provides main() boilerplate  Class itself can have a body, in this case run from main()  “public” is the default  No semicolons
  • 21. Something More Involved A simple “data” class (bean?) to store a Person with “name” and “age” fields.
  • 22. A Class ... public class Person { public final String name; public final int age; Person(String name, int age) { ... in Java: this.name = name; this.age = age; } } case class Person(name: String, age: Int) ... in Scala:
  • 23. Don't Repeat Yourself? public class Person { public final String name; public final int age; Person(String name, int age) { this.name = name; this.age = age; } }  We had to say “name” and “age” four times each.  We had to say “Person” two times.  We had to say “public” three times and “final” twice.  We had to say “String” and “int” twice each.
  • 24. Scala Says It Once case class Person(name: String, age: Int)  A case class is an immutable (final) set of fields  It automatically defines equals, hashCode, toString sensibly, the Java version does not (without even more code...) Side note: Scala puts the type after the name
  • 25. Pattern Matching  Why is it called a case class? def selectCategory(person: Person) = person match { case Person("Havoc", _) => "Slow Race" case Person(_, age) if age > 60 => "Masters" case _ => "Regular" } (Stuff to notice: “_” as wildcard, no “return” keyword, implicit return type)
  • 26. Working With Collections import java.util.ArrayList; ... Person[] people; Person[] minors; Person[] adults; { ArrayList<Person> minorsList = new ArrayList<Person>(); ArrayList<Person> adultsList = new ArrayList<Person>(); ... in Java: for (int i = 0; i < people.length; i++) (people[i].age < 18 ? minorsList : adultsList) .add(people[i]); minors = minorsList.toArray(people); adults = adultsList.toArray(people); } ... in Scala: val people: Array[Person] val (minors, adults) = people partition (_.age < 18)
  • 27. Anonymous Functions val people: Array[Person] val (minors, adults) = people partition (_.age < 18) // the short form _.age < 18 // with named parameter person => person.age < 18 // non-anonymous version def isMinor(person: Person): Boolean = person.age < 18
  • 28. Less Is More Greatly Reducing Useless Noise = Much More Maintainable Code
  • 29. Touring a Few More Tricks  Type Inference  Traits  Flexible Method Call Syntax  Implicits  Java interoperability
  • 30. Type Inference • Omit explicit types when it's obvious // type String is inferred val s = “hello” // but spell it out if you want to val s2: String = “hello”
  • 31. Traits: Interface + Code • Scala has “traits” rather than interfaces; traits optionally contain some implementation • With no implementation, they compile to interfaces • Multiple inheritance Just Works (Google “Scala class linearization” for gory details that don't matter) trait Quacker { def quack = println("Quack") } trait Barker { def bark = println("Woof") } class DuckDog extends Quacker with Barker
  • 32. Flexible Method Call Syntax • To read Scala code you need to know: the dot and the parens can be left out when there's no ambiguity val s = "Hello" val len1 = s.length() val len2 = s.length val len3 = s length val s1 = s.substring(1) val s2 = s substring 1
  • 33. Few Restrictions on Method Names • You can name a method with any Unicode characters • Some people call this “operator overloading” but it is NOT like C++ where “operators” are special. In Scala, it's just that Scala doesn't restrict method names. • Yes you can name a method “⁋‡◾” but please use common sense! val a = 1 val b = 2 // “+” with method syntax println(a.+(b)) // “+” with dot and parens omitted println(a + b)
  • 34. Implicit Conversion For example, the standard library contains: // Convert a Byte to a wider numeric type implicit def byte2short(x: Byte): Short = x.toShort implicit def byte2int(x: Byte): Int = x.toInt implicit def byte2long(x: Byte): Long = x.toLong implicit def byte2float(x: Byte): Float = x.toFloat implicit def byte2double(x: Byte): Double = x.toDouble This mechanism is available for any user-defined type.
  • 35. Implicit Conversion to Find a Method If a method doesn't exist, maybe a conversion can make it exist. Safe alternative to “monkey patching” (as in Ruby, JavaScript). // Silly example class Quacker { def quack = println("quack") } implicit def stringToQuacker(s: String): Quacker = new Quacker "hello".quack
  • 36. Practical Migration and Interoperability • Scala differs from Java only on the Java: import java.net.URL; source code level Import java.io.InputStream; • Once you have a .jar or .war, it just URL u = new URL(“http://foo.com”); looks like Java InputStream s = u.openStream(); s.close(); • Scala code can seamlessly import and use any Java class • Projects can have a mix of Java Scala: import java.net.URL and Scala files val u = new URL(“http://foo.com”) val s = u.openStream() • Deploy Scala to any cloud or s.close() container that supports Java
  • 37. Scala Has a Java Core While Scala improves on Java, it keeps Java as the core  the syntax is in the same general C++-like family  on JVM level, classes are classes, methods are methods, etc.  support for object-oriented programming in exactly Java style  objects have toString, equals  even annotations, generics, and other “advanced” features correspond between Java and Scala  collections are different but trivial no-copy conversions are provided  Static types You can implement the same API in Scala that you would in Java; all the same once it's compiled to class files. Scala can be used as “concise Java” right away, short learning curve.
  • 39. Immutable Data • Concise, readable code is one advantage of Scala • Another huge one: support for immutable data
  • 40. Immutability: Key to Functional Programming • Emphasizes transformation (take a value, return a new value) over mutable state (take a value, change the value in-place) • Think ƒ(x) • Advantages include: – Inherently parallelizable and thread-safe – Enables many optimizations, such as lazy evaluation – Can make code more flexible and generic, for example by supporting composition
  • 41. Mutable Data, Imperative Style (Java) public static void addOneToAll(ArrayList<Integer> items) { for (int i = 0; i < items.size(); ++i) { items.set(i, items.get(i) + 1); } } Mutates (modifies) the list in-place
  • 42. Immutable Data, Functional Style (Java) public static List<Integer> addOneToAll(List<Integer> items) { ArrayList<Integer> result = new ArrayList<Integer>(); for (int i : items) { result.add(i + 1); } return result; } Returns a new list, leaving original untouched
  • 43. Mutable Data, Imperative Style (Scala) def addOneToAll(items : mutable.IndexedSeq[Int]) = { var i = 0 while (i < items.length) { items.update(i, items(i) + 1) i += 1 } } Mutates (modifies) the list in-place
  • 44. Immutable Data, Functional Style (Scala) def addOneToAll(items : Seq[Int]) = items map { _ + 1 } Anonymous function applied to each item in the list Returns a new list, leaving original untouched
  • 45. Language and library support  Most importantly, library APIs all support use of immutable collections and other data  But many small language features support immutability. case class Person(name: String, age: Int) // case classes automatically have “copy” def celebrateBirthday(person: Person) = person.copy(age=person.age+1)
  • 46. Enabling Composition public static List<Integer> addTwoToAll(List<Integer> items) { return addOneToAll(addOneToAll(items)); } (Composition is great for HTTP request handlers, by the way.)
  • 47. Automatic Thread Correctness The official Oracle tutorial on concurrency recommends immutable data, but Java APIs don't support it, and doing it properly in Java means lots of ceremony. In Scala it's the default, as it should be. Processors have more cores every day.
  • 48. Switch to Immutable Style! Even if you don't switch to Scala, this will improve your Java. Reserve mutability for hotspots revealed by profiling.
  • 49. Scala in Summary • Beautiful interoperability with Java: mix Scala and Java as desired, no huge rewrites • Conciseness means dramatic improvement in maintainability and productivity • Functional style with immutable data seamlessly supports parallel computation and thread safety • Vibrant community, hockey-stick growth curve, and available commercial support
  • 50. Recommended: Read the Book • Very clearly-written, by Scala's creator and designer • Highly recommended to start here
  • 52. Heroku's Scala Support We have to solve three problems to run on Heroku • Build the project • Run the project • Listen for HTTP requests
  • 53. A Build Pack for SBT • SBT = Simple Build Tool • https://github.com/heroku/heroku-buildpack-scala • Build pack is mis-named; it supports Java projects built with SBT, and you'd use another build pack for a Maven-built Scala project.
  • 54. Project => Executable • You need some way to build a Java command line with the proper classpath • Simple solution: “Start Script Plugin” generates a script called “start” that just runs your main class with the proper classpath • https://github.com/typesafehub/xsbt-start-script-plugin
  • 55. Listening on HTTP • Any HTTP server for the JVM is fine • You need to pass it the PORT environment variable provided by Heroku • We'll use Netty for an example • You may be familiar with it already • It illustrates using a Java API from Scala • (Real web projects might use something like Play Framework instead)
  • 56. Putting it together Follow along at: https://github.com/havocp/dreamforce-heroku- example Less than 200 lines to a deployed, scalable web server including build configuration and Netty boilerplate (around 100 lines is Netty stuff)
  • 57. Follow us @Typesafe on Twitter, or typesafe.com • Read Programming in Scala by Martin Odersky • Try writing a discrete module or two in Scala