lesson3
This commit is contained in:
parent
78141cf98e
commit
967c5b0155
33
src/main/kotlin/lesson3/idiom16.kt
Normal file
33
src/main/kotlin/lesson3/idiom16.kt
Normal file
@ -0,0 +1,33 @@
|
||||
package lesson3
|
||||
|
||||
object AClass{
|
||||
val a = "a"
|
||||
val b = "b"
|
||||
val c = "c"
|
||||
}
|
||||
|
||||
/**
|
||||
* [with]/[run]/[let] are used to create a scope with given argument or receiver
|
||||
*/
|
||||
fun main() {
|
||||
// Simple print of AClass
|
||||
println("a = ${AClass.a}, b= ${AClass.b}, c = ${AClass.c}")
|
||||
|
||||
// Using `with`
|
||||
with(AClass){
|
||||
// AClass type is the receiver in this scope
|
||||
println("a = $a, b= $b, c = $c")
|
||||
return@with "some value"
|
||||
}
|
||||
|
||||
//using `run`
|
||||
AClass.run {
|
||||
// the same as above
|
||||
println("a = $a, b= $b, c = $c")
|
||||
}
|
||||
|
||||
//Using `let` to compose result. Not recommended to use without a need
|
||||
val letResult = AClass.let {
|
||||
it.c + it.a
|
||||
}
|
||||
}
|
29
src/main/kotlin/lesson3/idiom17.kt
Normal file
29
src/main/kotlin/lesson3/idiom17.kt
Normal file
@ -0,0 +1,29 @@
|
||||
package lesson3
|
||||
|
||||
class Rectangle{
|
||||
var length: Number = 0
|
||||
var breadth: Number=0
|
||||
var color: Int = 0xffffff
|
||||
}
|
||||
|
||||
/**
|
||||
* Using apply/also to add a finalizing action
|
||||
*/
|
||||
fun main() {
|
||||
var i = 2
|
||||
|
||||
/**
|
||||
* [also] block does not affect the result
|
||||
*/
|
||||
fun getAndIncrement() = i.also { i += 1 }
|
||||
|
||||
/**
|
||||
* Configure properties of an object (apply)
|
||||
* https://kotlinlang.org/docs/idioms.html#configure-properties-of-an-object-apply
|
||||
*/
|
||||
val myRectangle = Rectangle().apply {
|
||||
length = 4
|
||||
breadth = 5
|
||||
color = 0xFAFAFA
|
||||
}
|
||||
}
|
46
src/main/kotlin/lesson3/idiom18-19.kt
Normal file
46
src/main/kotlin/lesson3/idiom18-19.kt
Normal file
@ -0,0 +1,46 @@
|
||||
package lesson3
|
||||
|
||||
/**
|
||||
* Lists and mutable lists
|
||||
*/
|
||||
fun main() {
|
||||
/**
|
||||
* This method creates a read-only list of strings. One should note that the type of the list is not specified
|
||||
*/
|
||||
val list = listOf("a", "b", "c")
|
||||
|
||||
println(list[0])
|
||||
|
||||
/**
|
||||
* This one creates a mutable list
|
||||
*/
|
||||
val mutableList = mutableListOf("a", "b", "c")
|
||||
mutableList[2] = "d"
|
||||
mutableList.add("e")
|
||||
mutableList += "f"
|
||||
|
||||
|
||||
/**
|
||||
* This one creates a mutable ArrayList.
|
||||
*/
|
||||
val arrayList = arrayListOf("a", "b", "c")
|
||||
|
||||
//Danger zone
|
||||
|
||||
val newList = list + "f" + mutableList
|
||||
|
||||
//Bonus
|
||||
|
||||
val lambdaList = List(3){it.toString()}
|
||||
}
|
||||
|
||||
/**
|
||||
* idiom 19
|
||||
* Use shortcut function to provide default values
|
||||
*/
|
||||
fun doSomething(additionalArguments: List<String> = emptyList()){
|
||||
TODO()
|
||||
emptyArray<String>()
|
||||
emptySet<String>()
|
||||
emptyMap<String,String>()
|
||||
}
|
19
src/main/kotlin/lesson3/idiom20-21.kt
Normal file
19
src/main/kotlin/lesson3/idiom20-21.kt
Normal file
@ -0,0 +1,19 @@
|
||||
package lesson3
|
||||
|
||||
fun main() {
|
||||
val map = HashMap<String,String>()
|
||||
|
||||
//The map could be accessed via indexing operation
|
||||
println(map["key"])
|
||||
map["key"] = "fff"
|
||||
|
||||
|
||||
/**
|
||||
* The destructuring declaration for maps and other objects that support `operator fun componentN`
|
||||
*/
|
||||
for ((k, v) in map) {
|
||||
println("$k -> $v")
|
||||
}
|
||||
|
||||
val (a,b) = Pair(1, 2)
|
||||
}
|
11
src/main/kotlin/lesson3/idiom22.kt
Normal file
11
src/main/kotlin/lesson3/idiom22.kt
Normal file
@ -0,0 +1,11 @@
|
||||
package lesson3
|
||||
|
||||
class AClassWithList{
|
||||
private val _list = ArrayList<Int>()
|
||||
val list: List<Int> get() = _list
|
||||
}
|
||||
|
||||
fun main() {
|
||||
val obj = AClassWithList()
|
||||
|
||||
}
|
25
src/main/kotlin/lesson3/idiom23-24.kt
Normal file
25
src/main/kotlin/lesson3/idiom23-24.kt
Normal file
@ -0,0 +1,25 @@
|
||||
package lesson3
|
||||
|
||||
fun main() {
|
||||
|
||||
val emailsList = emptyList<String>()
|
||||
|
||||
// When used directly infix in operator checks if the element is contained in a collection
|
||||
//using `operator fun contains`
|
||||
|
||||
if ("john@example.com" in emailsList) {
|
||||
println("is in list")
|
||||
}
|
||||
|
||||
if ("jane@example.com" !in emailsList) {
|
||||
println("not in list")
|
||||
}
|
||||
|
||||
// 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) }
|
||||
}
|
23
src/main/kotlin/lesson3/idiom25.kt
Normal file
23
src/main/kotlin/lesson3/idiom25.kt
Normal file
@ -0,0 +1,23 @@
|
||||
@file:JvmName("Idiom26Kt")
|
||||
|
||||
package lesson3
|
||||
|
||||
import kotlin.math.PI
|
||||
import kotlin.math.sin
|
||||
|
||||
fun main() {
|
||||
fun integrate(from: Double = 0.0, to: Double = 1.0, step: Double = 0.01, function: (Double) -> Double): Double {
|
||||
require(to > from)
|
||||
var sum = 0.0
|
||||
var pos = from
|
||||
while (pos < to) {
|
||||
sum += function(pos)
|
||||
pos += step
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
integrate { x -> x * x + 2 * x + 1 }
|
||||
integrate(0.0, PI) { sin(it) }
|
||||
integrate(0.0, PI, step = 0.02) { sin(it) }
|
||||
}
|
12
src/main/kotlin/lesson3/idiom26.kt
Normal file
12
src/main/kotlin/lesson3/idiom26.kt
Normal file
@ -0,0 +1,12 @@
|
||||
package lesson3
|
||||
|
||||
fun main() {
|
||||
val list: List<Int> = listOf(1, 2, 3, 4, 5, 6)
|
||||
|
||||
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
|
||||
|
||||
println(result)
|
||||
}
|
Loading…
Reference in New Issue
Block a user