From 35b9fc62f31bd51402da41ad8938577141aef459 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Fri, 5 Mar 2021 10:26:02 +0300 Subject: [PATCH] lesson1 --- src/main/kotlin/lesson1/idiom0.kt | 15 +++++++++++ src/main/kotlin/lesson1/idiom1.kt | 26 ++++++++++++++++++ src/main/kotlin/lesson1/idiom2.kt | 21 +++++++++++++++ src/main/kotlin/lesson1/idiom3-5.kt | 42 +++++++++++++++++++++++++++++ src/main/kotlin/main.kt | 5 ---- 5 files changed, 104 insertions(+), 5 deletions(-) create mode 100644 src/main/kotlin/lesson1/idiom0.kt create mode 100644 src/main/kotlin/lesson1/idiom1.kt create mode 100644 src/main/kotlin/lesson1/idiom2.kt create mode 100644 src/main/kotlin/lesson1/idiom3-5.kt delete mode 100644 src/main/kotlin/main.kt diff --git a/src/main/kotlin/lesson1/idiom0.kt b/src/main/kotlin/lesson1/idiom0.kt new file mode 100644 index 0000000..b7da0ad --- /dev/null +++ b/src/main/kotlin/lesson1/idiom0.kt @@ -0,0 +1,15 @@ +package lesson1 + +/** + * This is the entry point of the program. The body of this method is considered to be the body of the program. + * `suspend` modifier allows to launch the program in asynchronous mode and could be omitted. [args] are used to pass + * console arguments in a desktop application and also could be omitted. + * + * On JVM there could be several entry points that a distinguished by their fully-qualified name with kotlin-to-JVM + * transformation (capitalized + Kt suffix). + * + * In Kts/Jupyter/Worksheet main function is absent. The body of the script plays that role. + */ +suspend fun main(args: Array) { + println("Hello Kotlin from MIPT!") +} \ No newline at end of file diff --git a/src/main/kotlin/lesson1/idiom1.kt b/src/main/kotlin/lesson1/idiom1.kt new file mode 100644 index 0000000..3e5034b --- /dev/null +++ b/src/main/kotlin/lesson1/idiom1.kt @@ -0,0 +1,26 @@ +package lesson1 + +/** + * Interpolated string allow to construct string fast + */ +fun main() { + println("This is a plain string") + + val arg = "Interpolated" + println("This is an $arg string") + + val number = 1 + println("This is an $arg string number ${number + 1}") + + println("This is a string with \$") + + println(""" + This is a + multi + line + raw + string + """.trimIndent()) + + println("""This is a raw string number ${number+1} with \ and ${'$'} """) +} \ No newline at end of file diff --git a/src/main/kotlin/lesson1/idiom2.kt b/src/main/kotlin/lesson1/idiom2.kt new file mode 100644 index 0000000..f7053c3 --- /dev/null +++ b/src/main/kotlin/lesson1/idiom2.kt @@ -0,0 +1,21 @@ +package lesson1 + +/** + * `val` means read-only property + * + * `var` means writeable property + * + * Unnecessary use of `var` is not recommended. + * + * **NOTE:** In performance critical cases explicit cycles are better, but mutable state should be encapsulated. + */ +fun main() { + /* Not recommended */ + var counter = 0 + for(i in 0..20){ + counter += i + } + + /* recommended */ + val sum = (0..20).sum() +} \ No newline at end of file diff --git a/src/main/kotlin/lesson1/idiom3-5.kt b/src/main/kotlin/lesson1/idiom3-5.kt new file mode 100644 index 0000000..3aa42ea --- /dev/null +++ b/src/main/kotlin/lesson1/idiom3-5.kt @@ -0,0 +1,42 @@ +package lesson1 + +//preamble + +/** + * Functions could be defined everywhere in kotlin code. This is a so-called top-level function. + */ +fun doSomething(){ + println("I did it") +} + +object AnObject{ + /** + * This is a member-function + */ + fun doSomethingInAnObject(){ + println("I did it in an object") + } +} + +fun doSomethingSpecial(){ + /** + * This one is inside another function + */ + fun doSomethingInside(){ + println("I did it inside another function") + } + doSomethingInside() +} + +//idiom 3 - Unit +fun returnUnit(): Unit{ + // no return statement `Unit` is returned implicitly +} + +//idiom 4 - default parameters +fun doSomethingWithDefault(a: Int = 0, b: String = ""): String { + return "A string with a == $a and b == $b" +} + +//idiom 5 - function body shortcut +fun theSameAsBefore(a: Int = 0, b: String = ""): String = "A string with a == $a and b == $b" \ No newline at end of file diff --git a/src/main/kotlin/main.kt b/src/main/kotlin/main.kt deleted file mode 100644 index 5322214..0000000 --- a/src/main/kotlin/main.kt +++ /dev/null @@ -1,5 +0,0 @@ -package ru.mipt.npm.ks.demo - -fun main() { - println("Hello Kotlin from MIPT!") -} \ No newline at end of file