August 26, 2024

P01 - Find the last element of a list.

Example:

> last(listOf(1, 1, 2, 3, 5, 8))
8

kotlin

package org.kotlin99.lists

import com.natpryce.hamkrest.assertion.assertThat
import com.natpryce.hamkrest.equalTo
import org.junit.Test

fun <T> last(list: List<T>): T = list[list.lastIndex]

class P01Test {
    @Test fun `find the last element of a list`() {
        assertThat(last(listOf(1, 1, 2, 3, 5, 8)), equalTo(8))
    }

    @Test(expected = IndexOutOfBoundsException::class)
    fun `last element in empty list`() {
        last(listOf<Int>())
    }
}
Be first to comment
Leave a reply