From 6631dba59b5356b8d6377f4aa54363ad5a424618 Mon Sep 17 00:00:00 2001 From: darksnake Date: Fri, 2 Apr 2021 18:54:42 +0300 Subject: [PATCH] lesson 3 update --- src/main/kotlin/lesson3/idiom16.kt | 12 +++++++----- src/main/kotlin/lesson3/idiom17.kt | 2 ++ src/main/kotlin/lesson3/idiom18-19.kt | 3 +++ src/main/kotlin/lesson3/idiom20-21.kt | 8 +++++++- src/main/kotlin/lesson3/idiom22.kt | 2 +- src/main/kotlin/lesson3/idiom25.kt | 14 ++++++++++---- 6 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/main/kotlin/lesson3/idiom16.kt b/src/main/kotlin/lesson3/idiom16.kt index cb505b3..334aa96 100644 --- a/src/main/kotlin/lesson3/idiom16.kt +++ b/src/main/kotlin/lesson3/idiom16.kt @@ -6,6 +6,8 @@ object AClass{ val c = "c" } +fun getAClass(): AClass? = null + /** * [with]/[run]/[let] are used to create a scope with given argument or receiver */ @@ -14,20 +16,20 @@ fun main() { println("a = ${AClass.a}, b= ${AClass.b}, c = ${AClass.c}") // Using `with` - with(AClass){ + val res = with(AClass){ // AClass type is the receiver in this scope - println("a = $a, b= $b, c = $c") + println("a = $a, b = $b, c = $c") return@with "some value" } //using `run` - AClass.run { + getAClass()?.run { // the same as above println("a = $a, b= $b, c = $c") } //Using `let` to compose result. Not recommended to use without a need - val letResult = AClass.let { - it.c + it.a + val letResult = getAClass()?.let { arg -> + arg.c + arg.a } } \ No newline at end of file diff --git a/src/main/kotlin/lesson3/idiom17.kt b/src/main/kotlin/lesson3/idiom17.kt index 895d15f..333893e 100644 --- a/src/main/kotlin/lesson3/idiom17.kt +++ b/src/main/kotlin/lesson3/idiom17.kt @@ -17,6 +17,8 @@ fun main() { */ fun getAndIncrement() = i.also { i += 1 } + println(getAndIncrement()) + /** * Configure properties of an object (apply) * https://kotlinlang.org/docs/idioms.html#configure-properties-of-an-object-apply diff --git a/src/main/kotlin/lesson3/idiom18-19.kt b/src/main/kotlin/lesson3/idiom18-19.kt index 5551e87..69c7749 100644 --- a/src/main/kotlin/lesson3/idiom18-19.kt +++ b/src/main/kotlin/lesson3/idiom18-19.kt @@ -29,9 +29,12 @@ fun main() { val newList = list + "f" + mutableList + println(newList) + //Bonus val lambdaList = List(3){it.toString()} + println(lambdaList) } /** diff --git a/src/main/kotlin/lesson3/idiom20-21.kt b/src/main/kotlin/lesson3/idiom20-21.kt index d8c3fd5..2100e3b 100644 --- a/src/main/kotlin/lesson3/idiom20-21.kt +++ b/src/main/kotlin/lesson3/idiom20-21.kt @@ -15,5 +15,11 @@ fun main() { println("$k -> $v") } - val (a,b) = Pair(1, 2) + map.forEach { (k, v) -> println("$k -> $v")} + + val (a, b) = Pair(1, 2) + + val coord = doubleArrayOf(0.0, 1.0, 2.0) + + val (x,y,z) = coord } \ No newline at end of file diff --git a/src/main/kotlin/lesson3/idiom22.kt b/src/main/kotlin/lesson3/idiom22.kt index dc6e1db..69b0f90 100644 --- a/src/main/kotlin/lesson3/idiom22.kt +++ b/src/main/kotlin/lesson3/idiom22.kt @@ -7,5 +7,5 @@ class AClassWithList{ fun main() { val obj = AClassWithList() - + obj.list } \ No newline at end of file diff --git a/src/main/kotlin/lesson3/idiom25.kt b/src/main/kotlin/lesson3/idiom25.kt index 46bc743..9845739 100644 --- a/src/main/kotlin/lesson3/idiom25.kt +++ b/src/main/kotlin/lesson3/idiom25.kt @@ -1,13 +1,17 @@ -@file:JvmName("Idiom26Kt") - package lesson3 import kotlin.math.PI import kotlin.math.sin fun main() { - fun integrate(from: Double = 0.0, to: Double = 1.0, step: Double = 0.01, function: (Double) -> Double): Double { + fun integrate( + from: Double = 0.0, + to: Double = 1.0, + step: Double = 0.01, + function: (Double) -> Double, + ): Double { require(to > from) + require(step > 0) var sum = 0.0 var pos = from while (pos < to) { @@ -17,7 +21,9 @@ fun main() { return sum } - integrate { x -> x * x + 2 * x + 1 } + integrate { x -> + x * x + 2 * x + 1 + } integrate(0.0, PI) { sin(it) } integrate(0.0, PI, step = 0.02) { sin(it) } } \ No newline at end of file