scala - the university of edinburgh€¦ · why should you care about jp morgan? jp morgan tech at...

24
Scala (at JP Morgan)

Upload: others

Post on 06-Jul-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Scala - The University of Edinburgh€¦ · Why should you care about JP Morgan? JP Morgan Tech at JP Morgan FP & Scala. Imperative vs Functional. Scala. Why Scala not Haskell? Example:

Scala(at JP Morgan)

Page 2: Scala - The University of Edinburgh€¦ · Why should you care about JP Morgan? JP Morgan Tech at JP Morgan FP & Scala. Imperative vs Functional. Scala. Why Scala not Haskell? Example:

Why should you listen to me?

Page 3: Scala - The University of Edinburgh€¦ · Why should you care about JP Morgan? JP Morgan Tech at JP Morgan FP & Scala. Imperative vs Functional. Scala. Why Scala not Haskell? Example:

Hello

Page 4: Scala - The University of Edinburgh€¦ · Why should you care about JP Morgan? JP Morgan Tech at JP Morgan FP & Scala. Imperative vs Functional. Scala. Why Scala not Haskell? Example:

Why should you care about JP Morgan?

Page 5: Scala - The University of Edinburgh€¦ · Why should you care about JP Morgan? JP Morgan Tech at JP Morgan FP & Scala. Imperative vs Functional. Scala. Why Scala not Haskell? Example:

JP Morgan

○○○○

Page 6: Scala - The University of Edinburgh€¦ · Why should you care about JP Morgan? JP Morgan Tech at JP Morgan FP & Scala. Imperative vs Functional. Scala. Why Scala not Haskell? Example:

Tech at JP Morgan

○○○○

Page 7: Scala - The University of Edinburgh€¦ · Why should you care about JP Morgan? JP Morgan Tech at JP Morgan FP & Scala. Imperative vs Functional. Scala. Why Scala not Haskell? Example:

FP & Scala

Page 8: Scala - The University of Edinburgh€¦ · Why should you care about JP Morgan? JP Morgan Tech at JP Morgan FP & Scala. Imperative vs Functional. Scala. Why Scala not Haskell? Example:

Imperative vs Functional

Page 9: Scala - The University of Edinburgh€¦ · Why should you care about JP Morgan? JP Morgan Tech at JP Morgan FP & Scala. Imperative vs Functional. Scala. Why Scala not Haskell? Example:

Scala

Page 10: Scala - The University of Edinburgh€¦ · Why should you care about JP Morgan? JP Morgan Tech at JP Morgan FP & Scala. Imperative vs Functional. Scala. Why Scala not Haskell? Example:

Why Scala not Haskell?

Page 11: Scala - The University of Edinburgh€¦ · Why should you care about JP Morgan? JP Morgan Tech at JP Morgan FP & Scala. Imperative vs Functional. Scala. Why Scala not Haskell? Example:

Example: Case classes

public class Person {private final long id;private String name;

public Person(long id, String name) {this.id = id;this.name = name;

}

public long getId() {return this.id;

}public String getName() {

return this.name;}public String setName(String name) {

this.name = name;}// FIXME: Add sensible equals(), hashCode()// TODO: Add toString(), copy()

}

case class Person(id: Long, var name: String)

Page 12: Scala - The University of Edinburgh€¦ · Why should you care about JP Morgan? JP Morgan Tech at JP Morgan FP & Scala. Imperative vs Functional. Scala. Why Scala not Haskell? Example:

Example: Lazy initialisation

public class App {

private volatile ConnectionPool pool;

public ConnectionPool getConnectionPool() {

ConnectionPool cp = pool;

if (cp == null) {

synchronized(this) {

if (cp == null) {

cp = pool = new ConnectionPool();

}

}

}

return cp;

}

}

class App { lazy val pool = new ConnectionPool()}

Page 13: Scala - The University of Edinburgh€¦ · Why should you care about JP Morgan? JP Morgan Tech at JP Morgan FP & Scala. Imperative vs Functional. Scala. Why Scala not Haskell? Example:

