Add sample for Map.flatMap
This commit is contained in:
committed by
Ilya Gorbunov
parent
1a0b59da49
commit
dd46d5ca5a
@@ -40,7 +40,7 @@ public fun <K, V> Map<out K, V>.toList(): List<Pair<K, V>> {
|
||||
/**
|
||||
* Returns a single list of all elements yielded from results of [transform] function being invoked on each entry of original map.
|
||||
*
|
||||
* @sample samples.collections.Collections.Transformations.flatMap
|
||||
* @sample samples.collections.Maps.Transformations.flatMap
|
||||
*/
|
||||
public inline fun <K, V, R> Map<out K, V>.flatMap(transform: (Map.Entry<K, V>) -> Iterable<R>): List<R> {
|
||||
return flatMapTo(ArrayList<R>(), transform)
|
||||
|
||||
@@ -353,7 +353,11 @@ class Maps {
|
||||
assertPrints(peopleToAge.map { it.value }, "[20, 21]")
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun flatMap() {
|
||||
val map = mapOf("122" to 2, "3455" to 3)
|
||||
assertPrints(map.flatMap { (key, value) -> key.take(value).toList() }, "[1, 2, 3, 4, 5]")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user