Scala Flashcards
1
Q
Instantiate List
A
// List of Strings val fruit: List[String] = List("apples", "oranges", "pears")
// List of Integers val nums: List[Int] = List(1, 2, 3, 4)
// Empty List. val empty: List[Nothing] = List()
// Two dimensional list val dim: List[List[Int]] = List( List(1, 0, 0), List(0, 1, 0), List(0, 0, 1) )
2
Q
Pattern matching
A
val x: Int = Random.nextInt(10)
x match { case 0 => "zero" case 1 => "one" case 2 => "two" case _ => "other" }
3
Q
Extension method
A
case class Circle(x: Double, y: Double, radius: Double)
extension (c: Circle) def circumference: Double = c.radius * math.Pi * 2
4
Q
Higher Order Function
A
def doSomethingWithStuff(strategy: Int => Int, x: Int) = strategy(x)
5
Q
Range (inclusive)
A
(1 to n)
6
Q
typealias
A
type bunchOfStuff = Int => Int
7
Q
Traits (interfaces)
A
trait BunchOfStuff { def doSomething(x: Int) : Int }