From 967c5b0155cdfcda6f3d2aee323051228c9dd627 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Fri, 2 Apr 2021 12:16:07 +0300 Subject: [PATCH] lesson3 --- src/main/kotlin/lesson3/idiom16.kt | 33 +++++++++++++++++++ src/main/kotlin/lesson3/idiom17.kt | 29 +++++++++++++++++ src/main/kotlin/lesson3/idiom18-19.kt | 46 +++++++++++++++++++++++++++ src/main/kotlin/lesson3/idiom20-21.kt | 19 +++++++++++ src/main/kotlin/lesson3/idiom22.kt | 11 +++++++ src/main/kotlin/lesson3/idiom23-24.kt | 25 +++++++++++++++ src/main/kotlin/lesson3/idiom25.kt | 23 ++++++++++++++ src/main/kotlin/lesson3/idiom26.kt | 12 +++++++ 8 files changed, 198 insertions(+) create mode 100644 src/main/kotlin/lesson3/idiom16.kt create mode 100644 src/main/kotlin/lesson3/idiom17.kt create mode 100644 src/main/kotlin/lesson3/idiom18-19.kt create mode 100644 src/main/kotlin/lesson3/idiom20-21.kt create mode 100644 src/main/kotlin/lesson3/idiom22.kt create mode 100644 src/main/kotlin/lesson3/idiom23-24.kt create mode 100644 src/main/kotlin/lesson3/idiom25.kt create mode 100644 src/main/kotlin/lesson3/idiom26.kt diff --git a/src/main/kotlin/lesson3/idiom16.kt b/src/main/kotlin/lesson3/idiom16.kt new file mode 100644 index 0000000..cb505b3 --- /dev/null +++ b/src/main/kotlin/lesson3/idiom16.kt @@ -0,0 +1,33 @@ +package lesson3 + +object AClass{ + val a = "a" + val b = "b" + val c = "c" +} + +/** + * [with]/[run]/[let] are used to create a scope with given argument or receiver + */ +fun main() { + // Simple print of AClass + println("a = ${AClass.a}, b= ${AClass.b}, c = ${AClass.c}") + + // Using `with` + with(AClass){ + // AClass type is the receiver in this scope + println("a = $a, b= $b, c = $c") + return@with "some value" + } + + //using `run` + AClass.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 + } +} \ No newline at end of file diff --git a/src/main/kotlin/lesson3/idiom17.kt b/src/main/kotlin/lesson3/idiom17.kt new file mode 100644 index 0000000..895d15f --- /dev/null +++ b/src/main/kotlin/lesson3/idiom17.kt @@ -0,0 +1,29 @@ +package lesson3 + +class Rectangle{ + var length: Number = 0 + var breadth: Number=0 + var color: Int = 0xffffff +} + +/** + * Using apply/also to add a finalizing action + */ +fun main() { + var i = 2 + + /** + * [also] block does not affect the result + */ + fun getAndIncrement() = i.also { i += 1 } + + /** + * Configure properties of an object (apply) + * https://kotlinlang.org/docs/idioms.html#configure-properties-of-an-object-apply + */ + val myRectangle = Rectangle().apply { + length = 4 + breadth = 5 + color = 0xFAFAFA + } +} \ No newline at end of file diff --git a/src/main/kotlin/lesson3/idiom18-19.kt b/src/main/kotlin/lesson3/idiom18-19.kt new file mode 100644 index 0000000..5551e87 --- /dev/null +++ b/src/main/kotlin/lesson3/idiom18-19.kt @@ -0,0 +1,46 @@ +package lesson3 + +/** + * Lists and mutable lists + */ +fun main() { + /** + * This method creates a read-only list of strings. One should note that the type of the list is not specified + */ + val list = listOf("a", "b", "c") + + println(list[0]) + + /** + * This one creates a mutable list + */ + val mutableList = mutableListOf("a", "b", "c") + mutableList[2] = "d" + mutableList.add("e") + mutableList += "f" + + + /** + * This one creates a mutable ArrayList. + */ + val arrayList = arrayListOf("a", "b", "c") + + //Danger zone + + val newList = list + "f" + mutableList + + //Bonus + + val lambdaList = List(3){it.toString()} +} + +/** + * idiom 19 + * Use shortcut function to provide default values + */ +fun doSomething(additionalArguments: List = emptyList()){ + TODO() + emptyArray() + emptySet() + emptyMap() +} \ No newline at end of file diff --git a/src/main/kotlin/lesson3/idiom20-21.kt b/src/main/kotlin/lesson3/idiom20-21.kt new file mode 100644 index 0000000..d8c3fd5 --- /dev/null +++ b/src/main/kotlin/lesson3/idiom20-21.kt @@ -0,0 +1,19 @@ +package lesson3 + +fun main() { + val map = HashMap() + + //The map could be accessed via indexing operation + println(map["key"]) + map["key"] = "fff" + + + /** + * The destructuring declaration for maps and other objects that support `operator fun componentN` + */ + for ((k, v) in map) { + println("$k -> $v") + } + + val (a,b) = Pair(1, 2) +} \ No newline at end of file diff --git a/src/main/kotlin/lesson3/idiom22.kt b/src/main/kotlin/lesson3/idiom22.kt new file mode 100644 index 0000000..dc6e1db --- /dev/null +++ b/src/main/kotlin/lesson3/idiom22.kt @@ -0,0 +1,11 @@ +package lesson3 + +class AClassWithList{ + private val _list = ArrayList() + val list: List get() = _list +} + +fun main() { + val obj = AClassWithList() + +} \ No newline at end of file diff --git a/src/main/kotlin/lesson3/idiom23-24.kt b/src/main/kotlin/lesson3/idiom23-24.kt new file mode 100644 index 0000000..71bbaba --- /dev/null +++ b/src/main/kotlin/lesson3/idiom23-24.kt @@ -0,0 +1,25 @@ +package lesson3 + +fun main() { + + val emailsList = emptyList() + + // When used directly infix in operator checks if the element is contained in a collection + //using `operator fun contains` + + if ("john@example.com" in emailsList) { + println("is in list") + } + + if ("jane@example.com" !in emailsList) { + println("not in list") + } + + // Another (different) use of `in` is iteration over range or collection using + // using `operator fun iterator` + + for (i in 1..100) { println(i) } // closed range: includes 100 + for (i in 1 until 100) { println(i) } // half-open range: does not include 100 + for (x in 2..10 step 2) { println(x) } + for (x in 10 downTo 1) { println(x) } +} \ No newline at end of file diff --git a/src/main/kotlin/lesson3/idiom25.kt b/src/main/kotlin/lesson3/idiom25.kt new file mode 100644 index 0000000..46bc743 --- /dev/null +++ b/src/main/kotlin/lesson3/idiom25.kt @@ -0,0 +1,23 @@ +@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 { + require(to > from) + var sum = 0.0 + var pos = from + while (pos < to) { + sum += function(pos) + pos += step + } + return sum + } + + 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 diff --git a/src/main/kotlin/lesson3/idiom26.kt b/src/main/kotlin/lesson3/idiom26.kt new file mode 100644 index 0000000..2da1190 --- /dev/null +++ b/src/main/kotlin/lesson3/idiom26.kt @@ -0,0 +1,12 @@ +package lesson3 + +fun main() { + val list: List = listOf(1, 2, 3, 4, 5, 6) + + val result = list + .filter { it % 2 == 0 } //select even numbers + .map { it * it } // get square of each element + .sumByDouble { it.toDouble() } //use one of reduce operations + + println(result) +} \ No newline at end of file