How to change my helper function so that is collects the results of the parallel processing tasks - kotlin

I wrote this helper function, so that I can easily process a list in parallel and only continue code execution when all the work is done. It works nicely when you don't need to return a result.
(I know it isn't the best practice to create new pools every time, it can be easily moved out, but I wanted to keep the examples simple.)
fun recursiveAction(action: () -> Unit): RecursiveAction {
return object : RecursiveAction() {
override fun compute() {
action()
}
}
}
fun <T> List<T>.parallelForEach(parallelSize: Int, action: (T) -> Unit) {
ForkJoinPool(parallelSize).invoke(recursiveAction {
this.parallelStream().forEach { action(it) }
})
}
Example use:
val myList: List<SomeClass> [...]
val parallelSize: Int = 8
myList.parallelForEach(parallelSize) { listElement ->
//Some task here
}
Is there any way to make a similar helper construct for when you want to collect the results back into a list?
I know I have to use a RecursiveTask instead of the RecursiveAction, but I couldn't manage to write a helper function like I had above to wrap it.
I'd like to use it like this:
val myList: List<SomeClass> [...]
val parallelSize: Int = 8
val result: List<SomeClass> = myList.parallelForEach(parallelSize) { listElement ->
//Some task here
}
Alternatively, is there a simpler way to do this alltogether?

Answered by JeffMurdock over on Reddit
fun <T> recursiveTask(action: () -> T): RecursiveTask<T> {
return object : RecursiveTask<T>() {
override fun compute(): T {
return action()
}
}
}
fun <T, E> List<T>.parallelForEach(parallelSize: Int, action: (T) -> E): List<E> {
val pool = ForkJoinPool(parallelSize)
val result = mutableListOf<ForkJoinTask<E>>()
for (item in this) {
result.add(pool.submit(recursiveTask {
action(item)
}))
}
return result.map { it.join() }
}
fun main(args: Array<String>) {
val list = listOf(1, 2, 3)
list.parallelForEach(3) { it + 2 }.forEach { println(it) }
}

Related

How to use Either monad and avoid nested flatMap

I'm in a situation where I'm trying to setup some data and then call a service. Each step can fail, so I'm trying to use Arrow's Either to manage this.
But I'm ending up with a lot of nested flatMaps.
The following code snippet illustrates what I'm trying to do:
import arrow.core.Either
import arrow.core.flatMap
typealias ErrorResponse = String
typealias SuccessResponse = String
data class Foo(val userId: Int, val orderId: Int, val otherField: String)
data class User(val userId: Int, val username: String)
data class Order(val orderId: Int, val otherField: String)
interface MyService {
fun doSomething(foo: Foo, user: User, order: Order): Either<ErrorResponse, SuccessResponse> {
return Either.Right("ok")
}
}
fun parseJson(raw: String): Either<ErrorResponse, Foo> = TODO()
fun lookupUser(userId: Int): Either<ErrorResponse, User> = TODO()
fun lookupOrder(orderId: Int): Either<ErrorResponse, Order> = TODO()
fun start(rawData: String, myService: MyService): Either<ErrorResponse, SuccessResponse> {
val foo = parseJson(rawData)
val user = foo.flatMap {
lookupUser(it.userId)
}
//I want to lookupOrder only when foo and lookupUser are successful
val order = user.flatMap {
foo.flatMap { lookupOrder(it.orderId) }
}
//Only when all 3 are successful, call the service
return foo.flatMap { f ->
user.flatMap { u ->
order.flatMap { o ->
myService.doSomething(f, u, o)
}
}
}
}
I'm sure there is a better way to do this. Can someone help me with an idiomatic approach?
You can use the either { } DSL, this is available in a suspend manner or in a non-suspend manner through the either.eager { } builder.
That way you can use suspend fun <E, A> Either<E, A>.bind(): A.
Rewriting your code example:
fun start(rawData: String, myService: MyService): Either<ErrorResponse, SuccessResponse> =
either.eager {
val foo = parseJson(rawData).bind()
val user = lookupUser(foo.userId).bind()
val order = lookupOrder(foo.orderId).bind()
myService.doSomething(foo, user, order).bind()
}
If you run into an Either.Left, then bind() will short-circuit the either.eager block and return with the encountered Either.Left value.

Executing suspend function inside Iterable.sortedBy

I have this code:
interface Film {
suspend fun total(): Int
}
suspend fun getFilms() : List<Film> {
return films.sortedBy { it.total() }
}
But I get an error because I'm calling Film::total method inside non-suspension function (sortedBy selector). How can I solve this?
suspend fun getFilms(): List<Film> {
return films
.map { it to it.total() }
.sortedBy { it.second }
.map { it.first }
}
I guess it is just a warning, anyways you can force it to run using runBlocking
suspend fun getFilms() : List<Film> {
val films = arrayListOf<Film>()
return films.sortedBy { runBlocking { it.total() } }
}
The error states it clearly "Suspension functions can be called only within coroutine body". You are invoking total() from within sortedBy thus the error.
You should give more context in order for me to be able to provide a more accurate answer. Having such little context you can remove the suspend from total() to let the code compile. Anyway just try this:
suspend fun getFilms() = films.sortedBy { it.total() }
If you cannot change the interface what about something like that:
data class MyFilm(val total: Int) : Film {
override suspend fun total(): Int = total
}
suspend fun getFilms(): List<Film> = withContext(Dispatchers.Default) {
films.sortedBy { it.total }
}
You can wrap #IR42's answer in an extension function to make code a bit more readable if you use this in multiple places.
suspend inline fun <T, R : Comparable<R>> Iterable<T>.sortedBySuspending(
crossinline selector: suspend (T) -> R?
): List<T> = this
.map { it to selector(it) }
.sortedBy { it.second }
.map { it.first }

How to execute a defined function after each function in Kotlin

I am writing Espresso unit test code.
What I want to do is taking screenshot on every actions without specifying
takeSpoonScreenshot("")
This is my AndroidJUnit4 Testcode:
#Test
fun givenVideoDetail_whenChooseCurrentItem_thenShowCountLabel() {
pickerPage {
clickFirstVideoItem()
}
videoDetailPage {
clickSelectCheckBox()
assertCountLabel()
}
}
and this is my VideoDetailPage.kt:
fun videoDetailPage(func: VideoDetailPage.() -> Unit) = VideoDetailPage.apply {
assertFirstPage()
func()
}
fun screenshotAfterAction(func: VideoDetailPage.() -> Unit) = VideoDetailPage.apply {
func()
takeSpoonScreenshot("")
}
object VideoDetailPage : BaseActions() {
// Write 'How to test' here
fun assertFirstPage() {
resourceIsDisplayed(R.id.send_balloon_image)
resourceIsDisplayed(R.id.media_detail_item_check_box)
resourceIsDisplayed(R.id.video_editor_mute_btn)
}
fun clickFilterButton() = takeScreenshotAfterFunction {
clickButton(R.id.image_editor_filter)
}
fun clickSelectCheckBox() {
clickButton(R.id.media_detail_item_check_box)
}
fun assertFilterSelectionListIsOpen() {
resourceIsDisplayed(R.id.media_filter_list)
}
fun assertCountLabel() {
resourceIsDisplayed(R.id.media_editor_selected_count)
}
}
See that I made takeScreenshotAfterFunction, but It is not proper because I should write takeScreenshotAfterFunction N times.

How to implement parallel mapping for sequences in kotlin

I'm trying to implement a parallel implementation for both Iterable and Sequence in Kotlin. I got a little file, it consists of 4 extension functions, but the third one gives me an compiler error:
suspend fun <T, R> Iterable<T>.parallelMap(block: suspend(T) -> R) =
coroutineScope { map { async { block(it) } }.map { it.await() } }
suspend fun <T> Iterable<T>.parallelForEach(block: suspend (T) -> Unit) =
coroutineScope { map { async { block(it) } }.forEach { it.await() } }
suspend fun <T, R> Sequence<T>.parallelMap(block: suspend(T) -> R) =
coroutineScope { map { async { block(it) } }.map { it.await() } }
suspend fun <T> Sequence<T>.parallelForEach(block: suspend (T) -> Unit) =
coroutineScope { map { async { block(it) } }.forEach { it.await() } }
The compiler comes back and says that suspension functions can only be called inside suspension functions. Is there a way to implement this?
Edit: fixed bad copy/paste
Edit2: I thought of an implementation:
suspend fun <T, R> Sequence<T>.parrallelMap(block: suspend (T) -> R) =
asIterable().map { coroutineScope { async { block(it) } } }
.asSequence().map { runBlocking { it.await() } }
I was hoping that this would fire all the suspending functions and await them lazily. I'm just not sure if this is safe, or this saves time or not.
There is a problem with the core semantics of parallel execution for lazy sequences. Your current implementation does not start block(it) until the resulting sequence is iterated:
suspend fun <T, R> Sequence<T>.parallelMap(block: suspend(T) -> R) =
coroutineScope { map { async { block(it) } }.map { it.await() } }
Consider the following example:
sequenceOf(1, 2, 3).parallelMap { it * it }.forEach { println(it) }
For this sample the order of execution will be
val p1 = async { 1 * 1 }
val r1 = p1.await()
println(r1)
val p2 = async { 2 * 2 }
val r2 = p2.await()
println(r2)
val p3 = async { 3 * 3 }
val r3 = p3.await()
println(r3)
Note that the execution of the mapping operations is sequientional, not parallel.
What the compiler tells you is that the lambda of Sequence<T>.map {} is performed lazily on-demand outside of the context of the call (read: outside of your coroutine), so you can't use the coroutine you are currently in.
Frankly, I am not sure how one can both perform lazy computation and do it in parallel.

How to struture coroutine code without suspend function

I have a method called saveAccount
fun saveAccount(id: Int, newName: String): Account {
val encryptedNames: List<String> = repository.findNamesById(id)
val decryptedNames: List<String> = encryptedNames.map { cryptographyService.decrypt(it) }
if(decryptedNames.contains(newName))
throw IllegalStateException()
return repository.save(newName)
}
I want to concurrently decrypt all names, so I did:
suspend fun saveAccount(id: Int, newName: String): Account {
val encryptedNames: List<String> = repository.findNamesById(id)
val decryptedNames: List<String> = encryptedNames.map {
CoroutineScope(Dispatchers.IO).async {
cryptographyService.decrypt(it)
}
}.awaitAll()
if(decryptedNames.contains(newName))
throw IllegalStateException()
return repository.save(newName)
}
Until now everything is fine, but the question is: I can't make saveAccount a suspend function. What should I do?
So, you want to decrypt each name in a separate coroutine, but saveAccount should only return when all decryption is done.
You can use runBlocking for that:
fun saveAccount(id: Int, newName: String): Account {
// ...
val decryptedNames = runBlocking {
encryptedNames.map {
CoroutineScope(Dispatchers.IO).async {
cryptographyService.decrypt(it)
}
}.awaitAll()
}
// ...
}
This way saveAccount does not have to be a suspend function.