when

Scrabble Score
Scrabble Score in Kotlin
object ScrabbleScore {

    private fun scoreLetter(c: Char) =
            when (c.uppercaseChar()) {
                'A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T' -> 1
                'D', 'G' -> 2
                'B', 'C', 'M', 'P' -> 3
                'F', 'H', 'V', 'W', 'Y' -> 4
                'K' -> 5
                'J', 'X' -> 8
                'Q', 'Z' -> 10
                else -> 0
            }

    fun scoreWord(word: String) = word.sumOf { scoreLetter(it) }
}

An object declaration is used to define ScrabbleScore as essentially a singleton object instantiation of the class. This is sufficient, since there is no object state that needs to change with each call of the scoreWord method.

The private method for scoring the letter is implemented by a when expression. Although the function has multiple lines, it consists only of the one when expression, so it is defined using single-expression function syntax, with the curly braces omitted and the return type inferred.

The scoreWord function is also a single-expression function, implemented with the sumOf aggregate operation on the input word. The lambda of sumOf uses the it keyword to refer to the single Char parameter for the lambda, and passes that to the function for scoring a letter. The function returns the result of calling sumOf, which is the sum of the scores for each of the characters in the word.

17th Jul 2024 · Found it useful?