import collection.mutable.Stack import org.scalatest._ class ExampleSpec extends FlatSpec with Matchers { "A Stack" should "pop values in last-in-first-out order" in { val stack = new Stack[Int] stack.push(1) stack.push(2) stack.pop() should be (2) stack.pop() should be (1) } it should "throw NoSuchElementException if an empty stack is popped" in { val emptyStack = new Stack[Int] a [NoSuchElementException] should be thrownBy { emptyStack.pop() } } }
Scala Java
variables
|
|
methods
|
|
Scala Java
classes
+ equals() & hashcode() + toString() + pattern matching |
|
val evenNums = (1 to 1000).filter(x => x % 2 == 0)
val evenNums = (1 to 1000).filter(_ % 2 == 0)
def isEven = _ % 2 == 0
val evenNums = (1 to 1000) filter isEven
val evenNums = for (i <- 1 to 1000; if i % 2 == 0) yield i)
val evenNums = for (i <- 1 to 500) yield 2 * i)
val evenNums = 2 to 1000 by 2
def foo(f1: (String => Int), f2: (Int => String)) = f1 andThen f2
val obfuscate = foo(_.length, "*" * _)
bind(String.class)
.annotatedWith(Names.named("JDBC URL"))
.toInstance("jdbc:mysql://localhost/pizza");
class IntWrapper(val value: Int) {
def C = s"$value degrees" // same as value + "degrees"
}
implicit def Int2IntWrapper(value: Int) = new IntWrapper(value)
(new {
def foo() = "bar"
}).foo()
one argument
|
|
no argument
|
|
precedence rules
|
|
println("Hello World!")