Why Scala?
- Fast
- Concise
- Type Safe
- Type Inference
- Functional
- Pure Object Oriented
Why Scala?
- Extensible
- Built on JVM
- Pattern Matching
- Lazy Evaluation
- Stream
- Easy Parallelism
How Fast?
C > Java > Scala >> PHP, Ruby, Python
Java
public class Person {
private final String firstName;
private final String lastName;
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
Scala
case class Person(firstName: String, lastName: String)
Functional Combinators
- map
- foreach
- filter
- zip
- partition
Functional Combinators
- find
- drop and dropWhile
- foldRight and foldLeft
- flatten
- flatMap
Functional Combinators
val list = (1 to 10).toList
//> list : List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
list.filter(5 <)
//> res0: List[Int] = List(6, 7, 8, 9, 10)
list.fold(0)((a, n) => a + n)
list.fold(0)(_ + _)
//> res1: Int = 55
list.map(x => x * x)
//> res2: List[Int] = List(1, 4, 9, 16, 25, 36, 49, 64, 81, 100)
Curring
def power(x: Double)(y: Int) = Math.pow(x, y)
//> power: (x: Double)(y: Int)Double
list.map(power(2))
//> res3: List[Double] = List(2.0, 4.0, 8.0, 16.0, 32.0, 64.0,
//| 128.0, 256.0, 512.0, 1024.0)
def square = power(2) _
//> square: => Int => Double
list.map(square)
Tail Recursion
def factorial(n: Int): Int = {
if (n == 0)
1
else
n * factorial(n - 1)
}
Tail Recursion
def factorial(n: Int): Int = if (n == 0) 1 else n * factorial(n - 1)
factorial(3)
// if (3 == 0) 1 else 3 * factorial(3 - 1)
// 3 * factorial(2)
// 3 * (2 * factorial(1))
// 3 * (2 * (1 * factorial(0)))
// 3 * (2 * (1 * 1))
// 6
Tail Recursion
@tailrec
def factorial(i: Int): Int = {
def fact(i: Int, accumulator: Int): Int = {
if (i <= 1)
accumulator
else
fact(i - 1, i * accumulator)
}
fact(i, 1)
}
Trait
class Animal {}
trait Walking { def walk = println("walking...") }
trait Swimming { def swim = println("swimming...") }
trait Flying { def fly = println("flying...") }
class Duck extends Animal with Walking with Swimming {}
d.walk
//> walking...
d.swim
//> swimming...
Trait
val superDuck = new Duck with Flying
superDuck.walk
//> walking...
superDuck.swim
//> swimming...
superDuck.fly
//> flying...
Extensible
implicit class IntWithTimes(i: Int) {
def times(f: => Unit) = (1 to i) foreach (x => f)
}
5 times println("Welcome to the Scala worksheet")
// 5 days ago
// 5 days before
// 10 years back
Pattern Matching
abstract class Expr
case class Number(n: Int) extends Expr
case class Sum(e1: Expr, e2: Expr) extends Expr
def eval(e: Expr): Int = e match {
case Number(n) => n
case Sum(l, r) => eval(l) + eval(r)
}
eval(Sum(Sum(Number(1), Number(2)), Number(3)))
//> res3: Int = 6
Lazy Evaluation
def expr = {
val x = { print("x"); 1 }
lazy val y = { print("y"); 2 }
def z = { print("z"); 3 }
z+y+x+z+y+x
}
expr
Stream
def from(n: Int): Stream[Int] = n #:: from(n+1)
Stream
def from(n: Int): Stream[Int] = n #:: from(n+1)
def sieve(s: Stream[Int]): Stream[Int] =
s.head #:: sieve(s.tail filter (_ % s.head != 0))
val primes = sieve(from(2))
(primes take 100).toList
// Will return first 100 primes
Easy Parallelism
- Parallel Collections
- Thread-based Concurrency (derived from Java)
- Actor-based Concurrency (inspired by Erlang)
- Software Transactional Memory (basis of concurrency in Clojure)
Parallel Collections
val list = (1 to 1000000).toList.par
// Filter even numbers
list.filter(_ % 2 == 0)
// Filter odd numbers
list.filter(_ % 2 == 1)
Actor-based Concurrency
def heavyTask = {
Thread.sleep(1000L)
"Doing the HEAVY TASK"
}
val start = System.nanoTime
for (i <- 1 to 10) println(heavyTask)
val end = System.nanoTime
println("Time: " + (end - start)/1000000000.0)
//> Time: 10.033465
Actor-based Concurrency
import scala.actors._
import Actor._
// Copy heavyTask def from previous slide here
val caller = self
val start = System.nanoTime
for (i <- 1 to 10) actor { caller ! heavyTask }
for (i <- 1 to 10) receive { case msg => println(msg) }
val end = System.nanoTime
println("Time: " + (end - start)/1000000000.0)
//> Time: 2.077815
We have just scratched the surface
If I were to pick a language today other than Java, it would be Scala - James Gosling
Why?
- RAD
- Java & Scala
- Reactive
- Scalable
- Flexible
New Project
$ activator new my-project
New Project
Choose from these featured templates or enter a template name:
1) minimal-akka-java-seed
2) minimal-akka-scala-seed
3) minimal-java
4) minimal-scala
5) play-java
6) play-scala
(hit tab to see a list of all templates)
>
Thank You