Why Scala?
-
Upload
yevgen-pikus -
Category
Technology
-
view
35 -
download
1
Transcript of Why Scala?
Why Scala?
Awesome Scala Features
Yevgen Pikus
Topic
• Scala Introduction
• Awesome Scala Feature
• Learning & Using Scala
Why Scala?
"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
Variancepublic 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;
}}
Variancepublic 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??
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))))
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!!
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!!
Scala Introduction
• First version in 2003
• Martin Odersky
• JVM language
• OO, FP, Macros & [T]
Awesome Scala FeatureDetour
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")
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)
pattern matching
x match {
case hero: Superhero => print(hero.alias)
case Villain(alias, power, evilness) => print(s"Evilness: $evilness")
case _ => print("Unknown")
}
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
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")
for comprehensionsval 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")
Learning
& Using
Scala
Learning Scala
OO FP
Flexibility//multiple parametersdef f(i:Int, j: Int): String = "$i $j" //f(1,2) = "1 2"//curringdef f(i:Int)(j: Int): String = "$i $j" //f(1)(2) = "1 2"//anonymous function assigned to a valueval f: (Int, Int) => String = (i: Int, j: Int) => "$i $j" //f(1,2)//type inferenceval f = (i: Int, j: Int) => "$i $j" //f(1,2) = "1 2"//curryingval f: Int => Int => String = (i: Int) => (j: Int) => "$i $j"//f(1)(2) = "1 2"//type inferenceval f: Int => Int => String = i => j => "$i $j" //f(1)(2) = "1 2"
@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 @SpringPrefechAdapterprivate Collection employees;
Scalastyle Plugin
• Scala style checker plugin
• Supports custom rules
• Maven
• SBT
• Intellij
• etc.
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
Scala Ecosystem
• IDEs
– Intellij, Eclipse, Atom etc.
• Build
– SBT, Maven, Gradle
• Frameworks and libraries
– Akka, Play, Spark, Cats, Scalaz, shapeless etc.
Scala Compiler
Take Home Points
Programming should make FUN
Why Scala?
Awesome Scala Features
Yevgen Pikus