Skip to content

Instantly share code, notes, and snippets.

@loicdescotte
Last active May 16, 2016 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loicdescotte/8991962098b347f15a2af68981a140af to your computer and use it in GitHub Desktop.
Save loicdescotte/8991962098b347f15a2af68981a140af to your computer and use it in GitHub Desktop.
Kotlin : compose futures of nullables
@Test
fun combineNullable() {
fun giveInt(x: Int):Int? = x+1
fun giveInt2(x: Int):Int? = x+2
fun combine(x: Int): Int? = giveInt(x)?.let { giveInt2(it) }
assertEquals(combine(1),4)
}
@Test
fun combineFuture(){
fun giveInt(x:Int): CompletableFuture<Int> {
return CompletableFuture.supplyAsync({ x+1 })
}
fun giveInt2(x:Int): CompletableFuture<Int> {
return CompletableFuture.supplyAsync({ x+2 })
}
fun combine(x: Int): CompletableFuture<Int> {
return giveInt(x).thenCompose({ giveInt2(it) })
}
assertEquals(combine(1).get(),4)
}
@Test
fun combineFutureOfNullable(){
fun giveInt(x:Int): CompletableFuture<Int?> = CompletableFuture.supplyAsync({ x+1 })
fun giveInt2(x:Int): CompletableFuture<Int?> = CompletableFuture.supplyAsync({ x+2 })
fun combine (x: Int): CompletableFuture<Int?> = giveInt(x).thenCompose({ it?.let {giveInt2(it) }})
assertEquals(combine(1).get(),4)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment