From c17436b361f2c0f6d2b568804652beee9fbb0fff Mon Sep 17 00:00:00 2001 From: cvb941 Date: Fri, 26 Apr 2019 20:17:01 +0300 Subject: [PATCH] Create README.md --- README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..8ba003b --- /dev/null +++ b/README.md @@ -0,0 +1,26 @@ +# Parallel coroutine operations on Kotlin collections +Provides parallelized map, ~~reduce~~, ~~etc.~~ operations using coroutines in Kotlin. + +At this point, there is only a parallel map implementation called .mapParallel(). It is implemented like this. +```kotlin +suspend fun Iterable.mapParallel(transform: (T) -> R): List = coroutineScope { + map { async { transform(it) } }.map { it.await() } +} +``` + +Example of using the parallel map operation. +```kotlin +fun showCase() { + var list = listOf(1,2,3) + runBlocking(Dispatchers.Default) { + var mappedList = list.mapParallel { it * 2 } // Results in [2,4,6] + } +} +``` +If you want to achieve multithreading, make sure to run the coroutine with the Default dispatcher. + +## Future +In the future, I would like parallel reduce and other transformation functions to be implemented, +as well as chunked variations of the map and future operations. +Chunked operations could potentially improve performance since they would split the collection into just a couple of segments, +which would be processed each by a single thread. That could benefit from data locality and lesser thread management.