Provide flatMapIndexed operation

- similar to flatMap, but transform function takes index and element

#KT-36894
This commit is contained in:
Ilya Gorbunov
2020-06-19 16:16:55 +03:00
parent db93462bcf
commit 130987fa1e
20 changed files with 1216 additions and 74 deletions
@@ -318,6 +318,15 @@ constructor(
}
}
internal fun <T, C, R> flatMapIndexed(source: Sequence<T>, transform: (Int, T) -> C, iterator: (C) -> Iterator<R>): Sequence<R> =
sequence {
var index = 0
for (element in source) {
val result = transform(checkIndexOverflow(index++), element)
yieldAll(iterator(result))
}
}
/**
* A sequence that supports drop(n) and take(n) operations
*/