From 00a60ed91c8c9c2baebac866d24dd9b22331c1c2 Mon Sep 17 00:00:00 2001 From: Julien Lengrand-Lambert Date: Sun, 24 Jul 2022 22:45:08 +0200 Subject: [PATCH] Adds functional and coroutines --- .idea/uiDesigner.xml | 124 +++++++++++++++++++++++++++++++ build.gradle.kts | 4 +- src/main/kotlin/Main.kt | 7 -- src/main/kotlin/async_04.kt | 26 +++++++ src/main/kotlin/functional_03.kt | 26 +++++++ src/main/kotlin/goodies_02.kt | 4 +- 6 files changed, 181 insertions(+), 10 deletions(-) create mode 100644 .idea/uiDesigner.xml delete mode 100644 src/main/kotlin/Main.kt create mode 100644 src/main/kotlin/async_04.kt create mode 100644 src/main/kotlin/functional_03.kt diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index b5becb5..ea7e169 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,7 +1,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { - kotlin("jvm") version "1.7.10" + kotlin("jvm") version "1.6.21" application } @@ -14,6 +14,8 @@ repositories { dependencies { testImplementation(kotlin("test")) + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4") + } tasks.test { diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt deleted file mode 100644 index f2a59b6..0000000 --- a/src/main/kotlin/Main.kt +++ /dev/null @@ -1,7 +0,0 @@ -fun main(args: Array) { - println("Hello World!") - - // Try adding program arguments via Run/Debug configuration. - // Learn more about running applications: https://www.jetbrains.com/help/idea/running-applications.html. - println("Program arguments: ${args.joinToString()}") -} \ No newline at end of file diff --git a/src/main/kotlin/async_04.kt b/src/main/kotlin/async_04.kt new file mode 100644 index 0000000..1180497 --- /dev/null +++ b/src/main/kotlin/async_04.kt @@ -0,0 +1,26 @@ +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch +import kotlinx.coroutines.runBlocking + +fun main(args: Array) = runBlocking { // this: CoroutineScope + val job = launch { // launch a new coroutine and continue + printWorld() + } + println("Hello") // main coroutine continues while a previous one is delayed + job.join() // wait until child coroutine completes + println("Done") +} + +suspend fun printWorld() { + delay(1000L) + println("World!") +} + +fun main2() = runBlocking { + repeat(100_000) { // launch a lot of coroutines + launch { + delay(5000L) + print(".") + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/functional_03.kt b/src/main/kotlin/functional_03.kt new file mode 100644 index 0000000..962923c --- /dev/null +++ b/src/main/kotlin/functional_03.kt @@ -0,0 +1,26 @@ +fun functionalFun(){ + + + val values = listOf(1, 2, 3, 4, 5, 6, 7, 8) +// val positives = values.filter({ x ->x > 5 }) +// val positives = values.filter({ it > 5 }) + val positives = values.filter { it > 5 } + + val fruits = listOf("apple", "apricot", "banana", "blueberry", "cherry", "coconut") + + fruits + .filter { it.startsWith("a") } + .sortedBy { it } + .map { it.uppercase() } + .forEach { println(it) } + + + // Has all of the fun stuff you'd expect + println(values.fold(0) { acc, i -> acc + i }) + + // amazing IDE support +// val hasNegative = values.filter { it > 0 }.size > 0 + + + +} \ No newline at end of file diff --git a/src/main/kotlin/goodies_02.kt b/src/main/kotlin/goodies_02.kt index c7e6670..2a92cd3 100644 --- a/src/main/kotlin/goodies_02.kt +++ b/src/main/kotlin/goodies_02.kt @@ -11,8 +11,8 @@ fun playingWithNulls(){ println(maybeSquare?.length) // bob?.department?.head?.name - val l: Int = if (maybeSquare != null) maybeSquare.length else -1 - val betterl = maybeSquare?.length ?: -1 +// val l: Int = if (maybeSquare != null) maybeSquare.length else -1 +// val betterl = maybeSquare?.length ?: -1 val nullableList: List = listOf(1, 2, null, 4) }