Skip to content

Commit 900eb0d

Browse files
committed
Inline functions and allow suspending transform lambdas
1 parent 9b9ff61 commit 900eb0d

File tree

16 files changed

+157
-139
lines changed

16 files changed

+157
-139
lines changed

.idea/compiler.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-all.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.1-all.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

transformations/src/main/kotlin/com/lukaskusik/coroutines/transformations/map/ParallelMap.kt

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import kotlinx.coroutines.coroutineScope
66
/**
77
* Performs map transformation on the iterable using coroutines.
88
*/
9-
suspend fun <T, R> Iterable<T>.mapParallel(
10-
transform: (T) -> R
9+
suspend inline fun <T, R> Iterable<T>.mapParallel(
10+
crossinline transform: suspend (T) -> R
1111
): List<R> = coroutineScope {
1212
map { async { transform(it) } }.map { it.await() }
1313
}
@@ -16,72 +16,72 @@ suspend fun <T, R> Iterable<T>.mapParallel(
1616
/**
1717
* Performs map transformation on the array using coroutines.
1818
*/
19-
suspend fun <T, R> Array<out T>.mapParallel(
20-
transform: (T) -> R
19+
suspend inline fun <T, R> Array<out T>.mapParallel(
20+
crossinline transform: suspend (T) -> R
2121
): List<R> = coroutineScope {
2222
map { async { transform(it) } }.map { it.await() }
2323
}
2424

2525
/**
2626
* Performs map transformation on the array using coroutines.
2727
*/
28-
suspend fun <R> ByteArray.mapParallel(
29-
transform: (Byte) -> R
28+
suspend inline fun <R> ByteArray.mapParallel(
29+
crossinline transform: suspend (Byte) -> R
3030
): List<R> = coroutineScope {
3131
map { async { transform(it) } }.map { it.await() }
3232
}
3333

3434
/**
3535
* Performs map transformation on the array using coroutines.
3636
*/
37-
suspend fun <R> ShortArray.mapParallel(
38-
transform: (Short) -> R
37+
suspend inline fun <R> ShortArray.mapParallel(
38+
crossinline transform: suspend (Short) -> R
3939
): List<R> = coroutineScope {
4040
map { async { transform(it) } }.map { it.await() }
4141
}
4242

4343
/**
4444
* Performs map transformation on the array using coroutines.
4545
*/
46-
suspend fun <R> IntArray.mapParallel(
47-
transform: (Int) -> R
46+
suspend inline fun <R> IntArray.mapParallel(
47+
crossinline transform: suspend (Int) -> R
4848
): List<R> = coroutineScope {
4949
map { async { transform(it) } }.map { it.await() }
5050
}
5151

5252
/**
5353
* Performs map transformation on the array using coroutines.
5454
*/
55-
suspend fun <R> LongArray.mapParallel(
56-
transform: (Long) -> R
55+
suspend inline fun <R> LongArray.mapParallel(
56+
crossinline transform: suspend (Long) -> R
5757
): List<R> = coroutineScope {
5858
map { async { transform(it) } }.map { it.await() }
5959
}
6060

6161
/**
6262
* Performs map transformation on the array using coroutines.
6363
*/
64-
suspend fun <R> FloatArray.mapParallel(
65-
transform: (Float) -> R
64+
suspend inline fun <R> FloatArray.mapParallel(
65+
crossinline transform: suspend (Float) -> R
6666
): List<R> = coroutineScope {
6767
map { async { transform(it) } }.map { it.await() }
6868
}
6969

7070
/**
7171
* Performs map transformation on the array using coroutines.
7272
*/
73-
suspend fun <R> DoubleArray.mapParallel(
74-
transform: (Double) -> R
73+
suspend inline fun <R> DoubleArray.mapParallel(
74+
crossinline transform: suspend (Double) -> R
7575
): List<R> = coroutineScope {
7676
map { async { transform(it) } }.map { it.await() }
7777
}
7878

7979
/**
8080
* Performs map transformation on the array using coroutines.
8181
*/
82-
suspend fun <R> BooleanArray.mapParallel(
83-
transform: (Boolean) -> R
82+
suspend inline fun <R> BooleanArray.mapParallel(
83+
crossinline transform: suspend (Boolean) -> R
8484
): List<R> = coroutineScope {
8585
map { async { transform(it) } }.map { it.await() }
8686
}
87-
//endregion
87+
//endregion

transformations/src/main/kotlin/com/lukaskusik/coroutines/transformations/map/ParallelMapChunked.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import kotlin.math.ceil
1010
*
1111
* @param chunkSize Size of each sub-collection that will be reduced in each coroutine.
1212
*/
13-
suspend fun <T, R> Iterable<T>.mapParallelChunked(
13+
suspend inline fun <T, R> Iterable<T>.mapParallelChunked(
1414
chunkSize: Int,
15-
transform: (T) -> R
15+
crossinline transform: suspend (T) -> R
1616
): List<R> = coroutineScope {
1717
chunked(chunkSize).map { subChunk ->
1818
async {
19-
subChunk.map(transform)
19+
subChunk.map { transform(it) }
2020
}
2121
}.flatMap {
2222
it.await()
@@ -33,9 +33,9 @@ suspend fun <T, R> Iterable<T>.mapParallelChunked(
3333
* @param chunksCount How many chunks should the collection be split into. Defaults to the number of available processors.
3434
*
3535
*/
36-
suspend fun <T, E> Collection<T>.mapParallelChunked(
36+
suspend inline fun <T, E> Collection<T>.mapParallelChunked(
3737
chunksCount: Int = Runtime.getRuntime().availableProcessors(),
38-
transform: (T) -> E
38+
crossinline transform: suspend (T) -> E
3939
): List<E> {
4040
assert(chunksCount > 0) { "Parameter chunksCount must be greater than 0" }
4141
if (isEmpty()) return emptyList()

transformations/src/main/kotlin/com/lukaskusik/coroutines/transformations/mapindexed/ParallelMapIndexed.kt

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import kotlinx.coroutines.coroutineScope
66
/**
77
* Performs map transformation on the iterable using coroutines.
88
*/
9-
suspend fun <T, R> Iterable<T>.mapIndexedParallel(
10-
transform: (index: Int, T) -> R
9+
suspend inline fun <T, R> Iterable<T>.mapIndexedParallel(
10+
crossinline transform: suspend (index: Int, T) -> R
1111
): List<R> = coroutineScope {
1212
mapIndexed { i, elem -> async { transform(i, elem) } }.map { it.await() }
1313
}
@@ -16,72 +16,72 @@ suspend fun <T, R> Iterable<T>.mapIndexedParallel(
1616
/**
1717
* Performs map transformation on the array using coroutines.
1818
*/
19-
suspend fun <T, R> Array<out T>.mapIndexedParallel(
20-
transform: (index: Int, T) -> R
19+
suspend inline fun <T, R> Array<out T>.mapIndexedParallel(
20+
crossinline transform: suspend (index: Int, T) -> R
2121
): List<R> = coroutineScope {
2222
mapIndexed { i, elem -> async { transform(i, elem) } }.map { it.await() }
2323
}
2424

2525
/**
2626
* Performs map transformation on the array using coroutines.
2727
*/
28-
suspend fun <R> ByteArray.mapIndexedParallel(
29-
transform: (index: Int, Byte) -> R
28+
suspend inline fun <R> ByteArray.mapIndexedParallel(
29+
crossinline transform: suspend (index: Int, Byte) -> R
3030
): List<R> = coroutineScope {
3131
mapIndexed { i, elem -> async { transform(i, elem) } }.map { it.await() }
3232
}
3333

3434
/**
3535
* Performs map transformation on the array using coroutines.
3636
*/
37-
suspend fun <R> ShortArray.mapIndexedParallel(
38-
transform: (index: Int, Short) -> R
37+
suspend inline fun <R> ShortArray.mapIndexedParallel(
38+
crossinline transform: suspend (index: Int, Short) -> R
3939
): List<R> = coroutineScope {
4040
mapIndexed { i, elem -> async { transform(i, elem) } }.map { it.await() }
4141
}
4242

4343
/**
4444
* Performs map transformation on the array using coroutines.
4545
*/
46-
suspend fun <R> IntArray.mapIndexedParallel(
47-
transform: (index: Int, Int) -> R
46+
suspend inline fun <R> IntArray.mapIndexedParallel(
47+
crossinline transform: suspend (index: Int, Int) -> R
4848
): List<R> = coroutineScope {
4949
mapIndexed { i, elem -> async { transform(i, elem) } }.map { it.await() }
5050
}
5151

5252
/**
5353
* Performs map transformation on the array using coroutines.
5454
*/
55-
suspend fun <R> LongArray.mapIndexedParallel(
56-
transform: (index: Int, Long) -> R
55+
suspend inline fun <R> LongArray.mapIndexedParallel(
56+
crossinline transform: suspend (index: Int, Long) -> R
5757
): List<R> = coroutineScope {
5858
mapIndexed { i, elem -> async { transform(i, elem) } }.map { it.await() }
5959
}
6060

6161
/**
6262
* Performs map transformation on the array using coroutines.
6363
*/
64-
suspend fun <R> FloatArray.mapIndexedParallel(
65-
transform: (index: Int, Float) -> R
64+
suspend inline fun <R> FloatArray.mapIndexedParallel(
65+
crossinline transform: suspend (index: Int, Float) -> R
6666
): List<R> = coroutineScope {
6767
mapIndexed { i, elem -> async { transform(i, elem) } }.map { it.await() }
6868
}
6969

7070
/**
7171
* Performs map transformation on the array using coroutines.
7272
*/
73-
suspend fun <R> DoubleArray.mapIndexedParallel(
74-
transform: (index: Int, Double) -> R
73+
suspend inline fun <R> DoubleArray.mapIndexedParallel(
74+
crossinline transform: suspend (index: Int, Double) -> R
7575
): List<R> = coroutineScope {
7676
mapIndexed { i, elem -> async { transform(i, elem) } }.map { it.await() }
7777
}
7878

7979
/**
8080
* Performs map transformation on the array using coroutines.
8181
*/
82-
suspend fun <R> BooleanArray.mapIndexedParallel(
83-
transform: (index: Int, Boolean) -> R
82+
suspend inline fun <R> BooleanArray.mapIndexedParallel(
83+
crossinline transform: suspend (index: Int, Boolean) -> R
8484
): List<R> = coroutineScope {
8585
mapIndexed { i, elem -> async { transform(i, elem) } }.map { it.await() }
8686
}
87-
//endregion
87+
//endregion

transformations/src/main/kotlin/com/lukaskusik/coroutines/transformations/mapindexed/ParallelMapIndexedChunked.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import kotlinx.coroutines.coroutineScope
99
*
1010
* @param chunkSize Size of each sub-collection that will be reduced in each coroutine.
1111
*/
12-
suspend fun <T, R> Iterable<T>.mapIndexedParallelChunked(
12+
suspend inline fun <T, R> Iterable<T>.mapIndexedParallelChunked(
1313
chunkSize: Int,
14-
transform: (index: Int, T) -> R
14+
crossinline transform: suspend (index: Int, T) -> R
1515
): List<R> = coroutineScope {
1616
withIndex().chunked(chunkSize).map { subChunk ->
1717
async {
@@ -32,9 +32,9 @@ suspend fun <T, R> Iterable<T>.mapIndexedParallelChunked(
3232
* @param chunksCount How many chunks should the collection be split into. Defaults to the number of available processors.
3333
*
3434
*/
35-
suspend fun <T, E> Collection<T>.mapIndexedParallelChunked(
35+
suspend inline fun <T, E> Collection<T>.mapIndexedParallelChunked(
3636
chunksCount: Int = Runtime.getRuntime().availableProcessors(),
37-
transform: (index: Int, T) -> E
37+
crossinline transform: suspend (index: Int, T) -> E
3838
): List<E> {
3939
assert(chunksCount > 0) { "Parameter chunksCount must be greater than 0" }
4040
if (isEmpty()) return emptyList()
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
11
package com.lukaskusik.coroutines.transformations.mapinplace
22

3-
fun <T> Array<T>.mapInPlace(transform: (T) -> T): Array<T> {
3+
inline fun <T> Array<T>.mapInPlace(transform: (T) -> T): Array<T> {
44
for (i in this.indices) this[i] = transform(this[i])
55
return this
66
}
77

8-
fun ByteArray.mapInPlace(transform: (Byte) -> Byte): ByteArray {
8+
inline fun ByteArray.mapInPlace(transform: (Byte) -> Byte): ByteArray {
99
for (i in this.indices) this[i] = transform(this[i])
1010
return this
1111
}
1212

13-
fun ShortArray.mapInPlace(transform: (Short) -> Short): ShortArray {
13+
inline fun ShortArray.mapInPlace(transform: (Short) -> Short): ShortArray {
1414
for (i in this.indices) this[i] = transform(this[i])
1515
return this
1616
}
1717

18-
fun IntArray.mapInPlace(transform: (Int) -> Int): IntArray {
18+
inline fun IntArray.mapInPlace(transform: (Int) -> Int): IntArray {
1919
for (i in this.indices) this[i] = transform(this[i])
2020
return this
2121
}
2222

23-
fun LongArray.mapInPlace(transform: (Long) -> Long): LongArray {
23+
inline fun LongArray.mapInPlace(transform: (Long) -> Long): LongArray {
2424
for (i in this.indices) this[i] = transform(this[i])
2525
return this
2626
}
2727

28-
fun FloatArray.mapInPlace(transform: (Float) -> Float): FloatArray {
28+
inline fun FloatArray.mapInPlace(transform: (Float) -> Float): FloatArray {
2929
for (i in this.indices) this[i] = transform(this[i])
3030
return this
3131
}
3232

33-
fun DoubleArray.mapInPlace(transform: (Double) -> Double): DoubleArray {
33+
inline fun DoubleArray.mapInPlace(transform: (Double) -> Double): DoubleArray {
3434
for (i in this.indices) this[i] = transform(this[i])
3535
return this
3636
}
3737

38-
fun BooleanArray.mapInPlace(transform: (Boolean) -> Boolean): BooleanArray {
38+
inline fun BooleanArray.mapInPlace(transform: (Boolean) -> Boolean): BooleanArray {
3939
for (i in this.indices) this[i] = transform(this[i])
4040
return this
4141
}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
11
package com.lukaskusik.coroutines.transformations.mapinplace
22

3-
fun <T> Array<T>.mapInPlaceIndexed(transform: (index: Int, T) -> T): Array<T> {
3+
inline fun <T> Array<T>.mapInPlaceIndexed(transform: (index: Int, T) -> T): Array<T> {
44
for (i in this.indices) this[i] = transform(i, this[i])
55
return this
66
}
77

8-
fun ByteArray.mapInPlaceIndexed(transform: (index: Int, Byte) -> Byte): ByteArray {
8+
inline fun ByteArray.mapInPlaceIndexed(transform: (index: Int, Byte) -> Byte): ByteArray {
99
for (i in this.indices) this[i] = transform(i, this[i])
1010
return this
1111
}
1212

13-
fun ShortArray.mapInPlaceIndexed(transform: (index: Int, Short) -> Short): ShortArray {
13+
inline fun ShortArray.mapInPlaceIndexed(transform: (index: Int, Short) -> Short): ShortArray {
1414
for (i in this.indices) this[i] = transform(i, this[i])
1515
return this
1616
}
1717

18-
fun IntArray.mapInPlaceIndexed(transform: (index: Int, Int) -> Int): IntArray {
18+
inline fun IntArray.mapInPlaceIndexed(transform: (index: Int, Int) -> Int): IntArray {
1919
for (i in this.indices) this[i] = transform(i, this[i])
2020
return this
2121
}
2222

23-
fun LongArray.mapInPlaceIndexed(transform: (index: Int, Long) -> Long): LongArray {
23+
inline fun LongArray.mapInPlaceIndexed(transform: (index: Int, Long) -> Long): LongArray {
2424
for (i in this.indices) this[i] = transform(i, this[i])
2525
return this
2626
}
2727

28-
fun FloatArray.mapInPlaceIndexed(transform: (index: Int, Float) -> Float): FloatArray {
28+
inline fun FloatArray.mapInPlaceIndexed(transform: (index: Int, Float) -> Float): FloatArray {
2929
for (i in this.indices) this[i] = transform(i, this[i])
3030
return this
3131
}
3232

33-
fun DoubleArray.mapInPlaceIndexed(transform: (index: Int, Double) -> Double): DoubleArray {
33+
inline fun DoubleArray.mapInPlaceIndexed(transform: (index: Int, Double) -> Double): DoubleArray {
3434
for (i in this.indices) this[i] = transform(i, this[i])
3535
return this
3636
}
3737

38-
fun BooleanArray.mapInPlaceIndexed(transform: (index: Int, Boolean) -> Boolean): BooleanArray {
38+
inline fun BooleanArray.mapInPlaceIndexed(transform: (index: Int, Boolean) -> Boolean): BooleanArray {
3939
for (i in this.indices) this[i] = transform(i, this[i])
4040
return this
4141
}

0 commit comments

Comments
 (0)