-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
220 additions
and
0 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
ryeojin/source/src/main/kotlin/ch09/9.1.1_1_GenericFunctionsAndProperties.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package ch09.ex1_1_1_GenericFunctionsAndProperties | ||
|
||
fun main(args: Array<String>) { | ||
val letters = ('a'..'z').toList() | ||
println(letters.slice<Char>(0..2)) | ||
println(letters.slice(10..13)) | ||
} |
8 changes: 8 additions & 0 deletions
8
ryeojin/source/src/main/kotlin/ch09/9.1.1_2_GenericFunctionsAndProperties1.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package ch09.ex1_1_2_GenericFunctionsAndProperties1 | ||
|
||
val <T> List<T>.penultimate: T | ||
get() = this[size - 2] | ||
|
||
fun main(args: Array<String>) { | ||
println(listOf(1, 2, 3, 4).penultimate) | ||
} |
9 changes: 9 additions & 0 deletions
9
ryeojin/source/src/main/kotlin/ch09/9.1.3_1_TypeParameterConstraints.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package ch09.ex1_3_1_TypeParameterConstraints | ||
|
||
fun <T : Number> oneHalf(value: T): Double { | ||
return value.toDouble() / 2.0 | ||
} | ||
|
||
fun main(args: Array<String>) { | ||
println(oneHalf(3)) | ||
} |
9 changes: 9 additions & 0 deletions
9
ryeojin/source/src/main/kotlin/ch09/9.1.3_2_TypeParameterConstraints1.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package ch09.ex1_3_2_TypeParameterConstraints1 | ||
|
||
fun <T: Comparable<T>> max(first: T, second: T): T { | ||
return if (first > second) first else second | ||
} | ||
|
||
fun main(args: Array<String>) { | ||
println(max("kotlin", "java")) | ||
} |
16 changes: 16 additions & 0 deletions
16
ryeojin/source/src/main/kotlin/ch09/9.1.3_3_TypeParameterConstraints2.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package ch09.ex1_3_3_TypeParameterConstraints2 | ||
|
||
import java.time.Period | ||
|
||
fun <T> ensureTrailingPeriod(seq: T) | ||
where T : CharSequence, T : Appendable { | ||
if (!seq.endsWith('.')) { | ||
seq.append('.') | ||
} | ||
} | ||
|
||
fun main(args: Array<String>) { | ||
val helloWorld = StringBuilder("Hello World") | ||
ensureTrailingPeriod(helloWorld) | ||
println(helloWorld) | ||
} |
11 changes: 11 additions & 0 deletions
11
ryeojin/source/src/main/kotlin/ch09/9.2.1_1_GenericsAtRuntimeTypeChecksAndCasts.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package ch09.ex2_1_1_GenericsAtRuntimeTypeChecksAndCasts | ||
|
||
fun printSum(c: Collection<*>) { | ||
val intList = c as? List<Int> | ||
?: throw IllegalArgumentException("List is expected") | ||
println(intList.sum()) | ||
} | ||
|
||
fun main(args: Array<String>) { | ||
printSum(listOf(1, 2, 3)) | ||
} |
11 changes: 11 additions & 0 deletions
11
ryeojin/source/src/main/kotlin/ch09/9.2.1_2_GenericsAtRuntimeTypeChecksAndCasts1.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package ch09.ex2_1_2_GenericsAtRuntimeTypeChecksAndCasts1 | ||
|
||
fun printSum(c: Collection<Int>) { | ||
if (c is List<Int>) { | ||
println(c.sum()) | ||
} | ||
} | ||
|
||
fun main(args: Array<String>) { | ||
printSum(listOf(1, 2, 3)) | ||
} |
8 changes: 8 additions & 0 deletions
8
ryeojin/source/src/main/kotlin/ch09/9.2.2_1_DeclaringFunctionsWithReifiedTypeParameters.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package ch09.ex2_2_1_DeclaringFunctionsWithReifiedTypeParameters | ||
|
||
inline fun <reified T> isA(value: Any) = value is T | ||
|
||
fun main(args: Array<String>) { | ||
println(isA<String>("abc")) | ||
println(isA<String>(123)) | ||
} |
6 changes: 6 additions & 0 deletions
6
ryeojin/source/src/main/kotlin/ch09/9.2.2_2_DeclaringFunctionsWithReifiedTypeParameters1.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package ch09.ex2_2_2_DeclaringFunctionsWithReifiedTypeParameters1 | ||
|
||
fun main(args: Array<String>) { | ||
val items = listOf("one", 2, "three") | ||
println(items.filterIsInstance<String>()) | ||
} |
9 changes: 9 additions & 0 deletions
9
ryeojin/source/src/main/kotlin/ch09/9.3.1_WhyVarianceExists.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package ch09.ex3_1_WhyVarianceExists | ||
|
||
fun printContents(list: List<Any>) { | ||
println(list.joinToString()) | ||
} | ||
|
||
fun main(args: Array<String>) { | ||
printContents(listOf("abc", "bac")) | ||
} |
15 changes: 15 additions & 0 deletions
15
ryeojin/source/src/main/kotlin/ch09/9.3.5_1_CopyDataAny.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package ch09.CopyDataAny | ||
|
||
fun <T: R, R> copyData(source: MutableList<T>, | ||
destination: MutableList<R>) { | ||
for (item in source) { | ||
destination.add(item) | ||
} | ||
} | ||
|
||
fun main(args: Array<String>) { | ||
val ints = mutableListOf(1, 2, 3) | ||
val anyItems = mutableListOf<Any>() | ||
copyData(ints, anyItems) | ||
println(anyItems) | ||
} |
15 changes: 15 additions & 0 deletions
15
ryeojin/source/src/main/kotlin/ch09/9.3.5_2_CopyDataOut.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package ch09.CopyDataOut | ||
|
||
fun <T> copyData(source: MutableList<out T>, | ||
destination: MutableList<T>) { | ||
for (item in source) { | ||
destination.add(item) | ||
} | ||
} | ||
|
||
fun main(args: Array<String>) { | ||
val ints = mutableListOf(1, 2, 3) | ||
val anyItems = mutableListOf<Any>() | ||
copyData(ints, anyItems) | ||
println(anyItems) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package ch09.CopyDataIn | ||
|
||
fun <T> copyData(source: MutableList<T>, | ||
destination: MutableList<in T>) { | ||
for (item in source) { | ||
destination.add(item) | ||
} | ||
} | ||
|
||
fun main(args: Array<String>) { | ||
val ints = mutableListOf(1, 2, 3) | ||
val anyItems = mutableListOf<Any>() | ||
copyData(ints, anyItems) | ||
println(anyItems) | ||
} |
11 changes: 11 additions & 0 deletions
11
ryeojin/source/src/main/kotlin/ch09/9.3.6.2_1_printFirst.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package ch09.printFirst | ||
|
||
fun printFirst(list: List<*>) { | ||
if (list.isNotEmpty()) { | ||
println(list.first()) | ||
} | ||
} | ||
|
||
fun main(args: Array<String>) { | ||
printFirst(listOf("Svetlana", "Dmitry")) | ||
} |
11 changes: 11 additions & 0 deletions
11
ryeojin/source/src/main/kotlin/ch09/9.3.6.2_2_StarProjection1.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package ch09.ex3_6_2_2_StarProjection1 | ||
|
||
fun <T> printFirst(list: List<T>) { | ||
if (list.isNotEmpty()) { | ||
println(list.first()) | ||
} | ||
} | ||
|
||
fun main(args: Array<String>) { | ||
printFirst(listOf("Svetlana", "Dmitry")) | ||
} |
21 changes: 21 additions & 0 deletions
21
ryeojin/source/src/main/kotlin/ch09/9.3.6.2_3_FieldValidator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package ch09.FieldValidator | ||
|
||
import kotlin.reflect.KClass | ||
|
||
interface FieldValidator<in T> { | ||
fun validate(input: T): Boolean | ||
} | ||
|
||
object DefaultStringValidator : FieldValidator<String> { | ||
override fun validate(input: String) = input.isNotEmpty() | ||
} | ||
|
||
object DefaultIntValidator : FieldValidator<Int> { | ||
override fun validate(input: Int) = input >= 0 | ||
} | ||
|
||
fun main(args: Array<String>) { | ||
val validators = mutableMapOf<KClass<*>, FieldValidator<*>>() | ||
validators[String::class] = DefaultStringValidator | ||
validators[Int::class] = DefaultIntValidator | ||
} |
38 changes: 38 additions & 0 deletions
38
ryeojin/source/src/main/kotlin/ch09/9.3.6.2_4_StarProjection3.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package ch09.ex3_6_2_4_StarProjection3 | ||
|
||
import kotlin.reflect.KClass | ||
|
||
interface FieldValidator<in T> { | ||
fun validate(input: T): Boolean | ||
} | ||
|
||
object DefaultStringValidator : FieldValidator<String> { | ||
override fun validate(input: String) = input.isNotEmpty() | ||
} | ||
|
||
object DefaultIntValidator : FieldValidator<Int> { | ||
override fun validate(input: Int) = input >= 0 | ||
} | ||
|
||
object Validators { | ||
private val validators = | ||
mutableMapOf<KClass<*>, FieldValidator<*>>() | ||
|
||
fun <T: Any> registerValidator( | ||
kClass: KClass<T>, fieldValidator: FieldValidator<T>) { | ||
validators[kClass] = fieldValidator | ||
} | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
operator fun <T: Any> get(kClass: KClass<T>): FieldValidator<T> = | ||
validators[kClass] as? FieldValidator<T> | ||
?: throw IllegalArgumentException( | ||
"No validator for ${kClass.simpleName}") | ||
} | ||
|
||
fun main(args: Array<String>) { | ||
Validators.registerValidator(String::class, DefaultStringValidator) | ||
Validators.registerValidator(Int::class, DefaultIntValidator) | ||
println(Validators[String::class].validate("Kotlin")) | ||
println(Validators[Int::class].validate(42)) | ||
} |