Scala
object-functional
Lorenzo Gallegos, Keith Risper, Lars Walen
Big Picture
- development started in 2001
- at the École Polytechnique Fédérale de Lausae (Switzerland)
- by Martin Odersky
- "for general software applications, statically typed, designed to concisely express solutions in an elegant, type-safe and lightweight manner"
Hello World
object HelloWorld extends App {
println("Hello, World!")
}
or
println("Hello, World!")
99 Bottles of Beer
(99 to 1 by -1).par foreach {n =>
println("""|%d bottles of beer on the wall
|%d bottles of beer
|Take one down, pass it around
|%d bottles of beer on the wall\n""".stripMargin format (n, n, n -1))}
Object-Oriented Features
- Every value is an object
- Class methods and variables
- Subclassing
Classes
class Point(xc: Int, yc: Int) {
var x: Int = xc
var y: Int = yc
def move(dx: Int, dy: Int) {
x = x + dx
y = y + dy
}
override def toString(): String = "(" + x + ", " + y + ")";
}
Possible successor to Java?
James Strachan, creator of Groovy, thinks so.
Functional Features
Just about everything we learned about in Haskell
For-expressions
- Different name for list comprehensions
val s = for (x <- 1 to 25 if x*x > 50) yield 2*x
Anonymous Functions
- Like lambdas in Haskell
- Use type inference
list map { x => sqrt(x) }
Lazy Evaluation
- Usually evaluates eagerly
-
lazy keyword specifies lazy evaluation
Higher-Order Functions
list.filter(_ > 2).map(_ * 3).sum
Exception Handling
- Very similar to Java
- No checked exceptions
Likeable Things
- Immutable data structures
- Traits
- Imports