"Kotlin Koans - Conventions"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-<source +<syntaxhighlight ))
17번째 줄: 17번째 줄:


==Comparison==
==Comparison==
<source lang='kotlin'>
<syntaxhighlight lang='kotlin'>
data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) : Comparable<MyDate> {
data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) : Comparable<MyDate> {
     override fun compareTo(other: MyDate) = when {
     override fun compareTo(other: MyDate) = when {
30번째 줄: 30번째 줄:


==In range==
==In range==
<source lang='kotlin'>
<syntaxhighlight lang='kotlin'>
class DateRange(val start: MyDate, val endInclusive: MyDate) {
class DateRange(val start: MyDate, val endInclusive: MyDate) {
     operator fun contains(item: MyDate): Boolean = start <= item && item <= endInclusive
     operator fun contains(item: MyDate): Boolean = start <= item && item <= endInclusive
40번째 줄: 40번째 줄:
</source>
</source>
{{글숨김|MyDate.kt}}
{{글숨김|MyDate.kt}}
<source lang='kotlin'>
<syntaxhighlight lang='kotlin'>
data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) : Comparable<MyDate> {
data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) : Comparable<MyDate> {
     override fun compareTo(other: MyDate) = when {
     override fun compareTo(other: MyDate) = when {
52번째 줄: 52번째 줄:


==Range to==
==Range to==
<source lang='kotlin'>
<syntaxhighlight lang='kotlin'>
operator fun MyDate.rangeTo(other: MyDate) = DateRange(this, other)
operator fun MyDate.rangeTo(other: MyDate) = DateRange(this, other)


62번째 줄: 62번째 줄:
</source>
</source>
{{글숨김|MyDate.kt}}
{{글숨김|MyDate.kt}}
<source lang='kotlin'>
<syntaxhighlight lang='kotlin'>
data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) : Comparable<MyDate> {
data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) : Comparable<MyDate> {
     override fun compareTo(other: MyDate) = when {
     override fun compareTo(other: MyDate) = when {
74번째 줄: 74번째 줄:


==For loop==
==For loop==
<source lang='kotlin'>
<syntaxhighlight lang='kotlin'>
class DateRange(val start: MyDate, val end: MyDate): Iterable<MyDate>{
class DateRange(val start: MyDate, val end: MyDate): Iterable<MyDate>{
     override fun iterator(): Iterator<MyDate> = DateIterator(this)
     override fun iterator(): Iterator<MyDate> = DateIterator(this)
96번째 줄: 96번째 줄:
</source>
</source>
{{글숨김|MyDate.kt}}
{{글숨김|MyDate.kt}}
<source lang='kotlin'>
<syntaxhighlight lang='kotlin'>
data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) : Comparable<MyDate> {
data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) : Comparable<MyDate> {
     override fun compareTo(other: MyDate) = when {
     override fun compareTo(other: MyDate) = when {
109번째 줄: 109번째 줄:
{{글숨김끝}}
{{글숨김끝}}
{{글숨김|DateUtil.kt}}
{{글숨김|DateUtil.kt}}
<source lang='kotlin'>
<syntaxhighlight lang='kotlin'>
import java.util.Calendar
import java.util.Calendar
134번째 줄: 134번째 줄:


==Operators overloading==
==Operators overloading==
<source lang='kotlin'>
<syntaxhighlight lang='kotlin'>
import TimeInterval.*
import TimeInterval.*


157번째 줄: 157번째 줄:
</source>
</source>
{{글숨김|DateUtil.kt}}
{{글숨김|DateUtil.kt}}
<source lang='kotlin'>
<syntaxhighlight lang='kotlin'>
import java.util.Calendar
import java.util.Calendar
174번째 줄: 174번째 줄:


==Destructuring declarations==
==Destructuring declarations==
<source lang='kotlin'>
<syntaxhighlight lang='kotlin'>
data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int)
data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int)


188번째 줄: 188번째 줄:
==Invoke==
==Invoke==
{{참고|Kotlin invoke()}}
{{참고|Kotlin invoke()}}
<source lang='kotlin'>
<syntaxhighlight lang='kotlin'>
class Invokable {
class Invokable {
     var numberOfInvocations: Int = 0
     var numberOfInvocations: Int = 0

2020년 11월 2일 (월) 00:41 판

# Kotlin Koans
Kotlin Koans - Introduction
Kotlin Koans - Conventions
Kotlin Koans - Collections
Kotlin Koans - Properties
Kotlin Koans - Builders
Kotlin Koans - Generics

1 Comparison

<syntaxhighlight lang='kotlin'> 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 </source>

2 In range

<syntaxhighlight lang='kotlin'> 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)

} </source>

MyDate.kt

<syntaxhighlight lang='kotlin'> 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
   }

} </source>

3 Range to

<syntaxhighlight lang='kotlin'> 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

} </source>

MyDate.kt

<syntaxhighlight lang='kotlin'> 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
   }

} </source>

4 For loop

<syntaxhighlight lang='kotlin'> 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)
   }

} </source>

MyDate.kt

<syntaxhighlight lang='kotlin'> 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
   }

} ​ operator fun MyDate.rangeTo(other: MyDate) = DateRange(this, other) </source>

DateUtil.kt

<syntaxhighlight lang='kotlin'> import java.util.Calendar ​ fun MyDate.nextDay() = addTimeIntervals(TimeInterval.DAY, 1) ​ enum class TimeInterval {

   DAY,
   WEEK,
   YEAR

} ​ fun MyDate.addTimeIntervals(timeInterval: TimeInterval, number: Int): MyDate {

   val c = Calendar.getInstance()
   c.set(year, month, dayOfMonth)
   when (timeInterval) {
       TimeInterval.DAY -> c.add(Calendar.DAY_OF_MONTH, number)
       TimeInterval.WEEK -> c.add(Calendar.WEEK_OF_MONTH, number)
       TimeInterval.YEAR -> c.add(Calendar.YEAR, number)
   }
   return MyDate(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE))

} </source>

5 Operators overloading

<syntaxhighlight lang='kotlin'> 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

} </source>

DateUtil.kt

<syntaxhighlight lang='kotlin'> import java.util.Calendar ​ fun MyDate.addTimeIntervals(timeInterval: TimeInterval, number: Int): MyDate {

   val c = Calendar.getInstance()
   c.set(year, month, dayOfMonth)
   when (timeInterval) {
       TimeInterval.DAY -> c.add(Calendar.DAY_OF_MONTH, number)
       TimeInterval.WEEK -> c.add(Calendar.WEEK_OF_MONTH, number)
       TimeInterval.YEAR -> c.add(Calendar.YEAR, number)
   }
   return MyDate(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE))

} </source>

6 Destructuring declarations

<syntaxhighlight lang='kotlin'> 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

} </source>

7 Invoke

<syntaxhighlight lang='kotlin'> class Invokable {

   var numberOfInvocations: Int = 0
       private set
   operator fun invoke(): Invokable {
       numberOfInvocations++
       return this
   }

}

fun invokeTwice(invokable: Invokable) = invokable()() </source>

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}