Add test for parallel iteration over generated sequence
1 file changed
tree: c459977455f17f246ad0edeecd1a3b80bd7ed4fb
  1. kotlinx-coroutines-async/
  2. kotlinx-coroutines-async-example-ui/
  3. kotlinx-coroutines-generate/
  4. .gitignore
  5. pom.xml
  6. README.md
README.md

kotlinx.coroutines

Two libraries built upon Kotlin coroutines:

  • kotlinx-coroutines-async with convenient interfaces/wrappers to commonly used asynchronous API shipped with standard JDK, namely promise-like CompletableFuture and asynchronous channels from java.nio package
  • kotlinx-coroutines-generate provides ability to create Sequence objects generated by coroutine body containing yield suspension points

Examples

Async

import kotlinx.coroutines.async
import java.util.concurrent.CompletableFuture

private fun startLongAsyncOperation(v: Int) =
        CompletableFuture.supplyAsync {
            Thread.sleep(1000)
            "Result: $v"
        }

fun main(args: Array<String>) {
    val future = async<String> {
        (1..5).map {
            await(startLongAsyncOperation(it))
        }.joinToString("\n")
    }

    println(future.get())
}

Bear in mind that async library actively uses CompletableFuture from JDK 8, so it will not work with earlier versions.

Generate

import kotlinx.coroutines.generate

fun main(args: Array<String>) {
    val sequence = generate<Int> {
        for (i in 1..5) {
            yield(i)
        }
    }

    println(sequence.joinToString(" "))
}

For more examples you can look at kotlinx-coroutines-async-example-ui sample project or in tests directories.

Maven

Add the bintray repository

<repository>
    <snapshots>
        <enabled>false</enabled>
    </snapshots>
    <id>dl</id>
    <name>bintray</name>
    <url>http://dl.bintray.com/kotlin/kotlinx.coroutines</url>
</repository>

Add dependencies:

<dependency>
    <groupId>org.jetbrains.kotlinx</groupId>
    <artifactId>kotlinx-coroutines-generate</artifactId>
    <version>0.1-alpha-1</version>
</dependency>
<dependency>
    <groupId>org.jetbrains.kotlinx</groupId>
    <artifactId>kotlinx-coroutines-async</artifactId>
    <version>0.1-alpha-1</version>
</dependency>

Gradle

Just add dependencies:

compile 'org.jetbrains.kotlinx:kotlinx-coroutines-generate:0.1-alpha-1'
compile 'org.jetbrains.kotlinx:kotlinx-coroutines-async:0.1-alpha-1'

NB: As async library is built upon CompletableFuture it requires JDK 8 (24 Android API level)

Also you should include our bintray repository:

repositories {
    maven {
        url "http://dl.bintray.com/kotlin/kotlinx.coroutines"
    }
}