Last active
May 16, 2016 19:32
-
-
Save loicdescotte/8991962098b347f15a2af68981a140af to your computer and use it in GitHub Desktop.
Kotlin : compose futures of nullables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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