Example: Mixins and call by value

public class Logger { public boolean quiet = false; public String log(String msg) { if (!quiet) System.out.println(msg); }}

public class Person { static Logger LOG = new Logger();

/*... 30 lines of boilerplate ...*/

public void email(String msg) { if (!LOG.quiet) LOG.log(“Emailing “ + name); }}

trait Logger { var quiet = false def log(msg: => String) = if (!quiet) println(msg);}

case class Person(id: Long, var name: String) extends Logger { def email(msg: String) = { log(“Emailing “ + name) }}

Page 14: Scala - The University of Edinburgh€¦ · Why should you care about JP Morgan? JP Morgan Tech at JP Morgan FP & Scala. Imperative vs Functional. Scala. Why Scala not Haskell? Example:

Example: Algebraic types

data List a = Nil | Cons a (List a)map :: (a->b) -> (List a) -> (List b)map f Nil = Nilmap f (Cons x xs) = Cons (f x) (map f xs)

sealed trait List[+A] { def map[B](f: A => B): List[B] = this match { case Nil => Nil case Cons(head, tail) => Cons(f(head), tail.map(f)) }}

case object Nil extends List[Nothing]case class Cons[A](head: A, tail: List[A]) extends List[A]

Page 15: Scala - The University of Edinburgh€¦ · Why should you care about JP Morgan? JP Morgan Tech at JP Morgan FP & Scala. Imperative vs Functional. Scala. Why Scala not Haskell? Example:

It’s not all good

List(1, 2, 3).toSet()

override def ++[B >: A, That](that: GenTraversableOnce[B])(implicit bf: CanBuildFrom[List[A], B, That])

Page 17: Scala - The University of Edinburgh€¦ · Why should you care about JP Morgan? JP Morgan Tech at JP Morgan FP & Scala. Imperative vs Functional. Scala. Why Scala not Haskell? Example:

What’s driving Scala adoption?

Page 18: Scala - The University of Edinburgh€¦ · Why should you care about JP Morgan? JP Morgan Tech at JP Morgan FP & Scala. Imperative vs Functional. Scala. Why Scala not Haskell? Example:

Spark

val data = sc.load(“hdfs://namenode/input.txt”)val top20 = data.map(line => line.split(“ “)) .map(word => (word, 1)) .reduceByKey((count1, count2) => count1 + count2) .top(10)(Ordering.by(_._2)

Page 19: Scala - The University of Edinburgh€¦ · Why should you care about JP Morgan? JP Morgan Tech at JP Morgan FP & Scala. Imperative vs Functional. Scala. Why Scala not Haskell? Example:

Case study: TPS

Page 20: Scala - The University of Edinburgh€¦ · Why should you care about JP Morgan? JP Morgan Tech at JP Morgan FP & Scala. Imperative vs Functional. Scala. Why Scala not Haskell? Example:

How did it go?

Page 21: Scala - The University of Edinburgh€¦ · Why should you care about JP Morgan? JP Morgan Tech at JP Morgan FP & Scala. Imperative vs Functional. Scala. Why Scala not Haskell? Example:

TPS port to Spark

Page 22: Scala - The University of Edinburgh€¦ · Why should you care about JP Morgan? JP Morgan Tech at JP Morgan FP & Scala. Imperative vs Functional. Scala. Why Scala not Haskell? Example:

Corporate vs Startup

Page 23: Scala - The University of Edinburgh€¦ · Why should you care about JP Morgan? JP Morgan Tech at JP Morgan FP & Scala. Imperative vs Functional. Scala. Why Scala not Haskell? Example:

Think like an owner

Page 24: Scala - The University of Edinburgh€¦ · Why should you care about JP Morgan? JP Morgan Tech at JP Morgan FP & Scala. Imperative vs Functional. Scala. Why Scala not Haskell? Example:

Thanks!