Skip to content

Commit c591e19

Browse files
chore: clean up examples
1 parent 5f26ec7 commit c591e19

File tree

9 files changed

+98
-265
lines changed

9 files changed

+98
-265
lines changed

examples/01_introduction/01_Hello world.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ also, note that semicolons are optional.
2020

2121
For Kotlin versions less than 1.3 `main` function must be defined with parameter:
2222

23-
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.2">
23+
<div class="language-kotlin" theme="idea">
2424

2525
```kotlin
2626
fun main(args: Array<String>) {

examples/05_special_classes/00_description.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/05_special_classes/01_Data classes.md

Lines changed: 0 additions & 45 deletions
This file was deleted.

examples/05_special_classes/02_Enum.md

Lines changed: 0 additions & 48 deletions
This file was deleted.

examples/05_special_classes/03_Sealed Classes.md

Lines changed: 0 additions & 34 deletions
This file was deleted.

examples/05_special_classes/04_Object.md

Lines changed: 0 additions & 135 deletions
This file was deleted.

examples/08_stdlib/02_withFunction.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ prefix each member with the instance name.
99
class Configuration(var host: String = "", var port: Int = 0, var isSSL: Boolean = false)
1010

1111
fun main() {
12-
1312
val configuration = Configuration()
13+
// TODO rewrite: make conform to code style
1414
with(configuration) {
1515
host = "127.0.0.1"
1616
port = 9000
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Map element access
2+
3+
`[]` operator returns value corresponding to the given key, or `null` if such a key is not present in the map
4+
5+
`getValue` function returns an existing value corresponding to the given key or throws an exception,
6+
mentioning which key was not found.
7+
8+
If the map was produced with withDefault, this function will return the default value instead of throwing an exception.
9+
10+
<div class="language-kotlin" theme="idea">
11+
12+
```kotlin
13+
fun main(args: Array<String>) {
14+
15+
//sampleStart
16+
val map = mapOf("key" to 42)
17+
18+
val value1 = map["key"] // 1
19+
val value2 = map["key2"] // 2
20+
21+
val value3: Int = map.getValue("key") // 1
22+
23+
val mapWithDefault = map.withDefault { k -> k.length }
24+
val value4 = mapWithDefault.getValue("key2") // 3
25+
26+
try {
27+
map.getValue("anotherKey") // 4
28+
}
29+
catch (e: NoSuchElementException) {
30+
println("Message: $e")
31+
}
32+
33+
//sampleEnd
34+
35+
println("value1 is $value1")
36+
println("value2 is $value2")
37+
println("value3 is $value3")
38+
println("value4 is $value4")
39+
}
40+
```
41+
42+
</div>
43+
44+
1. Produces 42 because `"key"` is present in map
45+
2. Produces `null` because `"key2"` is absent.
46+
3. Produces default value of 4 because `"key2"` is absent.
47+
4. Throws NoSuchElementException because `"anotherKey"` is not in the map

examples/08_stdlib/16_getOrElse.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# getOrElse
2+
3+
Provides safe access to elements of collection. It takes index and a function that computes default value
4+
in cases when index is out of bound.
5+
6+
7+
<div class="language-kotlin" theme="idea">
8+
9+
```kotlin
10+
fun main() {
11+
12+
//sampleStart
13+
val list = listOf(0, 10, 20)
14+
println(list.getOrElse(1) { 42 }) // 1
15+
println(list.getOrElse(10) { 42 }) // 2
16+
//sampleEnd
17+
}
18+
```
19+
20+
</div>
21+
22+
1. Prints element at index `1`
23+
2. Prints `42` because index `10` is out of bounds
24+
25+
`getOrElse` can also be applied to `Map` to get value for given key.
26+
27+
<div class="language-kotlin" theme="idea">
28+
29+
```kotlin
30+
fun main() {
31+
32+
//sampleStart
33+
val map = mutableMapOf<String, Int?>()
34+
println(map.getOrElse("x") { 1 }) // 1
35+
36+
map["x"] = 3
37+
println(map.getOrElse("x") { 1 }) // 2
38+
39+
map["x"] = null
40+
println(map.getOrElse("x") { 1 }) // 1
41+
//sampleEnd
42+
}
43+
```
44+
45+
</div>
46+
47+
48+
1. Prints default value because value for key `"x"` is absent
49+
2. Prints `3`, value for key `"x"`

0 commit comments

Comments
 (0)