Scala is becoming the language of choice for many development teams. This talk highlights how Scala excels in the world of multi-core processing and explores how it compares to Java 8.
Video Presentation: http://youtu.be/8vxTowBXJSg
16. class
Point
{
private
int
x;
private
int
y;
public
Point(int
x,
int
y)
{
setX(x);
setY(y);
}
public
int
getX()
{
return
x;
}
public
void
setX(int
x)
{
this.x
=
x;}
public
int
getY()
{
return
y;
}
public
void
setY(int
y)
{
this.y
=
y;}
public
boolean
equals(Object
other)
{
if
(other
instanceof
Point)
{
Point
otherPoint
=
(Point)
other;
return
otherPoint.getX()
==
getX()
&&
otherPoint.getY()
==
getY();
}
else
{
return
false;
}
}
public
int
hashCode()
{
return
(new
Integer[]
{getX(),
getY()}).hashCode();
}
}
19. Traits
abstract class Animal {
def speak
}
trait FourLeggedAnimal {
def walk
def run
}
trait WaggingTail {
val tailLength: Int
def startTail { println("tail started") }
def stopTail { println("tail stopped") }
}
class Dog extends Animal with WaggingTail with FourLeggedAnimal {
def speak { println("Dog says 'woof'") }
def walk { println("Dog is walking") }
def run { println("Dog is running") }
}
20. Immutable Classes
public final class Point {
private final int x;
private final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() { return x; }
public int getY() { return y; }
}
Java
Scala case class Point(x: Int, y: Int)
32. Pattern Matching
def
parseArgument(arg
:
String,
value:
Any)
=
(arg,
value)
match
{
case
("-‐h"
|
"-‐-‐help",
null)
=>
displayHelp()
case
("-‐l",
lang)
=>
setLanguageTo(lang)
case
bad
=>
badArgument(bad)
}
33. Pattern Matching
def
parseArgument(arg
:
String,
value:
Any)
=
(arg,
value)
match
{
case
("-‐h"
|
"-‐-‐help",
null)
=>
displayHelp()
case
("-‐l",
lang)
=>
setLanguageTo(lang)
case
("-‐o"
|
"-‐-‐optim",
n
:
Int)
if
((0
<
n)
&&
(n
<=
5))
=>
setOptimizationLevelTo(n)
case
("-‐o"
|
"-‐-‐optim",
badLevel)
=>
badOptimizationLevel(badLevel)
case
bad
=>
badArgument(bad)
}
34. Patterns
// constant patterns
case 0 => "zero”
case true => "true”
case "hello" => "you said 'hello'”
case Nil => "an empty List"
// sequence patterns
case List(0, _, _) => "a three-element list with 0 as the first element”
case List(1, _*) => "a list beginning with 1, having any number of elements”
case Vector(1, _*) => "a vector starting with 1, having any number of elements”
// constructor patterns
case Person(first, "Alexander") => s"found an Alexander, first name = $first”
case Dog("Suka") => "found a dog named Suka”
35. I call it my billion-dollar mistake. It
was the invention of the null
reference in 1965.
“
”- Tony Hoare
36. Option
def toInt(in: String): Option[Int] = {
try {
Some(Integer.parseInt(in.trim))
} catch {
case ex: NumberFormatException => None
}
}
toInt(someString) match {
case Some(i) => println(i)
case None => println("That didn't work.")
}
37. Collections with Option
val bag = List("1", "2", "foo", "3", "bar")
bag.map(toInt) // List[Option[Int]] = List(Some(1), Some(2), None, Some(3), None)
bag.flatMap(toInt) // List(1, 2, 3)
38. Summary
• Parallel Processing made easier
• Developer Productivity
• Java interoperable
• Still relevant in a Java 8 world