Skip to content

Commit 965d9e2

Browse files
flatMapIndexed
1 parent 80ae904 commit 965d9e2

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

ktorm-core/src/main/kotlin/me/liuwj/ktorm/dsl/Query.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,30 @@ public inline fun <R, C : MutableCollection<in R>> Query.flatMapTo(
550550
return destination
551551
}
552552

553+
/**
554+
* Return a single list of all elements yielded from results of [transform] function being invoked on each row
555+
* and its index of the query.
556+
*
557+
* @since 3.1.0
558+
*/
559+
public inline fun <R> Query.flatMapIndexed(transform: (index: Int, row: QueryRowSet) -> Iterable<R>): List<R> {
560+
return flatMapIndexedTo(ArrayList(), transform)
561+
}
562+
563+
/**
564+
* Append all elements yielded from results of [transform] function being invoked on each row and its index
565+
* of the query, to the given [destination].
566+
*
567+
* @since 3.1.0
568+
*/
569+
public inline fun <R, C : MutableCollection<in R>> Query.flatMapIndexedTo(
570+
destination: C,
571+
transform: (index: Int, row: QueryRowSet) -> Iterable<R>
572+
): C {
573+
var index = 0
574+
return flatMapTo(destination) { transform(index++, it) }
575+
}
576+
553577
/**
554578
* Return a [Map] containing key-value pairs provided by [transform] function applied to rows of the query.
555579
*

ktorm-core/src/main/kotlin/me/liuwj/ktorm/entity/EntitySequence.kt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,34 @@ public inline fun <E : Any, R, C : MutableCollection<in R>> EntitySequence<E, *>
460460
return destination
461461
}
462462

463+
/**
464+
* Return a single list of all elements yielded from results of [transform] function being invoked
465+
* on each element and its index of original sequence.
466+
*
467+
* The operation is terminal.
468+
*
469+
* @since 3.1.0
470+
*/
471+
public inline fun <E : Any, R> EntitySequence<E, *>.flatMapIndexed(transform: (index: Int, E) -> Iterable<R>): List<R> {
472+
return flatMapIndexedTo(ArrayList(), transform)
473+
}
474+
475+
/**
476+
* Append all elements yielded from results of [transform] function being invoked on each element
477+
* and its index of original sequence, to the given [destination].
478+
*
479+
* The operation is terminal.
480+
*
481+
* @since 3.1.0
482+
*/
483+
public inline fun <E : Any, R, C : MutableCollection<in R>> EntitySequence<E, *>.flatMapIndexedTo(
484+
destination: C,
485+
transform: (index: Int, E) -> Iterable<R>
486+
): C {
487+
var index = 0
488+
return flatMapTo(destination) { transform(index++, it) }
489+
}
490+
463491
/**
464492
* Customize the selected columns of the internal query by the given [columnSelector] function, and return a [List]
465493
* containing the query results.

ktorm-core/src/test/kotlin/me/liuwj/ktorm/dsl/QueryTest.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,4 +285,18 @@ class QueryTest : BaseTest() {
285285
}
286286
}
287287
}
288+
289+
@Test
290+
fun testFlatMap() {
291+
val names = database
292+
.from(Employees)
293+
.select(Employees.name)
294+
.where { Employees.departmentId eq 1 }
295+
.orderBy(Employees.salary.desc())
296+
.flatMapIndexed { index, row -> listOf("$index:${row.getString(1)}") }
297+
298+
assert(names.size == 2)
299+
assert(names[0] == "0:vince")
300+
assert(names[1] == "1:marry")
301+
}
288302
}

ktorm-core/src/test/kotlin/me/liuwj/ktorm/entity/EntitySequenceTest.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,15 @@ class EntitySequenceTest : BaseTest() {
196196
assert(names.size == 4)
197197
assert(names[0] == "vince")
198198
}
199+
200+
@Test
201+
fun testFlatMap() {
202+
val names = database.employees
203+
.sortedBy { it.id }
204+
.flatMapIndexed { index, employee -> listOf("$index:${employee.name}") }
205+
206+
println(names)
207+
assert(names.size == 4)
208+
assert(names[0] == "0:vince")
209+
}
199210
}

0 commit comments

Comments
 (0)