Kotlin Koans 14 In range

目次

In range (Playground)

Description

In Kotlin in checks are translated to the corresponding contains calls:

Kotlinでは、 incontains の呼び出しに該当します。

val list = listOf("a", "b")
"a" in list  // list.contains("a")
"a" !in list // !list.contains("a")

Read about ranges. Add a method fun contains(d: MyDate) to the class DateRange to allow in checks with a range of dates.

range(範囲) を読んでください。DateRange クラスに fun contains(d: MyDate) メソッドを追加して、in で日付の範囲チェックができるように実装してください。

Code

class DateRange(val start: MyDate, val endInclusive: MyDate) {
    operator fun contains(d: MyDate): Boolean = start <= d && d <= endInclusive
}

fun checkInRange(date: MyDate, first: MyDate, last: MyDate): Boolean {
    return date in DateRange(first, last)
}

Memo

← Posts