# | Kotlin Koans |
---|---|
Kotlin Koans - Introduction | |
Kotlin Koans - Conventions | |
Kotlin Koans - Collections | |
Kotlin Koans - Properties | |
Kotlin Koans - Builders | |
Kotlin Koans - Generics |
1 Comparison
Kotlin
Copy
data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) : Comparable<MyDate> {
override fun compareTo(other: MyDate) = when {
year != other.year -> year - other.year
month != other.month -> month - other.month
else -> dayOfMonth - other.dayOfMonth
}
}
fun compare(date1: MyDate, date2: MyDate) = date1 < date2
2 In range
Kotlin
Copy
class DateRange(val start: MyDate, val endInclusive: MyDate) {
operator fun contains(item: MyDate): Boolean = start <= item && item <= endInclusive
}
fun checkInRange(date: MyDate, first: MyDate, last: MyDate): Boolean {
return date in DateRange(first, last)
}
3 Range to
Kotlin
Copy
operator fun MyDate.rangeTo(other: MyDate) = DateRange(this, other)
class DateRange(override val start: MyDate, override val endInclusive: MyDate): ClosedRange<MyDate>
fun checkInRange(date: MyDate, first: MyDate, last: MyDate): Boolean {
return date in first..last
}
4 For loop
Kotlin
Copy
class DateRange(val start: MyDate, val end: MyDate): Iterable<MyDate>{
override fun iterator(): Iterator<MyDate> = DateIterator(this)
}
class DateIterator(val dateRange:DateRange) : Iterator<MyDate> {
var current: MyDate = dateRange.start
override fun next(): MyDate {
val result = current
current = current.nextDay()
return result
}
override fun hasNext(): Boolean = current <= dateRange.end
}
fun iterateOverDateRange(firstDate: MyDate, secondDate: MyDate, handler: (MyDate) -> Unit) {
for (date in firstDate..secondDate) {
handler(date)
}
}
5 Operators overloading
Kotlin
Copy
import TimeInterval.*
data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int)
enum class TimeInterval { DAY, WEEK, YEAR }
operator fun MyDate.plus(timeInterval: TimeInterval) = addTimeIntervals(timeInterval, 1)
class RepeatedTimeInterval(val timeInterval: TimeInterval, val number: Int)
operator fun TimeInterval.times(number: Int) = RepeatedTimeInterval(this, number)
operator fun MyDate.plus(timeIntervals: RepeatedTimeInterval) = addTimeIntervals(timeIntervals.timeInterval, timeIntervals.number)
fun task1(today: MyDate): MyDate {
return today + YEAR + WEEK
}
fun task2(today: MyDate): MyDate {
return today + YEAR * 2 + WEEK * 3 + DAY * 5
}
6 Destructuring declarations
Kotlin
Copy
data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int)
fun isLeapDay(date: MyDate): Boolean {
val (year, month, dayOfMonth) = date
// 29 February of a leap year
return year % 4 == 0 && month == 2 && dayOfMonth == 29
}
7 Invoke
Kotlin
편집자 Jmnote Jmnote bot
로그인하시면 댓글을 쓸 수 있습니다.