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.

Why Scala?

129 views

Published on

Motivation for using functional programming and an introduction to Scala.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Why Scala?

  1. 1. Why Scala? Awesome Scala Features Yevgen Pikus
  2. 2. Topic • Scala Introduction • Awesome Scala Feature • Learning & Using Scala
  3. 3. Why Scala?
  4. 4. "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong.“ – Richard Buckminster Fuller
  5. 5. Variance public static Double mean(List<Double> l) { if (l.size() <= 0) throw new ArithmeticException(); Double sum = 0.0; for (Double x : l) { sum += x; } return sum / l.size(); } public static Double variance(List<Double> l) { try { Double m = mean(l); List<Double> tmp = new ArrayList<>(l.size()); for (Double x : l) { tmp.add(Math.pow(x - m, 2)); } return mean(tmp); } catch (ArithmeticException e) { e.printStackTrace(); return -1.0; } }
  6. 6. Variance public static Double mean(List<Double> l) { if (l.size() <= 0) throw new ArithmeticException(); Double sum = 0.0; for (Double x : l) { sum += x; } return sum / l.size(); } public static Double variance(List<Double> l) { try { Double m = mean(l); List<Double> tmp = new ArrayList<>(l.size()); for (Double x : l) { tmp.add(Math.pow(x - m, 2)); } return mean(tmp); } catch (ArithmeticException e) { e.printStackTrace(); return -1.0; } } BOILERPLATE??
  7. 7. def mean(xs: Seq[Double]): Option[Double] = if(xs.nonEmpty) Some(xs.sum / xs.size) else None def variance(xs: Seq[Double]): Option[Double] = mean(xs).flatMap(m => mean(xs.map(x => math.pow(x - m, 2))))
  8. 8. def mean(xs: Seq[Double]): Option[Double] = if(xs.nonEmpty) Some(xs.sum / xs.size) else None def variance(xs: Seq[Double]): Option[Double] = mean(xs).flatMap(m => mean(xs.map(x => math.pow(x - m, 2)))) NO BOILERPLATE!!
  9. 9. def mean(xs: Seq[Double]): Option[Double] = if(xs.nonEmpty) Some(xs.sum / xs.size) else None type MeanFunc = Seq[Double] => Option[Double] def variance(xs: Seq[Double], mean: MeanFunc): Option[Double] = mean(xs).flatMap(m => mean(xs.map(x => math.pow(x - m, 2)))) NO BOILERPLATE!!
  10. 10. Scala Introduction • First version in 2003 • Martin Odersky • JVM language • OO, FP, Macros & [T]
  11. 11. Awesome Scala Feature Detour
  12. 12. case classes case class Superhero(alias: String, power: String, name: String = "Unknown") val superman = Superhero("Superman", "Flight", "Clark Kent") val ironman = Superhero(alias = "Iron Man", power = "Intellect“) val flash = Superhero("The Flash", "Speed") val makkari = flash.copy(alias = "MAKKARI")
  13. 13. type inference val alias = "Iron Man" // alias: String = "Iron Man" val age = 42 //age: Int = 42 val ironman = Superhero(alias = "Iron Man", power = "Intellect") //ironMan: Superhero = Superhero("Iron Man", "Intellect") val superheros = List(flash, ironman) //superheros: List[Superhero] = List(flash, ironman)
  14. 14. pattern matching x match { case hero: Superhero => print(hero.alias) case Villain(alias, power, evilness) => print(s"Evilness: $evilness") case _ => print("Unknown") }
  15. 15. List(flash, ironman).map(sh => sh.alias) //List("The Flash", "Iron Man") val isVillain: Person => Boolean = person => person.evilness > 0 def createHero(p: Person, isVillain: Person => Boolean): Hero = if(isVillain(p)) Villain(p.alias, p.power, p.evilness) else Superhero(p.alias, p.power) higher-order functions
  16. 16. collections val superheros = List(flash, ironman) superheros.map(sh => sh.alias) //List("The Flash", "Iron Man") superheros.filter(_.power == "Intellect") //List(Superhero("Iron Man", "Intellect", "Tony Stark") List(List("Iron Man"), List("Batman", "Joker")).flatMap(x => x) //List("Iron Man", "Batman", "Joker")
  17. 17. for comprehensions val heros = List(flash, ironman) val l = for(h <- heros) yield h.alias //map //List("The Flash", "Iron Man") val l = for(h <- heros if h.power == "Intellect") yield h //filter //List(Superhero("Iron Man", "Intellect", "Tony Stark") val herosGroups = List(List("Iron Man"), List("Batman", "Joker")) val l = for(hs <- herosGroups; h <- hs) yield h //flatMap //List("Iron Man", "Batman", "Joker")
  18. 18. Learning & Using Scala
  19. 19. Learning Scala OO FP
  20. 20. Flexibility //multiple parameters def f(i:Int, j: Int): String = "$i $j" //f(1,2) = "1 2" //curring def f(i:Int)(j: Int): String = "$i $j" //f(1)(2) = "1 2" //anonymous function assigned to a value val f: (Int, Int) => String = (i: Int, j: Int) => "$i $j" //f(1,2) //type inference val f = (i: Int, j: Int) => "$i $j" //f(1,2) = "1 2" //currying val f: Int => Int => String = (i: Int) => (j: Int) => "$i $j" //f(1)(2) = "1 2" //type inference val f: Int => Int => String = i => j => "$i $j" //f(1)(2) = "1 2"
  21. 21. @SuppressWarnings({"unchecked", "rawtypes"}) //copied from http://annotatiomania.com @Deprecated @OneToMany(@HowManyDBADoYouNeedToChangeALightBulb) @OneToManyMore @AnyOne @AnyBody @YouDoNotTalkAboutOneToMany // Fightclub, LOL @TweakThisWithThat( tweak = { @TweakID(name = "id", preferredValue = 1839), @TweakID(name = "test", preferredValue = 839), @TweakID(name = "test.old", preferredValue = 34), }, inCaseOf = { @ConditionalXMLFiltering(run = 5), } ) @ManyToMany @Many @AnnotationsTotallyRock @DeclarativeProgrammingRules @NoMoreExplicitAlgorithms @Fetch @FetchMany @FetchWithDiscriminator(name = "no_name") @SeveralAndThenNothing @MaybeThisDoesSomething @JoinTable(joinColumns = { @JoinColumn(name = "customer_id", referencedColumnName = "id") }) @DoesThisEvenMeanAnything @DoesAnyoneEvenReadThis @PrefetchJoinWithDiscriminator @JustTrollingYouKnow @LOL @IfJoiningAvoidHashJoins @ButUseHashJoinsWhenMoreThan(records = 1000) @XmlDataTransformable @SpringPrefechAdapter private Collection employees;
  22. 22. Scalastyle Plugin • Scala style checker plugin • Supports custom rules • Maven • SBT • Intellij • etc.
  23. 23. Language Features import scala.language.implicitConversions import scala.language.dynamics import scala.language.postfixOps import scala.language.reflectiveCalls import scala.language.higherKinds import scala.language.existentials import scala.language.macros
  24. 24. Scala Ecosystem • IDEs – Intellij, Eclipse, Atom etc. • Build – SBT, Maven, Gradle • Frameworks and libraries – Akka, Play, Spark, Cats, Scalaz, shapeless etc.
  25. 25. Scala Compiler
  26. 26. Take Home Points Programming should make FUN
  27. 27. Why Scala? Awesome Scala Features Yevgen Pikus

×