v Partial Functions - Scala

Partial Functions

If you want to learn more, check out Scala Cookbook and Programming in Scala.

isDefinedAt determines which inputs are accepted. apply is the actual operation.

Create A Partial Function

// Create a new partial function that inputs a integer and outputs a string
val dayOfTheWeek = new PartialFunction[Int, String] {
    // Create an array with the days of the week
    val days = Array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")

    // Only accept input integers that are between 0 and 6
    def isDefinedAt(i: Int) = i > 0 && i < 6

    // If accepted, return the correct day of the week string
    def apply(i: Int) = days(i-1)
}

Run The Partial Function

dayOfTheWeek(2)
Tuesday