Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Option

Exercise 1

  1. Write a function that return an Option[String]
  2. Call the function and pattern match on the possible results

Exercise 2

  1. Write a map with the days of the week 1 "monday", 2 :"tuesday", etc
  2. Write a function day that return the name of the day or an error message
def dayOfTheWeek(day: Int, map: [Int, String]): Option[String] = ???

Exercise 3

Write a vector with the number 1 to 10

val vector = Vector(1,2,3,4,5,6,7,8,9,10)

Write a function indexOf that

  • return the number at the index
  • or an error message when the index is out of bounds.
def indexOf(index: Int, vector: Vector[Int]): Try[Int] = ???

Call the indexOf function and print the result in a pattern match

indexOf(2, vector) match ???

Exercise 4

Do the same as in Exercise 3 but now use 'Either'

def indexOf(index: Int, vector: Vector[Int]): Either[Int] = ???