From 868cbc0f50a05d4193af0ca99cd220a43fafe75d Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Fri, 16 Apr 2021 14:23:07 +0300 Subject: [PATCH] lesson4 --- build.gradle.kts | 2 +- gradle/wrapper/gradle-wrapper.properties | 2 +- src/main/kotlin/lesson2/idiom8.kt | 6 ++-- src/main/kotlin/lesson4/Idiom31.kt | 34 ++++++++++++++++++++++ src/main/kotlin/lesson4/idiom27.kt | 18 ++++++++++++ src/main/kotlin/lesson4/idiom28.kt | 11 ++++++++ src/main/kotlin/lesson4/idiom29.kt | 12 ++++++++ src/main/kotlin/lesson4/idiom30.kt | 24 ++++++++++++++++ src/main/kotlin/lesson4/idiom32.kt | 17 +++++++++++ src/main/kotlin/lesson4/idiom33.kt | 36 ++++++++++++++++++++++++ src/main/kotlin/lesson4/idiom34.kt | 16 +++++++++++ 11 files changed, 173 insertions(+), 5 deletions(-) create mode 100644 src/main/kotlin/lesson4/Idiom31.kt create mode 100644 src/main/kotlin/lesson4/idiom27.kt create mode 100644 src/main/kotlin/lesson4/idiom28.kt create mode 100644 src/main/kotlin/lesson4/idiom29.kt create mode 100644 src/main/kotlin/lesson4/idiom30.kt create mode 100644 src/main/kotlin/lesson4/idiom32.kt create mode 100644 src/main/kotlin/lesson4/idiom33.kt create mode 100644 src/main/kotlin/lesson4/idiom34.kt diff --git a/build.gradle.kts b/build.gradle.kts index a321a2f..31f6ed0 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,5 +1,5 @@ plugins { - kotlin("jvm") version "1.4.31" + kotlin("jvm") version "1.4.32" } group = "ru.mipt.npm" diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 4d9ca16..442d913 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/src/main/kotlin/lesson2/idiom8.kt b/src/main/kotlin/lesson2/idiom8.kt index b7e8d48..f37e754 100644 --- a/src/main/kotlin/lesson2/idiom8.kt +++ b/src/main/kotlin/lesson2/idiom8.kt @@ -3,12 +3,12 @@ package lesson2 class SimpleClass(val a: Int, val b: Double, c: Double) -data class DataClass(val a: Int, val b: Double){ - val c get() = b +1 +data class DataClass(val a: Int, val b: Double) { + val c get() = b + 1 } fun main() { - val simple = SimpleClass(1,2.0,3.0) + val simple = SimpleClass(1, 2.0, 3.0) println(simple) diff --git a/src/main/kotlin/lesson4/Idiom31.kt b/src/main/kotlin/lesson4/Idiom31.kt new file mode 100644 index 0000000..582bdf2 --- /dev/null +++ b/src/main/kotlin/lesson4/Idiom31.kt @@ -0,0 +1,34 @@ +package lesson4 + +class Bad{ + val value: Int + + init { + value = requestValue() + } + + private fun requestValue(): Int = TODO() +} + + +//Factory functions are preferred to the initialization logic + +class Good internal constructor(val value: Int){ + init { + //Initialization block is there to check arguments + require(value >= 0) + } + + companion object +} + +private fun requestValue(): Int = TODO() + +// This is the factory-function +fun Good() = Good(requestValue()) + + +// additional constructor-like builders could be added to the companion + +@OptIn(ExperimentalUnsignedTypes::class) +fun Good.Companion.build(value: UInt) = Good(value.toInt()) \ No newline at end of file diff --git a/src/main/kotlin/lesson4/idiom27.kt b/src/main/kotlin/lesson4/idiom27.kt new file mode 100644 index 0000000..0c1433a --- /dev/null +++ b/src/main/kotlin/lesson4/idiom27.kt @@ -0,0 +1,18 @@ +package lesson4 + +import kotlin.random.Random + +//TODO move to beginning of the course +fun main() { + val b = Random.nextBoolean() + // assign value to an expression + val a = if (b) 0 else 1 + + //Use Try/catch as an exception + val result = try { + "22a".toInt() + } catch (e: NumberFormatException) { + null //Strongly discouraged in performance-critical code + } + +} \ No newline at end of file diff --git a/src/main/kotlin/lesson4/idiom28.kt b/src/main/kotlin/lesson4/idiom28.kt new file mode 100644 index 0000000..32ff608 --- /dev/null +++ b/src/main/kotlin/lesson4/idiom28.kt @@ -0,0 +1,11 @@ +package lesson4 + +@OptIn(ExperimentalStdlibApi::class) +fun main() { + //Consume mutating lambda + val list = buildList { + repeat(10){ + add(it) + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/lesson4/idiom29.kt b/src/main/kotlin/lesson4/idiom29.kt new file mode 100644 index 0000000..9ed32d8 --- /dev/null +++ b/src/main/kotlin/lesson4/idiom29.kt @@ -0,0 +1,12 @@ +package lesson4 + +import java.nio.file.Files +import java.nio.file.Paths + +fun main() { + val stream = Files.newInputStream(Paths.get("/some/file.txt")) + // The resource is automatically closed when leaving the scope + stream.buffered().reader().use { reader -> + println(reader.readText()) + } +} \ No newline at end of file diff --git a/src/main/kotlin/lesson4/idiom30.kt b/src/main/kotlin/lesson4/idiom30.kt new file mode 100644 index 0000000..0f43045 --- /dev/null +++ b/src/main/kotlin/lesson4/idiom30.kt @@ -0,0 +1,24 @@ +package lesson4 + +interface Factory { + fun build(str: String): T +} + + +class IntContainer(val arg: Int) { + companion object : Factory { + override fun build(str: String) = IntContainer(str.toInt()) + } +} + +class DoubleContainer(val arg: Double) { + companion object : Factory { + override fun build(str: String) = DoubleContainer(str.toDouble()) + } +} + +fun buildContainer(str: String, factory: Factory): T = factory.build(str) + +fun main() { + buildContainer("22", IntContainer) +} \ No newline at end of file diff --git a/src/main/kotlin/lesson4/idiom32.kt b/src/main/kotlin/lesson4/idiom32.kt new file mode 100644 index 0000000..73b5b2d --- /dev/null +++ b/src/main/kotlin/lesson4/idiom32.kt @@ -0,0 +1,17 @@ +package lesson4 + +class ClassWithALazyProperty{ + //Use lazy delegate for something that should be calculated ones on first call + val lazyValue by lazy { + //Do dome heavy logic here + } +} + +//Using other delegates +fun main() { + val map = mapOf("a" to 1, "b" to 2) + + val a by map + + println(a) +} \ No newline at end of file diff --git a/src/main/kotlin/lesson4/idiom33.kt b/src/main/kotlin/lesson4/idiom33.kt new file mode 100644 index 0000000..7b32ee2 --- /dev/null +++ b/src/main/kotlin/lesson4/idiom33.kt @@ -0,0 +1,36 @@ +package lesson4 + +/** + * The demonstration of use of inline [forEach] function with non-local return + */ +fun foo() { + listOf(1, 2, 3, 4, 5).forEach { + if (it == 3) return // non-local return directly to the caller of foo() + print(it) + } + println("this point is unreachable") +} + +/** + * Definition of inline function + */ +inline fun List.forEachOdd(block: (Int) -> Unit) = forEach { + if (it % 2 == 1) block(it) +} + + +/** + * Using inline function for type reification during the compile time + */ +inline fun List.prettyPrint() = forEach { + when (T::class) { + Double::class -> println("Double: ${it as Double}") + Int::class -> println("Int: ${it as Int}") + else -> it.toString() + } +} + +/** + * **WARNING** inline functions are an advanced feature and should be used only for reification or non-local return + * NOT for optimization. + */ \ No newline at end of file diff --git a/src/main/kotlin/lesson4/idiom34.kt b/src/main/kotlin/lesson4/idiom34.kt new file mode 100644 index 0000000..77030c2 --- /dev/null +++ b/src/main/kotlin/lesson4/idiom34.kt @@ -0,0 +1,16 @@ +package lesson4 + +/** + * Values are boxed. Each call is indirect + */ +val list: List = List(20){it.toDouble()} + +/** + * Still boxed + */ +val genericArray: Array = Array(20){it.toDouble()} + +/** + * Non-boxed + */ +val specializedArray: DoubleArray = DoubleArray(20){it.toDouble()} \ No newline at end of file