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.
|
* 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> {
|
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)
|
return flatMapTo(ArrayList<R>(), transform)
|
||||||
|
|||||||
@@ -353,7 +353,11 @@ class Maps {
|
|||||||
assertPrints(peopleToAge.map { it.value }, "[20, 21]")
|
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]")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -290,7 +290,13 @@ object Mapping : TemplateGroupBase() {
|
|||||||
specialFor(ArraysOfUnsigned) { inlineOnly() }
|
specialFor(ArraysOfUnsigned) { inlineOnly() }
|
||||||
|
|
||||||
doc { "Returns a single list of all elements yielded from results of [transform] function being invoked on each ${f.element} of original ${f.collection}." }
|
doc { "Returns a single list of all elements yielded from results of [transform] function being invoked on each ${f.element} of original ${f.collection}." }
|
||||||
sample("samples.collections.Collections.Transformations.flatMap")
|
|
||||||
|
fun sampleClass(f: Family): String = when (f) {
|
||||||
|
Maps -> "samples.collections.Maps.Transformations"
|
||||||
|
else -> "samples.collections.Collections.Transformations"
|
||||||
|
}
|
||||||
|
|
||||||
|
sample("${sampleClass(f)}.flatMap")
|
||||||
typeParam("R")
|
typeParam("R")
|
||||||
returns("List<R>")
|
returns("List<R>")
|
||||||
body {
|
body {
|
||||||
|
|||||||
Reference in New Issue
Block a user