Add map() documentation sample

* docs: add map() sample
* docs: add samples for Map and CharSequence
* docs: add another sample for Maps.map()
This commit is contained in:
Benjamin Orsini
2019-05-20 18:20:09 +02:00
committed by ilya-g
parent 08ea982688
commit 3e39f26379
10 changed files with 69 additions and 1 deletions
@@ -393,6 +393,12 @@ class Collections {
assertPrints(chars.joinToString(limit = 5, truncated = "...!") { it.toUpperCase().toString() }, "A, B, C, D, E, ...!")
}
@Sample
fun map() {
val numbers = listOf(1, 2, 3)
assertPrints(numbers.map { it * it }, "[1, 4, 9]")
}
@Sample
fun take() {
val chars = ('a'..'z').toList()
@@ -277,7 +277,6 @@ class Maps {
assertPrints(map2, "{beverage=2.7$, meal=12.4$}")
}
@Sample
fun mapToSortedMap() {
val map = mapOf(Pair("c", 3), Pair("b", 2), Pair("d", 1))
@@ -303,6 +302,16 @@ class Maps {
assertPrints(props.getProperty("z", "fail"), "fail")
}
@Sample
fun mapToList() {
val peopleToAge = mapOf("Alice" to 20, "Bob" to 21)
assertPrints(
peopleToAge.map { (name, age) -> "$name is $age years old" },
"[Alice is 20 years old, Bob is 21 years old]"
)
assertPrints(peopleToAge.map { it.value }, "[20, 21]")
}
}
}
@@ -284,4 +284,10 @@ class Strings {
assertPrints(string.dropLastWhile { !it.isLetter() }, "<<<First Grade")
}
@Sample
fun map() {
val string = "kotlin"
assertPrints(string.map { it.toUpperCase() }, "[K, O, T, L, I, N]")
}
}