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

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
 
(사용자 2명의 중간 판 17개는 보이지 않습니다)
1번째 줄: 1번째 줄:
==개요==
[[분류: Kotlin Koans]]
;Kotlin Koans - Conventions
{| class='wikitable count'
! # !! [[Kotlin Koans]]
|- class='count-reset'
| || [[Kotlin Koans - Introduction]]
|-
| || [[Kotlin Koans - Conventions]]
|-
| || [[Kotlin Koans - Collections]]
|-
| || [[Kotlin Koans - Properties]]
|-
| || [[Kotlin Koans - Builders]]
|-
| || [[Kotlin Koans - Generics]]
|}


==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 {
13번째 줄: 27번째 줄:


fun compare(date1: MyDate, date2: MyDate) = date1 < date2
fun compare(date1: MyDate, date2: MyDate) = date1 < date2
</source>
</syntaxhighlight>


==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
24번째 줄: 38번째 줄:
     return date in DateRange(first, last)
     return date in DateRange(first, last)
}
}
</source>
</syntaxhighlight>
 
{{글숨김|MyDate.kt}}
===MyDate.kt===
<syntaxhighlight lang='kotlin'>
<source 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 {
35번째 줄: 48번째 줄:
     }
     }
}
}
</source>
</syntaxhighlight>
{{글숨김끝}}
 
==Range to==
==Range to==
<source lang='kotlin'>
<syntaxhighlight lang='kotlin'>
operator fun MyDate.rangeTo(other: MyDate) = DateRange(this, other)
 
class DateRange(override val start: MyDate, override val endInclusive: MyDate): ClosedRange<MyDate>


</source>
fun checkInRange(date: MyDate, first: MyDate, last: MyDate): Boolean {
    return date in first..last
}
</syntaxhighlight>
{{글숨김|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
    }
}
</syntaxhighlight>
{{글숨김끝}}


==For loop==
==For loop==
<source lang='kotlin'>
<syntaxhighlight lang='kotlin'>
</source>
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)
    }
}
</syntaxhighlight>
{{글숨김|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)
</syntaxhighlight>
{{글숨김끝}}
{{글숨김|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))
}
</syntaxhighlight>
{{글숨김끝}}


==Operators overloading==
==Operators overloading==
<source lang='kotlin'>
<syntaxhighlight lang='kotlin'>
</source>
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
}
</syntaxhighlight>
{{글숨김|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))
}
</syntaxhighlight>
{{글숨김끝}}


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


==Invoke==
==Invoke==
<source lang='kotlin'>
{{참고|Kotlin invoke()}}
</source>
<syntaxhighlight lang='kotlin'>
 
class Invokable {
==같이 보기==
    var numberOfInvocations: Int = 0
* [[Kotlin Koans]]
        private set
    operator fun invoke(): Invokable {
        numberOfInvocations++
        return this
    }
}


[[분류: Kotlin]]
fun invokeTwice(invokable: Invokable) = invokable()()
</syntaxhighlight>

2020년 11월 2일 (월) 00:55 기준 최신판

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

1 Comparison[ | ]

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[ | ]

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)
}
MyDate.kt
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
    }
}

3 Range to[ | ]

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
}
MyDate.kt
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
    }
}

4 For loop[ | ]

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)
    }
}
MyDate.kt
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)
DateUtil.kt
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))
}

5 Operators overloading[ | ]

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
}
DateUtil.kt
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))
}

6 Destructuring declarations[ | ]

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[ | ]

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

fun invokeTwice(invokable: Invokable) = invokable()()
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}