3. Scala's History
Scala History
2.8.1
2.8.0
2.7.0
2.6.0
2.5.0
2.7.1 - 2.7.7
2.4.0
2.3.0
2.2.0
2.1.0
1.4.0.0
1.3.0.2
1.2.0.0
1.1.1.0
Jan 04 Jan 05 Jan 06 Jan 07 Jan 08 Jan 09 Jan 10
4. More Info on Scala
Home page: http://www.scala-lang.org/
Excellent community
scala-user@listes.epfl.ch
irc://chat.freenode.net/scala
http://scala.sygneca.com/ (Wiki)
7. Scala and the JVM
Compiles to 100% Java Bytecode
Generally works flawlessly with Java
Leverages JIT: performance ±5% of
Java
Full access to existing Java ecosystem
CLR (.Net) port in progress
8. About This Talk
Scala 2.8.0+
Significant changes to library
I'm still coming up to speed on
them
This is not a lecture
10. The Scala REPL
Read, Evaluate, Print, Loop
You need a JVM (1.6+ preferred) with
“java” in your path or JAVA_HOME set
Download the latest binaries from
http://www.scala-lang.org/downloads
Unpack wherever, go to bin subdirectory
Type “scala<enter>”
13. Three Very Common Keywords
val – defines an immutable
value or reference
var – defines a mutable value
def – defines a
function/method
14. Scala is Concerned with Mutability
Immutable data structures reduce
(but not eliminate) concurrency
issues
Combined with actors make a
powerful approach to parallel tasks
Strong library and language support
15. Scala is Strongly Typed...
val foo : Int = 12
var bar : String = "twelve"
def baz (in : Int) : Double =
in.toDouble
22. Scala is Closurific
def counter (name : String) = {
var i = 0;
{ () =>
i += 1
name + ":" + i
}
}
23. Scala is Concise...
public class Person {
private String name;
private int age;
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
} case class Person(name : String,
VS var age : Int)
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int hashCode() {...}
public String toString() {...}
public boolean equals() {...}
}
30. Letting You Get to the Real Work
// One line of magic...
implicit val orderPeople =
Ordering.by((_:Person).age)
// ...allows powerful constructs
val (oldest,youngest) =
(people.max,people.min)
38. Traits are Really Awesome
...But they can carry implementation
trait Comparable[T <: Comparable[T]] {
self : T =>
def < (that : T) : Boolean
def <= (that : T) = this < that ||
this == a
def > (that : T) = that < this
def >= (that : T) = that <= this
}
43. Scala Objects
“An object definition defines a single
object of a new class” (Scala Reference,
§5.4
object Friendly {
var publicInt = 0
def hi = println("Hi!")
}
49. Case Classes (and Objects)
case class Car (name : String,
gears : Int)
scala> val myCar = Car("Corolla", 4)
myCar: Car = Car(Corolla,4)
scala> myCar.name
res0: String = Corolla
50. Case Class Automation
Factory method (apply)
toString
hashCode
equals (and therefore ==)
Constructor params become vals,
can be turned into vars
51. Case Class Bonii : Copy
scala> val myCar = Car("Corolla", 4)
myCar: Car = Car(Corolla,4)
scala> val myOtherCar =
myCar.copy(name = "Forester")
myOtherCar: Car = Car(Forester,4)
56. For Comprehensions
for (first <- people;
second <- people
if first != second)
yield List(first,second)
57. Currying
Provide N argument lists, but only use 1
to N-1 of them to define a new function
def scaler (factor : Int)
(value : Int) =
factor * value
val times2 = scaler(2) _
times2(12) // == 24
61. “By Name” to the Rescue
def debug (msg : => String) {
if (this.debugEnabled) {
println(msg)
}
}
https://github.com/weiglewilczek/slf4s
62. Pattern Matching : Switch on Steroids
“case _” here is the default case
def literalMatch (in: Any) {
in match {
case 1 => doBar("One")
case "test" => doBar("test")
case 'x' => doBar("x")
case 2.2f => doBar("float")
case _ => doBar("lemon curry?")
}
}
63. Pattern Matching : Alternate Matches
Using “|” allows multi-match cases
def literalMatch (in: Any) {
in match {
case 1 | 2 | 3 => doBar("One to three")
case "this" | "that" => doBar("the other")
case _ => doBar("lemon curry?")
}
}
67. Pattern Matching : Generics
Erasure is not your friend
def typeMatch (in: Any) {
in match {
case ls : List[String] => doBar("danger!")
case li : List[Int] => doBar("never happens")
case _ => // NOOP
}
}
69. Pattern Matching : Generics Workaround
Like duck tape : ugly, but effective
def typeMatch (in: Any) {
in match {
case ls : List[_]
if ls.forall(_.isInstanceOf[String]) =>
doBar("Strings!")
case li : List[_] => doBar("Some kind of List")
case _ => // NOOP
}
}
70. More More Info on Scala
Home page: http://www.scala-lang.org/
Excellent community
scala-user@listes.epfl.ch
irc://chat.freenode.net/scala
http://scala.sygneca.com/ (Wiki)