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
+6 -1
View File
@@ -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.
*/
@@ -1242,6 +1242,11 @@ class StringTest {
assertEquals("a ab abc abcd ".toList(), result)
}
@Test fun flatMapIndexed() = withOneCharSequenceArg("bdfi") { data ->
val result = data.flatMapIndexed { index, c -> ('a'..c).drop(index) + ' ' }
assertEquals("ab bcd cdef defghi ".toList(), result)
}
@Test fun fold() = withOneCharSequenceArg { arg1 ->
// calculate number of digits in the string
val data = arg1("a1b2c3def")