From e4783ca9e2a24e53fe017ef6a3c73cce9ccde77e Mon Sep 17 00:00:00 2001 From: darksnake Date: Fri, 1 Apr 2022 19:57:32 +0300 Subject: [PATCH] Update 01.04.2022 --- build.gradle.kts | 2 +- src/main/kotlin/demos/exceptions.kt | 15 +++++++++ src/main/kotlin/demos/fileIO.kt | 22 ++++++++++++++ src/main/kotlin/demos/integration.kt | 28 +++++++++++++++++ src/main/kotlin/demos/performance.kt | 9 ++++++ src/main/kotlin/demos/primitives.kt | 9 ++++++ src/main/kotlin/lesson1/idiom2.kt | 3 ++ src/main/kotlin/lesson2/idiom13.kt | 3 +- src/main/kotlin/lesson2/idiom14-15.kt | 9 +++++- src/main/kotlin/lesson2/idiom6.kt | 2 +- src/main/kotlin/lesson2/idiom9.kt | 4 +-- src/main/kotlin/lesson3/idiom16.kt | 2 +- src/main/kotlin/lesson3/idiom17.kt | 1 + src/main/kotlin/lesson3/idiom18-19.kt | 10 +++--- src/main/kotlin/lesson3/idiom20-21.kt | 14 ++++++++- src/main/kotlin/lesson3/idiom22.kt | 3 ++ src/main/kotlin/lesson3/idiom23-24.kt | 44 ++++++++++++++++++++++++--- src/main/kotlin/lesson3/idiom25.kt | 1 + src/main/kotlin/lesson3/idiom26.kt | 4 ++- 19 files changed, 168 insertions(+), 17 deletions(-) create mode 100644 src/main/kotlin/demos/exceptions.kt create mode 100644 src/main/kotlin/demos/fileIO.kt create mode 100644 src/main/kotlin/demos/integration.kt create mode 100644 src/main/kotlin/demos/performance.kt create mode 100644 src/main/kotlin/demos/primitives.kt diff --git a/build.gradle.kts b/build.gradle.kts index 31f6ed0..9f3ad0a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,5 +1,5 @@ plugins { - kotlin("jvm") version "1.4.32" + kotlin("jvm") version "1.6.10" } group = "ru.mipt.npm" diff --git a/src/main/kotlin/demos/exceptions.kt b/src/main/kotlin/demos/exceptions.kt new file mode 100644 index 0000000..2b8e583 --- /dev/null +++ b/src/main/kotlin/demos/exceptions.kt @@ -0,0 +1,15 @@ +package demos + +import java.io.IOException + +fun main() { + try { + val b: Nothing = throw RuntimeException("Oops") + } catch (t: IOException){ + println(t.message + "_io") + } catch (t: RuntimeException){ + println(t.message) + } finally { + println("caught") + } +} \ No newline at end of file diff --git a/src/main/kotlin/demos/fileIO.kt b/src/main/kotlin/demos/fileIO.kt new file mode 100644 index 0000000..ec9ade8 --- /dev/null +++ b/src/main/kotlin/demos/fileIO.kt @@ -0,0 +1,22 @@ +package demos + +import java.nio.file.Files +import java.nio.file.Path +import kotlin.io.path.readLines +import kotlin.io.path.useLines +import kotlin.io.path.writeLines + +fun main() { + val myFile: Path = Files.createTempFile("ks_demo", ".txt") + myFile.writeLines((0..1000).map { it.toString() }) + + println(myFile) + + val sum: Double = myFile.useLines { + it.drop(10).take(10).map { line -> + line.toDouble() + }.sum() + } + + println(sum) +} \ No newline at end of file diff --git a/src/main/kotlin/demos/integration.kt b/src/main/kotlin/demos/integration.kt new file mode 100644 index 0000000..87cad11 --- /dev/null +++ b/src/main/kotlin/demos/integration.kt @@ -0,0 +1,28 @@ +package demos + +import kotlin.math.PI +import kotlin.math.cos +import kotlin.math.pow +import kotlin.math.sin + +fun integrate(range: ClosedRange, nPoints: Int = 100, f: (Double) -> Double): Double { + val points = DoubleArray(nPoints) { i -> + range.start + i * (range.endInclusive - range.start) / (nPoints - 1) + } + + return points.sumOf { f(it) } * (range.endInclusive - range.start) / nPoints +} + +fun ((Double) -> Double).integrate( + range: ClosedRange, + nPoints: Int = 100, +): Double = integrate(range, nPoints, this) + +fun main() { + val res = integrate(0.0..PI, 500) { x -> sin(x) + cos(x) * x.pow(2) } + println(res) + + val f = { x: Double -> sin(x) + cos(x) * x.pow(2) } + + val res2 = f.integrate(0.0..PI) +} \ No newline at end of file diff --git a/src/main/kotlin/demos/performance.kt b/src/main/kotlin/demos/performance.kt new file mode 100644 index 0000000..0181678 --- /dev/null +++ b/src/main/kotlin/demos/performance.kt @@ -0,0 +1,9 @@ +package demos + +fun function(array: DoubleArray): DoubleArray { + return DoubleArray(array.size) { i -> array[i] + 1 } +} + +fun main() { + println(listOf(1, 2, 3).asSequence().map { it * it }.map { it + 1 }.toList()) +} \ No newline at end of file diff --git a/src/main/kotlin/demos/primitives.kt b/src/main/kotlin/demos/primitives.kt new file mode 100644 index 0000000..ed77282 --- /dev/null +++ b/src/main/kotlin/demos/primitives.kt @@ -0,0 +1,9 @@ +package demos + +fun main() { + val array = doubleArrayOf(1.0, 2.0, 3.0) + val b: Double? = 1.0 + val newArray: DoubleArray = DoubleArray(array.size) { + array[it] + (b ?: 0.0) + } +} \ No newline at end of file diff --git a/src/main/kotlin/lesson1/idiom2.kt b/src/main/kotlin/lesson1/idiom2.kt index f7053c3..7ad4c40 100644 --- a/src/main/kotlin/lesson1/idiom2.kt +++ b/src/main/kotlin/lesson1/idiom2.kt @@ -18,4 +18,7 @@ fun main() { /* recommended */ val sum = (0..20).sum() + + val intArray: IntArray = IntArray(20){ it } + intArray.sum() } \ No newline at end of file diff --git a/src/main/kotlin/lesson2/idiom13.kt b/src/main/kotlin/lesson2/idiom13.kt index dff6625..502c2a8 100644 --- a/src/main/kotlin/lesson2/idiom13.kt +++ b/src/main/kotlin/lesson2/idiom13.kt @@ -3,9 +3,10 @@ package lesson2 fun printNotNull(any: Any) = println(any) val value: Int? = 2 +//val value: Int? by lazy{ 2 } fun main() { - + //printNotNull(value) // Error if (value != null) { //not guaranteed to work with mutable variable printNotNull(value) diff --git a/src/main/kotlin/lesson2/idiom14-15.kt b/src/main/kotlin/lesson2/idiom14-15.kt index a7255b6..80c7c92 100644 --- a/src/main/kotlin/lesson2/idiom14-15.kt +++ b/src/main/kotlin/lesson2/idiom14-15.kt @@ -8,9 +8,16 @@ fun String.countOs(): Int = count { it == 'о' } // implicit this points to Stri fun main() { fun Int.printMe() = println(this) // explicit this - "обороноспособность".countOs().printMe() + "вылысыпыдыстычка".countOs().printMe() + +// listOf(1, 2, 3).odd +// listOf(1, 2.0, 3).odd } +//fun doSmth(){ +// "вылысыпыдыстычка".countOs().printMe() +//} + /** * Extension property (must be virtual) */ diff --git a/src/main/kotlin/lesson2/idiom6.kt b/src/main/kotlin/lesson2/idiom6.kt index 9cb4a61..a867ec7 100644 --- a/src/main/kotlin/lesson2/idiom6.kt +++ b/src/main/kotlin/lesson2/idiom6.kt @@ -27,7 +27,7 @@ fun main() { /** * Using singleton reference without constructor invocation */ - val obj = AnObject + val obj: AnInterface = AnObject /** * Anonymous object diff --git a/src/main/kotlin/lesson2/idiom9.kt b/src/main/kotlin/lesson2/idiom9.kt index 72c66cc..c67a09e 100644 --- a/src/main/kotlin/lesson2/idiom9.kt +++ b/src/main/kotlin/lesson2/idiom9.kt @@ -16,8 +16,8 @@ fun findSomething(): Int { fun checkCondition(): Int { fun conditionSatisfied() = false - if (conditionSatisfied()) { - return 1 + return if (conditionSatisfied()) { + 1 } else { //error is Nothing error("Condition is not satisfied") diff --git a/src/main/kotlin/lesson3/idiom16.kt b/src/main/kotlin/lesson3/idiom16.kt index 334aa96..033e652 100644 --- a/src/main/kotlin/lesson3/idiom16.kt +++ b/src/main/kotlin/lesson3/idiom16.kt @@ -28,7 +28,7 @@ fun main() { println("a = $a, b= $b, c = $c") } - //Using `let` to compose result. Not recommended to use without a need + //Using `let` to compose result. Not recommended using without a need val letResult = getAClass()?.let { arg -> arg.c + arg.a } diff --git a/src/main/kotlin/lesson3/idiom17.kt b/src/main/kotlin/lesson3/idiom17.kt index 333893e..96b5730 100644 --- a/src/main/kotlin/lesson3/idiom17.kt +++ b/src/main/kotlin/lesson3/idiom17.kt @@ -18,6 +18,7 @@ fun main() { fun getAndIncrement() = i.also { i += 1 } println(getAndIncrement()) + println(i) /** * 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 69c7749..16f8979 100644 --- a/src/main/kotlin/lesson3/idiom18-19.kt +++ b/src/main/kotlin/lesson3/idiom18-19.kt @@ -1,5 +1,7 @@ package lesson3 +import java.util.ArrayList + /** * Lists and mutable lists */ @@ -14,7 +16,7 @@ fun main() { /** * This one creates a mutable list */ - val mutableList = mutableListOf("a", "b", "c") + val mutableList: MutableList = mutableListOf("a", "b", "c") mutableList[2] = "d" mutableList.add("e") mutableList += "f" @@ -23,17 +25,17 @@ fun main() { /** * This one creates a mutable ArrayList. */ - val arrayList = arrayListOf("a", "b", "c") + val arrayList: ArrayList = arrayListOf("a", "b", "c") //Danger zone - val newList = list + "f" + mutableList + val newList: List = list + "f" + mutableList println(newList) //Bonus - val lambdaList = List(3){it.toString()} + 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 2100e3b..74b5ae8 100644 --- a/src/main/kotlin/lesson3/idiom20-21.kt +++ b/src/main/kotlin/lesson3/idiom20-21.kt @@ -1,17 +1,25 @@ package lesson3 fun main() { - val map = HashMap() + val map = mutableMapOf( + "key" to "a", + "key2" to "b", + ) //The map could be accessed via indexing operation println(map["key"]) map["key"] = "fff" + //val entry: MutableMap.MutableEntry = map.iterator().next() + /** * The destructuring declaration for maps and other objects that support `operator fun componentN` */ for ((k, v) in map) { + //val (k, v) = entry +// val k = entry.component1() +// val v = entry.component2() println("$k -> $v") } @@ -22,4 +30,8 @@ fun main() { val coord = doubleArrayOf(0.0, 1.0, 2.0) val (x,y,z) = coord + + data class Coordinate(val x: Double, val y: Int) + + val (x1, y1) = Coordinate(1.0,2) } \ No newline at end of file diff --git a/src/main/kotlin/lesson3/idiom22.kt b/src/main/kotlin/lesson3/idiom22.kt index 69b0f90..932b2c7 100644 --- a/src/main/kotlin/lesson3/idiom22.kt +++ b/src/main/kotlin/lesson3/idiom22.kt @@ -1,6 +1,9 @@ package lesson3 class AClassWithList{ + var b: Double = 2.0 + private set + private val _list = ArrayList() val list: List get() = _list } diff --git a/src/main/kotlin/lesson3/idiom23-24.kt b/src/main/kotlin/lesson3/idiom23-24.kt index 71bbaba..d970a81 100644 --- a/src/main/kotlin/lesson3/idiom23-24.kt +++ b/src/main/kotlin/lesson3/idiom23-24.kt @@ -1,5 +1,11 @@ package lesson3 +import java.time.Instant + +class DateRange(val start: Instant, val end: Instant) + +operator fun DateRange.contains(value: Instant): Boolean = value > start && value < end + fun main() { val emailsList = emptyList() @@ -15,11 +21,41 @@ fun main() { println("not in list") } + println(Instant.now() in DateRange(Instant.EPOCH, Instant.MAX)) + // 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) } + for (i in 1..100) { + println(i) + } // closed range: includes 100 + + (1..100).forEach { i -> + println(i) + } //the same, but with boxing + + 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) + } + + infix fun ClosedRange.step(step: Double): Sequence { + //TODO check arguments + var current = start + return sequence { + do { + yield(current) + current += step + } while (current <= endInclusive) + } + } + + for (x in 0.0..10.0 step 0.5){ + println(x) + } } \ No newline at end of file diff --git a/src/main/kotlin/lesson3/idiom25.kt b/src/main/kotlin/lesson3/idiom25.kt index 9845739..092f7ae 100644 --- a/src/main/kotlin/lesson3/idiom25.kt +++ b/src/main/kotlin/lesson3/idiom25.kt @@ -26,4 +26,5 @@ fun main() { } integrate(0.0, PI) { sin(it) } integrate(0.0, PI, step = 0.02) { sin(it) } + //integrate(0.0, step = 0.02, PI) { sin(it) } } \ No newline at end of file diff --git a/src/main/kotlin/lesson3/idiom26.kt b/src/main/kotlin/lesson3/idiom26.kt index 2da1190..618c970 100644 --- a/src/main/kotlin/lesson3/idiom26.kt +++ b/src/main/kotlin/lesson3/idiom26.kt @@ -6,7 +6,9 @@ fun main() { 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 + //.onEach { println(it) } + //.sumOf { it } //use one of reduce operations + .reduce { acc, i -> acc + i } println(result) } \ No newline at end of file