Scala - The University of Edinburgh€¦ · Why should you care about JP Morgan? JP Morgan Tech at...
Transcript of Scala - The University of Edinburgh€¦ · Why should you care about JP Morgan? JP Morgan Tech at...
Scala(at JP Morgan)
Why should you listen to me?
Hello
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 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)
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()}
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) }}
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]
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])
Learning more about Scala
What’s driving Scala adoption?
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)
Case study: TPS
How did it go?
TPS port to Spark
Corporate vs Startup
Think like an owner
Thanks!