lesson4
This commit is contained in:
parent
6631dba59b
commit
868cbc0f50
@ -1,5 +1,5 @@
|
||||
plugins {
|
||||
kotlin("jvm") version "1.4.31"
|
||||
kotlin("jvm") version "1.4.32"
|
||||
}
|
||||
|
||||
group = "ru.mipt.npm"
|
||||
|
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@ -1,5 +1,5 @@
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
|
34
src/main/kotlin/lesson4/Idiom31.kt
Normal file
34
src/main/kotlin/lesson4/Idiom31.kt
Normal file
@ -0,0 +1,34 @@
|
||||
package lesson4
|
||||
|
||||
class Bad{
|
||||
val value: Int
|
||||
|
||||
init {
|
||||
value = requestValue()
|
||||
}
|
||||
|
||||
private fun requestValue(): Int = TODO()
|
||||
}
|
||||
|
||||
|
||||
//Factory functions are preferred to the initialization logic
|
||||
|
||||
class Good internal constructor(val value: Int){
|
||||
init {
|
||||
//Initialization block is there to check arguments
|
||||
require(value >= 0)
|
||||
}
|
||||
|
||||
companion object
|
||||
}
|
||||
|
||||
private fun requestValue(): Int = TODO()
|
||||
|
||||
// This is the factory-function
|
||||
fun Good() = Good(requestValue())
|
||||
|
||||
|
||||
// additional constructor-like builders could be added to the companion
|
||||
|
||||
@OptIn(ExperimentalUnsignedTypes::class)
|
||||
fun Good.Companion.build(value: UInt) = Good(value.toInt())
|
18
src/main/kotlin/lesson4/idiom27.kt
Normal file
18
src/main/kotlin/lesson4/idiom27.kt
Normal file
@ -0,0 +1,18 @@
|
||||
package lesson4
|
||||
|
||||
import kotlin.random.Random
|
||||
|
||||
//TODO move to beginning of the course
|
||||
fun main() {
|
||||
val b = Random.nextBoolean()
|
||||
// assign value to an expression
|
||||
val a = if (b) 0 else 1
|
||||
|
||||
//Use Try/catch as an exception
|
||||
val result = try {
|
||||
"22a".toInt()
|
||||
} catch (e: NumberFormatException) {
|
||||
null //Strongly discouraged in performance-critical code
|
||||
}
|
||||
|
||||
}
|
11
src/main/kotlin/lesson4/idiom28.kt
Normal file
11
src/main/kotlin/lesson4/idiom28.kt
Normal file
@ -0,0 +1,11 @@
|
||||
package lesson4
|
||||
|
||||
@OptIn(ExperimentalStdlibApi::class)
|
||||
fun main() {
|
||||
//Consume mutating lambda
|
||||
val list = buildList {
|
||||
repeat(10){
|
||||
add(it)
|
||||
}
|
||||
}
|
||||
}
|
12
src/main/kotlin/lesson4/idiom29.kt
Normal file
12
src/main/kotlin/lesson4/idiom29.kt
Normal file
@ -0,0 +1,12 @@
|
||||
package lesson4
|
||||
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Paths
|
||||
|
||||
fun main() {
|
||||
val stream = Files.newInputStream(Paths.get("/some/file.txt"))
|
||||
// The resource is automatically closed when leaving the scope
|
||||
stream.buffered().reader().use { reader ->
|
||||
println(reader.readText())
|
||||
}
|
||||
}
|
24
src/main/kotlin/lesson4/idiom30.kt
Normal file
24
src/main/kotlin/lesson4/idiom30.kt
Normal file
@ -0,0 +1,24 @@
|
||||
package lesson4
|
||||
|
||||
interface Factory<T : Any> {
|
||||
fun build(str: String): T
|
||||
}
|
||||
|
||||
|
||||
class IntContainer(val arg: Int) {
|
||||
companion object : Factory<IntContainer> {
|
||||
override fun build(str: String) = IntContainer(str.toInt())
|
||||
}
|
||||
}
|
||||
|
||||
class DoubleContainer(val arg: Double) {
|
||||
companion object : Factory<DoubleContainer> {
|
||||
override fun build(str: String) = DoubleContainer(str.toDouble())
|
||||
}
|
||||
}
|
||||
|
||||
fun <T : Any> buildContainer(str: String, factory: Factory<T>): T = factory.build(str)
|
||||
|
||||
fun main() {
|
||||
buildContainer("22", IntContainer)
|
||||
}
|
17
src/main/kotlin/lesson4/idiom32.kt
Normal file
17
src/main/kotlin/lesson4/idiom32.kt
Normal file
@ -0,0 +1,17 @@
|
||||
package lesson4
|
||||
|
||||
class ClassWithALazyProperty{
|
||||
//Use lazy delegate for something that should be calculated ones on first call
|
||||
val lazyValue by lazy {
|
||||
//Do dome heavy logic here
|
||||
}
|
||||
}
|
||||
|
||||
//Using other delegates
|
||||
fun main() {
|
||||
val map = mapOf("a" to 1, "b" to 2)
|
||||
|
||||
val a by map
|
||||
|
||||
println(a)
|
||||
}
|
36
src/main/kotlin/lesson4/idiom33.kt
Normal file
36
src/main/kotlin/lesson4/idiom33.kt
Normal file
@ -0,0 +1,36 @@
|
||||
package lesson4
|
||||
|
||||
/**
|
||||
* The demonstration of use of inline [forEach] function with non-local return
|
||||
*/
|
||||
fun foo() {
|
||||
listOf(1, 2, 3, 4, 5).forEach {
|
||||
if (it == 3) return // non-local return directly to the caller of foo()
|
||||
print(it)
|
||||
}
|
||||
println("this point is unreachable")
|
||||
}
|
||||
|
||||
/**
|
||||
* Definition of inline function
|
||||
*/
|
||||
inline fun List<Int>.forEachOdd(block: (Int) -> Unit) = forEach {
|
||||
if (it % 2 == 1) block(it)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Using inline function for type reification during the compile time
|
||||
*/
|
||||
inline fun <reified T: Any> List<T>.prettyPrint() = forEach {
|
||||
when (T::class) {
|
||||
Double::class -> println("Double: ${it as Double}")
|
||||
Int::class -> println("Int: ${it as Int}")
|
||||
else -> it.toString()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* **WARNING** inline functions are an advanced feature and should be used only for reification or non-local return
|
||||
* NOT for optimization.
|
||||
*/
|
16
src/main/kotlin/lesson4/idiom34.kt
Normal file
16
src/main/kotlin/lesson4/idiom34.kt
Normal file
@ -0,0 +1,16 @@
|
||||
package lesson4
|
||||
|
||||
/**
|
||||
* Values are boxed. Each call is indirect
|
||||
*/
|
||||
val list: List<Double> = List(20){it.toDouble()}
|
||||
|
||||
/**
|
||||
* Still boxed
|
||||
*/
|
||||
val genericArray: Array<Double> = Array(20){it.toDouble()}
|
||||
|
||||
/**
|
||||
* Non-boxed
|
||||
*/
|
||||
val specializedArray: DoubleArray = DoubleArray(20){it.toDouble()}
|
Loading…
Reference in New Issue
Block a user