



val pi = 3.14 var counter = 0
val: value (constant), var: variabledef area(width: Double) = width * width
, like in AlgolInt and Double
are classesdef fac(n: Int): Int = { if (n <= 1) 1 else n * fac(n - 1) }
1.to(10) // Applies to method to 1, returns Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
1.to(10).map(fac) // Vector(1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800)
1.to(10).map(x => 1.0/x) // Vector(1.0, 0.5, 0.3333333333333333, 0.25, 0.2, ... 0.1) 1.to(10).map(1.0 / _) 1.to(10).sortBy(_ > _) // Vector(10, 9, 8, 7, ..., 1)
Bakersfield Horace Mitchell $285,000 $50,000 $12,000 Channel Island Richard R. Rush $275,000 $60,000 $12,000
Source.fromFile(new File("salaries.txt")).getLines().toArray
....map(_.split("\\s+")) // Note the Java interop
....map(w => w(w.length - 3)) // Array index uses (), not []
Getting closer:
Array($285,000, $275,000, ...)
....map(_.replaceAll("[^0-9]", "").toInt).sum
val book = <book><author>Cay Horstmann</author><title>Scala for the Impatient</title></book> val author = book \\ "author"
\\ is XPath-like match. (For obvious reasons, they couldn't
use //.)
<img src="hamster.jpeg"/>
<xsl:template match="@* |
node()"><xsl:apply-templates select="@* |
node()"/></xsl:template>, and that's just
to keep that recursion goingval doc = ConstructingParser.fromFile(new File(...), true).document val images = doc \\ "img" \\ "@src"

val a = - k * x / m v += a * dt x += v * dt
size(200, 200) // Processing
frameRate(200)
...
def draw { // Scala
val a = - k * x / m
v += a * dt
x += v * dt
background(255, 204, 0)
val thickness = 30
rect(100 - thickness, 0, thickness, (100 + 100 * x).toInt)
}
class Person(val name: String) {
var age: Int
}
val fred = new Person("Fred")
fred.age = 42
getAge/setAgeclass Person(val name: String) {
private var myAge: Int = -1
def age = myAge
def age_=(newAge: Int) { if (newAge > myAge) myAge = newAge }
}
trait MouseListener {
val toolkit = Toolkit.getDefaultToolkit
def mouseClicked(e: MouseEvent) { toolkit.beep() }
def mouseReleased(e: MouseEvent) {}
}
case class ClassName(field1: Type1, field2: Type2, ...) extends Superclass
class Tree
case class Empty() extends Tree { override def toString = "." }
case class Node(value: Int, left: Tree = Empty(), right: Tree = Empty()) extends Tree
ClassName(arg1, arg2, ...)
Node(1, Node(7, Empty(), Node(2)), Node(9))
1
/ \
7 9
\
2
expr match {
case pattern1 => expr1
...
case patternN => exprN
}
def sum(tree: Tree): Int = tree match {
case Node(v, l, r) => v + sum(l) + sum(r)
case Empty() => 0
}
l, r, v are bound to the
values in the case class instances; you can use them in the expressions.
case n @ Node(_, _, _) => n.someNodeMethod()
case _
n and empty children.v >
value, return a non-empty tree with the same root and the same
left, but recursively insert in the right. v < value is analogous. If v ==
value, return thisabstract class Tree { def insert(v: Int): Tree }
case class Empty() extends Tree {
def insert(v: Int) = Node(v)
}
case class Node(value: Int, left: Tree = Empty(), right: Tree = Empty()) extends Tree {
def insert(v: Int) = if (v > value) Node(value, left, right.insert(v))
else if (v < value) Node(value, left.insert(v), right)
else this
}
:paste)
val t = Empty().insert(3).insert(1).insert(4).insert(1).insert(5).insert(9)
import scala.actors.Actor import scala.actors.Actor._
Actor, or create anonymous instance with
actor functionmyActor ! msgObj
receive function has as it's argument a match clause
class BankAccountActor extends Actor {
var balance = 0.0;
def act() {
while (true) {
receive {
case Deposit(amount) => balance += amount
case Withdraw(amount) => balance -= amount
case Inquire => sender ! Balance(balance) } } } }
sender is an instance field of Actor that
receive sets to the sender of the received messagereceive blocks until a matching message is receivedreact to avoid blocking (see a book)

.par to get data structure whose methods are
automatically paralellizedval a = new Array[BigInt](1000000) for (i <- (0 until a.size).par) a(i) = i val sum = a.par.sum val evens = a.par.count(_ % 2 == 0)
def time(f: => Unit) = {
val start = System.currentTimeMillis
f
System.currentTimeMillis - start
}
time { for (i <- (0 until a.size).par) a(i) = i } // 867
time { for (i <- (0 until a.size)) a(i) = i } // 1017
time { a.par.sum } // 48
time { a.sum } // 85

