Why Scala?

28
Why Scala? Awesome Scala Features Yevgen Pikus

Transcript of Why Scala?

Page 1: Why Scala?

Why Scala?

Awesome Scala Features

Yevgen Pikus

Page 2: Why Scala?

Topic

• Scala Introduction

• Awesome Scala Feature

• Learning & Using Scala

Page 3: Why Scala?

Why Scala?

Page 4: 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

Page 5: Why Scala?

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;

}}

Page 6: Why Scala?

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??

Page 7: Why Scala?
Page 8: Why Scala?

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))))

Page 9: Why Scala?

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!!

Page 10: Why Scala?

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!!

Page 11: Why Scala?

Scala Introduction

• First version in 2003

• Martin Odersky

• JVM language

• OO, FP, Macros & [T]

Page 12: Why Scala?

Awesome Scala FeatureDetour

Page 13: Why Scala?

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")

Page 14: Why Scala?

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)

Page 15: Why Scala?

pattern matching

x match {

case hero: Superhero => print(hero.alias)

case Villain(alias, power, evilness) => print(s"Evilness: $evilness")

case _ => print("Unknown")

}

Page 16: Why Scala?

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

Page 17: Why Scala?

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")

Page 18: Why Scala?

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")

Page 19: Why Scala?

Learning

& Using

Scala

Page 20: Why Scala?

Learning Scala

OO FP

Page 21: Why Scala?

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"

Page 22: Why Scala?

@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;

Page 23: Why Scala?

Scalastyle Plugin

• Scala style checker plugin

• Supports custom rules

• Maven

• SBT

• Intellij

• etc.

Page 24: Why Scala?

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

Page 25: Why Scala?

Scala Ecosystem

• IDEs

– Intellij, Eclipse, Atom etc.

• Build

– SBT, Maven, Gradle

• Frameworks and libraries

– Akka, Play, Spark, Cats, Scalaz, shapeless etc.

Page 26: Why Scala?

Scala Compiler

Page 27: Why Scala?

Take Home Points

Programming should make FUN

Page 28: Why Scala?

Why Scala?

Awesome Scala Features

Yevgen Pikus