Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Demystifying Scala

635 views

Published on

It’s functional, it’s object oriented, it’s everything you never knew you wanted and more! Scala has been growing in popularity over the last 15 years and has now taken off in a variety of applications ranging from data science to distributed systems to messaging and the web. You’ve been curious about this language, and now is your chance to learn more.

Published in: Engineering
  • Be the first to comment

Demystifying Scala

  1. 1. Engineering Team Lead, Sharethrough @kelleyrobinson Demystifying Scala Kelley Robinson
  2. 2. Demystifying Scala
  3. 3. 📍Lausanne
  4. 4. Engineering Team Lead, Sharethrough @kelleyrobinson Demystifying Scala Kelley Robinson
  5. 5. Engineering Team Lead, Sharethrough @kelleyrobinson Kelley Robinson
  6. 6. DEMYSTIFYING SCALA @KELLEYROBINSON Why Scala How to get started Introduction to Scala
  7. 7. DEMYSTIFYING SCALA @KELLEYROBINSON Why Scala How to get started Introduction to Scala
  8. 8. Ina Garten a.k.a. The Barefoot Contessa
  9. 9. DEMYSTIFYING SCALA @KELLEYROBINSON Syntax Intro
  10. 10. DEMYSTIFYING SCALA @KELLEYROBINSON “How easy is that?”.isEmpty // Boolean = false val chefs = List("Ina", "Martha", "Giada") // List[String] = List(Ina, Martha, Giada) chefs(0) // String = Ina
  11. 11. DEMYSTIFYING SCALA @KELLEYROBINSON
  12. 12. DEMYSTIFYING SCALA @KELLEYROBINSON def isStoreBoughtFine(yourName: String): Boolean = { 
 val areYouAnyoneButIna = yourName != "Ina Garten"
 
 areYouAnyoneButIna } Many ways to write a function...
  13. 13. DEMYSTIFYING SCALA @KELLEYROBINSON def isStoreBoughtFine(yourName: String): Boolean = { 
 yourName != "Ina Garten" } Many ways to write a function...
  14. 14. DEMYSTIFYING SCALA @KELLEYROBINSON def isStoreBoughtFine(yourName: String): Boolean = 
 yourName != "Ina Garten" Many ways to write a function...
  15. 15. DEMYSTIFYING SCALA @KELLEYROBINSON def isStoreBoughtFine(yourName: String) = 
 yourName != "Ina Garten" Many ways to write a function...
  16. 16. DEMYSTIFYING SCALA @KELLEYROBINSON def isStoreBoughtFine(yourName: String): Boolean = 
 "Ina Garten” // error: type mismatch; // found : String // required: Boolean Types provide safety
  17. 17. DEMYSTIFYING SCALA @KELLEYROBINSON def isStoreBoughtFine(yourName: String): Boolean // ok isStoreBoughtFine(“Kelley") // oh no! isStoreBoughtFine(42) // error: type mismatch; // found : Int(42) // required: String Calling a function
  18. 18. DEMYSTIFYING SCALA @KELLEYROBINSON val barefootContessa = "Ina Garten” 
 val barefootContessa = "Kelley Robinson"
 // <console> error: barefootContessa is already defined val yasKween = barefootContessa.toUpperCase println(barefootContessa) // Ina Garten Data is immutable by default
  19. 19. DEMYSTIFYING SCALA @KELLEYROBINSON Scala in a Nutshell
  20. 20. Object-Oriented Meets Functional Have the best of both worlds. @KELLEYROBINSONDEMYSTIFYING SCALA
  21. 21. DEMYSTIFYING SCALA @KELLEYROBINSON Functional Programming
  22. 22. DEMYSTIFYING SCALA @KELLEYROBINSON FP: Everything is an expression • think in values, not side-effects • statement is an expression with Unit return type
  23. 23. FP: Avoid Side Effects Changes outside of function scope Modifies state Not predictable DEMYSTIFYING SCALA @KELLEYROBINSON
  24. 24. FP: Referential transparency An expression can be replaced by its value without changing the program behavior DEMYSTIFYING SCALA @KELLEYROBINSON
  25. 25. FP: Immutable data Avoid modifying state DEMYSTIFYING SCALA @KELLEYROBINSON
  26. 26. FP: Expressions The programmer benefits Understanding Predictability Unit testing DEMYSTIFYING SCALA @KELLEYROBINSON
  27. 27. SEAMLESS JAVA INTEROP TYPE INFERENCE CONCURRENCY & DISTRIBUTION TRAITS PATTERN MATCHING HIGHER-ORDER FUNCTIONS Scala in a Nutshell DEMYSTIFYING SCALA @KELLEYROBINSON
  28. 28. Seamless Java Interop All the benefits of the Java Virtual Machine (JVM) @KELLEYROBINSONDEMYSTIFYING SCALA
  29. 29. Type Inference Let the type system work for you DEMYSTIFYING SCALA @KELLEYROBINSON
  30. 30. Traits Flexible interfaces @KELLEYROBINSONDEMYSTIFYING SCALA
  31. 31. DEMYSTIFYING SCALA @KELLEYROBINSON trait CelebrityChef
 
 trait FoodNetworkStar extends CelebrityChef {
 val showName: String
 }
 
 trait BestSellingAuthor {
 val bookName: String
 }
 
 object InaGarten extends FoodNetworkStar
 with BestSellingAuthor {
 val bookName: String = "Cooking for Jeffrey"
 val showName: String = "Barefoot Contessa"
 }
  32. 32. DEMYSTIFYING SCALA @KELLEYROBINSON trait CelebrityChef
 
 trait FoodNetworkStar extends CelebrityChef
 object InaGarten extends FoodNetworkStar with BestSellingAuthor object RoyChoi extends CelebrityChef def learnToCook(instructor: CelebrityChef) = { ... }
  33. 33. Pattern Matching “Switch” on steroids DEMYSTIFYING SCALA @KELLEYROBINSON
  34. 34. DEMYSTIFYING SCALA @KELLEYROBINSON def matchTest(x: Int): String = x match {
 case 1 => "one"
 case 2 => "two"
 case _ => "many"
 } println(matchTest(3)) // “many” Pattern Matching
  35. 35. DEMYSTIFYING SCALA @KELLEYROBINSON sealed trait Ingredient
 case object Produce extends Ingredient
 case object Meat extends Ingredient
 case object DryGood extends Ingredient
 
 def storageLocation(i: Ingredient): String = i match {
 case Produce => "Refrigerator"
 case Meat => "Freezer"
 case DryGood => "Pantry"
 } Pattern Matching
  36. 36. Higher-order Functions Be expressive and succinct DEMYSTIFYING SCALA @KELLEYROBINSON
  37. 37. DEMYSTIFYING SCALA @KELLEYROBINSON trait Ingredient { val shelfLife: Int } Scala val ingredients: Array[Ingredient]
 val (eatNow, eatLater) = ingredients.partition(i => i.shelfLife < 7) Java List<Ingredient> ingredients;
 List<Ingredient> eatNow = new ArrayList<Ingredient>(ingredients.size());
 List<Ingredient> eatLater = new ArrayList<Ingredient>(ingredients.size());
 for (Ingredient ingredient : ingredient) {
 if (ingredient.getShelfLife() < 7)
 eatNow.add(ingredient);
 else
 eatLater.add(ingredient);
 }
  38. 38. DEMYSTIFYING SCALA @KELLEYROBINSON trait Ingredient { val shelfLife: Int } Scala val ingredients: Array[Ingredient]
 val (eatNow, eatLater) = ingredients.partition(i => i.shelfLife < 7) Partition takes a function as its argument def partition(p: Int => Boolean)
  39. 39. DEMYSTIFYING SCALA @KELLEYROBINSON These are equivalent partition(i => i.shelfLife < 7) partition(_.shelfLife < 7) underscore is a convenience operator
  40. 40. DEMYSTIFYING SCALA @KELLEYROBINSON Why Scala How to get started Introduction to Scala
  41. 41. DEMYSTIFYING SCALA http://downloads.typesafe.com/website/casestudies/Sharethrough-Case-Study-v1.0.pdf @KELLEYROBINSON
  42. 42. DEMYSTIFYING SCALA @KELLEYROBINSON “Sharethrough developers were quickly productive with Scala, which can be attributed to their familiarity with Java, Scala’s expressiveness and lack of boilerplate code.”
  43. 43. DEMYSTIFYING SCALA @KELLEYROBINSON
  44. 44. DEMYSTIFYING SCALA https://twitter.com/kelleyrobinson/status/820426791433039872 @KELLEYROBINSON
  45. 45. DEMYSTIFYING SCALA @KELLEYROBINSON
  46. 46. DEMYSTIFYING SCALA @KELLEYROBINSON
  47. 47. DEMYSTIFYING SCALA @KELLEYROBINSON https://meta.plasm.us/posts/2015/07/11/roll-your-own-scala/
  48. 48. Flexibility requires discipline DEMYSTIFYING SCALA @KELLEYROBINSON
  49. 49. DEMYSTIFYING SCALA https://twitter.com/chanian/status/462103684281671681 @KELLEYROBINSON
  50. 50. Sad truths about Scala DEMYSTIFYING SCALA @KELLEYROBINSON
  51. 51. DEMYSTIFYING SCALA @KELLEYROBINSON Scala Functional Spectrum Java Haskell Sad truths about Scala
  52. 52. DEMYSTIFYING SCALA @KELLEYROBINSON Sad truths about Scala
  53. 53. DEMYSTIFYING SCALA Sad truths about Scala @KELLEYROBINSON // source code yikes object Task {
 implicit val taskInstance: Nondeterminism[Task] with Catchable[Task] with MonadError[({type λ[α,β] = Task[β]})#λ,Throwable] = new Nondeterminism[Task] with Catchable[Task] with MonadError[({type λ[α,β] = Task[β]})#λ,Throwable] { ... } }
  54. 54. DEMYSTIFYING SCALA @KELLEYROBINSON These are equivalent partition(i => i.shelfLife < 7) partition(_.shelfLife < 7) underscore is a convenience operator
  55. 55. DEMYSTIFYING SCALA @KELLEYROBINSON Symbolic operators // this is valid code def @*%--@&#&(foo: String): String = foo // exists for this purpose def +(s1: String, s2: String): String = s1.concat(s2)
  56. 56. “Modern” Functional Programming is cluttered with jargon Sad truths about Scala DEMYSTIFYING SCALA @KELLEYROBINSON
  57. 57. Scala is still pretty awesome DEMYSTIFYING SCALA @KELLEYROBINSON
  58. 58. Scala is still pretty awesome • Powerful • Flexible • Creative DEMYSTIFYING SCALA @KELLEYROBINSON
  59. 59. DEMYSTIFYING SCALA @KELLEYROBINSON Why Scala How to get started Introduction to Scala
  60. 60. Scala REPL Core Language https://www.scala-lang.org/download/install.html @KELLEYROBINSONDEMYSTIFYING SCALA
  61. 61. DEMYSTIFYING SCALA @KELLEYROBINSON Functional Programming Principles in Scala Scala Center + Coursera https://www.coursera.org/learn/progfun1
  62. 62. Scala Exercises 47 Degrees https://www.scala-exercises.org/scala_tutorial/terms_and_types @KELLEYROBINSONDEMYSTIFYING SCALA
  63. 63. ScalaBridge Introductory programming workshops for underrepresented groups • Curriculum is free and open source • Teaching is a great way to learn https://scalabridge.gitbooks.io/curriculum/content/setup/setup.html $ @KELLEYROBINSONDEMYSTIFYING SCALA
  64. 64. • Don’t be intimidated • Have fun • Be disciplined @KELLEYROBINSONDEMYSTIFYING SCALA
  65. 65. Thank You! @kelleyrobinson hello@krobinson.me

×