lesson1
This commit is contained in:
parent
21c53e699f
commit
35b9fc62f3
15
src/main/kotlin/lesson1/idiom0.kt
Normal file
15
src/main/kotlin/lesson1/idiom0.kt
Normal file
@ -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<String>) {
|
||||
println("Hello Kotlin from MIPT!")
|
||||
}
|
26
src/main/kotlin/lesson1/idiom1.kt
Normal file
26
src/main/kotlin/lesson1/idiom1.kt
Normal file
@ -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 ${'$'} """)
|
||||
}
|
21
src/main/kotlin/lesson1/idiom2.kt
Normal file
21
src/main/kotlin/lesson1/idiom2.kt
Normal file
@ -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()
|
||||
}
|
42
src/main/kotlin/lesson1/idiom3-5.kt
Normal file
42
src/main/kotlin/lesson1/idiom3-5.kt
Normal file
@ -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"
|
@ -1,5 +0,0 @@
|
||||
package ru.mipt.npm.ks.demo
|
||||
|
||||
fun main() {
|
||||
println("Hello Kotlin from MIPT!")
|
||||
}
|
Loading…
Reference in New Issue
Block a user