Skip to content

Commit a8a205c

Browse files
p7novAlexanderPrendota
authored andcommitted
docs: revised Stdlib (Kotlin#54)
1 parent 8941450 commit a8a205c

16 files changed

+147
-118
lines changed
Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,65 @@
11
# let
22

3-
**let** is a useful function defined in Kotlin Standard Library. It can be used for scoping and null-checks.
3+
`let` function allows you to call a specified functional block on an object. This function is useful for scoping and null-checks.
44

55
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
66

77
```kotlin
8-
var i = 0
9-
fun funWithSideEffect(): String = ".".repeat(i++)
10-
11-
fun getNullableString0(): String? = null
12-
fun getNullableString1(): String? = null
13-
148
fun main() {
15-
//sampleStart
16-
val r = funWithSideEffect().let {
17-
it.isEmpty() || it.contains("Kotlin") // 1
18-
}
19-
println("r = $r")
209

10+
fun checkStringContent(ns: String?) {
11+
println("for \"$ns\":")
12+
13+
ns.let { // 1
14+
println("\tcontent: " + it) // 2
15+
}
16+
}
17+
18+
checkStringContent(null)
19+
checkStringContent("")
20+
checkStringContent("some string with Kotlin")
21+
2122
fun checkNullableString(ns: String?) {
2223
println("for \"$ns\":")
2324

24-
ns?.let { // 2
25-
println("\tis empty? " + it.isEmpty())
25+
ns?.let { // 3
26+
println("\tis empty? " + it.isEmpty())
2627
println("\tcontains Kotlin? " + it.contains("Kotlin"))
2728
}
2829
}
2930
checkNullableString(null)
3031
checkNullableString("")
3132
checkNullableString("some string with Kotlin")
32-
//sampleEnd
3333
}
34+
3435
```
3536

3637
</div>
3738

39+
1. Defines a code block to execute on `ns`.
40+
2. Inside the curly braces `ns` is accessible by the name `it`.
41+
3. Uses a null-safe call, so `let` and its code block are executed only on non-null values.
42+
43+
`let` can also be called on a result of another function execution:
44+
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
45+
46+
```kotlin
47+
fun getStringOfLength(l: Int) = ".".repeat(l)
48+
49+
fun main() {
50+
fun checkString(l: Int) {
51+
val empty = getStringOfLength(l).let {
52+
println("\"$it\" is empty: ${it.isEmpty()} ") // 1
53+
54+
}
55+
}
56+
57+
checkString(0)
58+
checkString(1)
59+
}
60+
61+
```
62+
63+
</div>
3864

39-
1. Result of `funWithSideEffect` is now accessible by reference `it`.
40-
2. Use safe call, so `let` and its code block will be executed only with non-null value.
65+
1. Here `it` is the result of the `getStringOfLength(l)` call.

examples/05_stdlib/02_withFunction.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# with
22

3-
*with* is a function defined in the standard library that can be used to access members of an object in a more concise way, avoiding having to
4-
prefix each member with the instance name.
3+
*with* function lets you access members of an object in a short and concise way. Inside the `with` block you can refer to object members without specifying its name as a prefix.
54

65
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
76

examples/05_stdlib/03_filter.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# filter
22

3-
*filter* is a function defined in the standard library that can be used to filter collection. It takes a predicate as an lambda-parameters.
4-
3+
*filter* function enables you to filter collections. It takes a filter predicate as a lambda-parameter. The predicate is applied to each element. Elements that make the predicate `true` are returned in the result collection.
54
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
65

76
```kotlin
@@ -23,6 +22,6 @@ fun main() {
2322

2423
</div>
2524

26-
1. Define collection of numbers.
27-
2. Filter negative numbers.
28-
3. Or using even shorter way to filter positive numbers.
25+
1. Defines collection of numbers.
26+
2. Gets positive numbers.
27+
3. Uses the shorter `it` notation to get negative numbers.

examples/05_stdlib/04_map.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# map
22

3-
*map* is an extension function defined in the standard library that can be used to transform collection into another collection. It takes a transformer as an lambda-parameters.
3+
*map* extension function enables you to apply a transformation to all elements in a collection. It takes a transformer function as a lambda-parameter.
44

55
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
66

@@ -23,6 +23,6 @@ fun main() {
2323

2424
</div>
2525

26-
1. Define collection of numbers.
27-
2. Double numbers.
28-
3. Or using even shorter way to triple numbers.
26+
1. Defines a collection of numbers.
27+
2. Doubles numbers.
28+
3. Uses the shorter `it` notation to triple the numbers.

examples/05_stdlib/05_existential.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# any, all, none
22

3-
This extension functions answer the question about existence element(s) in collection based on given predicate.
3+
These functions check the existence of collection elements that match a given predicate.
44

55
### Function `any`
66

7-
Function `any` returns `true` if collection contains at least one element matching the given predicate.
7+
Function `any` returns `true` if the collection contains **at least one** element that matches the given predicate.
88

99
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
1010

@@ -27,14 +27,14 @@ fun main() {
2727

2828
</div>
2929

30-
1. Define collection of numbers.
31-
2. Whether there is an element less than `0`.
32-
3. Whether there is an element greater than `6`.
30+
1. Defines a collection of numbers.
31+
2. Checks if there are negative elements.
32+
3. Checks if there are elements greater than `6`.
3333

3434

3535
### Function `all`
3636

37-
Function `all` returns `true` iff all elements in collection matching the given predicate.
37+
Function `all` returns `true` if **all** elements in collection match the given predicate.
3838

3939
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
4040

@@ -57,14 +57,14 @@ fun main() {
5757

5858
</div>
5959

60-
1. Define collection of numbers.
61-
2. Whether all elements are even.
62-
3. Whether all elements less than `6`.
60+
1. Defines a collection of numbers.
61+
2. Checks whether all elements are even.
62+
3. Checks whether all elements are less than `6`.
6363

6464

6565
### Function `none`
6666

67-
Function `none` return `true` iff there is no element in collection matching the given predicate.
67+
Function `none` returns `true` if there are **no** elements that match the given predicate in the collection.
6868

6969
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
7070

@@ -87,7 +87,7 @@ fun main() {
8787

8888
</div>
8989

90-
1. Define collection of numbers.
91-
2. Whether there is no odd element which means all elements are even.
92-
3. If there is no element grater than 6.
90+
1. Defines a collection of numbers.
91+
2. Checks if there are no odd elements (all elements are even).
92+
3. Checks if there are no elements greater than 6.
9393

examples/05_stdlib/06_find.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
# find
1+
# find, findLast
2+
3+
`find` and `findLast` functions return the first or the last collection element that matches the given predicate. If there are no such elements, functions return `null`.
24

3-
Returns the first or last element matching the given predicate, or `null` if no such element was found.
45

56
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
67

@@ -24,7 +25,7 @@ fun main() {
2425

2526
</div>
2627

27-
1. Define collection of different words.
28-
2. Looking for the first word starting with "some".
29-
3. Looking for the last word starting with "some".
30-
4. Looking for the first word containing "nothing".
28+
1. Defines a collection of words.
29+
2. Looks for the first word starting with "some".
30+
3. Looks for the last word starting with "some".
31+
4. Looks for the first word containing "nothing".

examples/05_stdlib/07_firstlast.md

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22

33
### `first`, `last`
44

5-
Those functions return either the first or last element of the collection or the first/last matching the given predicate.
5+
These functions return the first and the last element of the collection correspondingly. You can also use them with a predicate; in this case, they return the first or the last element that matches the given predicate.
66

7-
In case of empty collection or if nothing matching the predicate those function throw an `NoSuchElementException`.
8-
9-
Lets make some Kotlin!
7+
If a collection is empty or doesn't contain elements matching the predicate, the functions throw `NoSuchElementException`.
108

119
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
1210

@@ -30,17 +28,15 @@ fun main() {
3028

3129
</div>
3230

33-
1. Define collection of numbers.
34-
2. Pick first element.
35-
3. Pick last element.
36-
4. Pick first even element.
37-
5. Pick last odd element.
38-
31+
1. Defines a collection of numbers.
32+
2. Picks the first element.
33+
3. Picks the last element.
34+
4. Picks the first even element.
35+
5. Picks the last odd element.
3936

4037
### `firstOrNull`, `lastOrNull`
4138

42-
Behaviour is almost the same instead if nothing was found the null is returned.
43-
39+
These functions work almost the same way with one difference: they return `null` if there are no matching elements.
4440
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
4541

4642
```kotlin
@@ -67,11 +63,11 @@ fun main() {
6763

6864
</div>
6965

70-
1. Define collection of different words.
71-
2. Define an empty collection.
72-
3. Pick the first element from empty collection. It supposed to be `null`
73-
4. Pick the last element from empty collection. It supposed to be `null` as well.
74-
5. Pick the first word starting with 'f'
75-
6. Pick the first word starting with 'z'
76-
7. Pick the last word ending with 'f'
77-
8. Pick the last word ending with 'z'
66+
1. Defines a collection of words.
67+
2. Defines an empty collection.
68+
3. Picks the first element from empty collection. It supposed to be `null`.
69+
4. Picks the last element from empty collection. It supposed to be `null` as well.
70+
5. Picks the first word starting with 'f'.
71+
6. Picks the first word starting with 'z'.
72+
7. Picks the last word ending with 'f'.
73+
8. Picks the last word ending with 'z'.

examples/05_stdlib/08_count.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# count
22

3-
The extension function `count` returns either the total number of elements in collection or the number of elements matching the given predicate.
3+
`count` functions returns either the total number of elements in a collection or the number of elements matching the given predicate.
44

55
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
66

@@ -21,6 +21,6 @@ fun main() {
2121

2222
</div>
2323

24-
1. Define collection of numbers.
25-
2. Count total number of elements.
26-
3. Count number of even elements.
24+
1. Defines a collection of numbers.
25+
2. Counts the total number of elements.
26+
3. Counts the number of even elements.

examples/05_stdlib/09_partition.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# partition
22

3-
This function splits the original collection into pair of lists, where first list contains elements for which predicate yielded `true`, while second list contains elements for which predicate yielded `false`.
3+
`partition` function splits the original collection into pair of lists using a given predicate:
4+
* Elements for which the predicate is `true`.
5+
* Elements for which the predicate is `false`.
46

57
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
68

@@ -10,16 +12,21 @@ fun main() {
1012
//sampleStart
1113
val numbers = listOf(1, -2, 3, -4, 5, -6) // 1
1214

13-
val (postives, negatives) = numbers.partition { it > 0 } // 2
15+
val evenOdd = numbers.partition { it % 2 == 0 } // 2
16+
val (positives, negatives) = numbers.partition { it > 0 } // 3
1417
//sampleEnd
1518

1619
println("Numbers: $numbers")
17-
println("Positive numbers: $postives")
20+
println("Odd numbers: ${evenOdd.first}")
21+
println("Odd numbers: ${evenOdd.second}")
22+
println("Positive numbers: $positives")
1823
println("Negative numbers: $negatives")
1924
}
25+
2026
```
2127

2228
</div>
2329

24-
1. Define collection of numbers.
25-
2. Use `partition` to split `numbers` into two lists with positive and negative numbers. Also apply destructuring to get members of returning `Pair`
30+
1. Defines a collection of numbers.
31+
2. Splits `numbers` into a `Pair` of lists with even and odd numbers.
32+
3. Splits `numbers` into two lists with positive and negative numbers. Pair destructuring is applied here to get the `Pair` members.

examples/05_stdlib/10_associateBy.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# associateBy, groupBy
22

3-
Both functions return a `Map` containing the elements from the given collection indexed by the key returned from `keySelector` function applied to each element.
4-
If `valueSelector` is also passed it is applied to each element as well.
3+
Functions `associateBy` and `groupBy` build maps from the elements of a collection indexed by the specified key. The key is defined in the `keySelector` parameter.
4+
You can also specify an optional `valueSelector` to define what will be stored in the `value` of the map element.
55

6-
The difference between `associateBy` and `groupBy` is the behaviour for values with the same key. `associateBy` takes only last element when `groupBy` takes all elements into collection.
6+
The difference between `associateBy` and `groupBy` is how they process objects with the same key:
7+
* `associateBy` uses the last suitable element as the value.
8+
* `groupBy` builds a list of all suitable elements and puts it in the value.
79

810
The returned map preserves the entry iteration order of the original collection.
911

@@ -37,8 +39,8 @@ fun main() {
3739

3840
</div>
3941

40-
1. Define data class descirbes a Person.
41-
2. Define collection of known people.
42-
3. Build a map from person's phone number to person information.
43-
4. Build another map from phone number to city where owner lives.
44-
5. Build the third map which contains cities and people living there.
42+
1. Defines a data class that descirbes a person.
43+
2. Defines a collection of people.
44+
3. Builds a map from phone numbers to their owners' information. `it.phone` is the `keySelector` here. The `valueSelector` is not provided, so the values of the map are `Person` objects themselves.
45+
4. Builds a map from phone numbers to the cities where owners live. `Person::city` is the `valueSelector` here, so the values of the map contain only cities.
46+
5. Builds a map that contains cities and people living there. The values of the map are lists of person names.

0 commit comments

Comments
 (0)