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)
   )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Pattern matching

A

val x: Int = Random.nextInt(10)

x match {
  case 0 => "zero"
  case 1 => "one"
  case 2 => "two"
  case _ => "other"
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Higher Order Function

A

def doSomethingWithStuff(strategy: Int => Int, x: Int) = strategy(x)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Range (inclusive)

A

(1 to n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

typealias

A

type bunchOfStuff = Int => Int

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Traits (interfaces)

A
trait BunchOfStuff {
  def doSomething(x: Int) : Int
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly