Scala - from "Hello, World" to "Heroku Scale"


Published on

  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Scala - from "Hello, World" to "Heroku Scale"

  1. 1. Scalafrom “Hello, World” to “Heroku Scale”Havoc Pennington, Typesafe@havocp
  2. 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, 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 products, and utilization and selling to larger enterprise customers. Further information on potential factors that could affect the financial results of, 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., inc. assumes no obligation and does not intend to update these forward-looking statements.
  3. 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. 4. This TalkThis 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. 5. A SamplerNo way to teach you Scala in 45 minutes.But you might be inspired to learn more based on what you see.
  6. 6. Getting to Know YouHow many of you are already familiar with – Scala – Functional programming – Heroku=> Any specific topics you hope to hear about in this talk?
  7. 7. Scala: Who Cares?History and Motivation
  8. 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. 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. 10. 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 ScalaApproved for general productionuse
  11. 11. Community TractionOpen source ecosystem with• Hundreds of contributors• 20 books• 40+ active user groups• Plenty of github/stackoverflow activity
  12. 12. RedMonk GitHub / StackOverflow AnalysisStackOverflow Popularity GitHub Popularity
  13. 13. Why Scala?Youve seen that Scala has a vibrant community and hockey-stick growth curve. Heres what I hope youll 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
  14. 14. Less To Type, So What? What matters is reading, rather than writing.
  15. 15. Less CodeWhen 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 programcomprehension, 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.
  16. 16. Lets See Some Scala
  17. 17. Hello.scala object Hello extends App { println("Hello, World") }
  18. 18. 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!
  19. 19. 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
  20. 20. Something More InvolvedA simple “data” class (bean?) to store a Person with “name” and “age” fields.
  21. 21. A Class ... public class Person { public final String name; public final int age; Person(String name, int age) {... in Java: = name; this.age = age; } } case class Person(name: String, age: Int)... in Scala:
  22. 22. Dont Repeat Yourself? public class Person { public final String name; public final int age; Person(String name, int age) { = 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.
  23. 23. 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
  24. 24. 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)
  25. 25. 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)
  26. 26. 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
  27. 27. Less Is More Greatly Reducing Useless Noise = Much More Maintainable Code
  28. 28. Touring a Few More Tricks  Type Inference  Traits  Flexible Method Call Syntax  Implicits  Java interoperability
  29. 29. Type Inference • Omit explicit types when its obvious // type String is inferred val s = “hello” // but spell it out if you want to val s2: String = “hello”
  30. 30. 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 dont matter) trait Quacker { def quack = println("Quack") } trait Barker { def bark = println("Woof") } class DuckDog extends Quacker with Barker
  31. 31. Flexible Method Call Syntax • To read Scala code you need to know: the dot and the parens can be left out when theres 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
  32. 32. 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, its just that Scala doesnt 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)
  33. 33. Implicit ConversionFor 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.
  34. 34. Implicit Conversion to Find a MethodIf a method doesnt exist, maybe a conversion can make it exist. Safe alternative to “monkey patching” (as in Ruby, JavaScript).// Silly exampleclass Quacker { def quack = println("quack")}implicit def stringToQuacker(s: String): Quacker = new Quacker"hello".quack
  35. 35. Practical Migration and Interoperability• Scala differs from Java only on the Java: import; source code level Import;• Once you have a .jar or .war, it just URL u = new URL(“”); 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 and Scala files val u = new URL(“”) val s = u.openStream()• Deploy Scala to any cloud or s.close() container that supports Java
  36. 36. Scala Has a Java CoreWhile 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 typesYou can implement the same API in Scala that you would in Java; all the same once its compiled to class files.Scala can be used as “concise Java” right away, short learning curve.
  37. 37. Two Questions
  38. 38. Immutable Data • Concise, readable code is one advantage of Scala • Another huge one: support for immutable data
  39. 39. 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
  40. 40. 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
  41. 41. 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
  42. 42. 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
  43. 43. 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
  44. 44. 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)
  45. 45. Enabling Composition public static List<Integer> addTwoToAll(List<Integer> items) { return addOneToAll(addOneToAll(items)); } (Composition is great for HTTP request handlers, by the way.)
  46. 46. Automatic Thread CorrectnessThe official Oracle tutorial on concurrency recommends immutable data, but Java APIs dont support it, and doing it properly in Java means lots of ceremony.In Scala its the default, as it should be.Processors have more cores every day.
  47. 47. Switch to Immutable Style!Even if you dont switch to Scala, this will improve your Java.Reserve mutability for hotspots revealed by profiling.
  48. 48. 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
  49. 49. Recommended: Read the Book• Very clearly-written, by Scalas creator and designer• Highly recommended to start here
  50. 50. Two Questions
  51. 51. Herokus Scala SupportWe have to solve three problems to run on Heroku • Build the project • Run the project • Listen for HTTP requests
  52. 52. A Build Pack for SBT • SBT = Simple Build Tool • • Build pack is mis-named; it supports Java projects built with SBT, and youd use another build pack for a Maven-built Scala project.
  53. 53. 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 •
  54. 54. Listening on HTTP • Any HTTP server for the JVM is fine • You need to pass it the PORT environment variable provided by Heroku • Well 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)
  55. 55. Putting it togetherFollow along at: exampleLess than 200 lines to a deployed, scalable web server including build configuration and Netty boilerplate (around 100 lines is Netty stuff)
  56. 56. • Follow us @Typesafe on Twitter, or• Read Programming in Scala by Martin Odersky• Try writing a discrete module or two in Scala