Skip to content

Instantly share code, notes, and snippets.

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 pencelab/106be556362cae556864a6632527211f to your computer and use it in GitHub Desktop.
Save pencelab/106be556362cae556864a6632527211f to your computer and use it in GitHub Desktop.
fun sumIsEvenAsync() = async {
val sum = sumThreeRandomNumbers(OPERATION_IS_EVEN)
log("$OPERATION_IS_EVEN: Sum = $sum")
sum % 2 == 0
}
fun sumAverageAsync() = async {
val sum = sumThreeRandomNumbers(OPERATION_AVERAGE)
log("$OPERATION_AVERAGE: Sum = $sum")
sum.toFloat() / 3
}
fun sumSquareAsync() = async {
val sum = sumThreeRandomNumbers(OPERATION_SQUARE)
log("$OPERATION_SQUARE: Sum = $sum")
sum * sum
}
private suspend fun sumThreeRandomNumbers(operation: String): Int {
val r1 = async { myRandomNumberSuccess(operation) }
val r2 = async { myRandomNumberSuccess(operation) }
val r3 = async { myRandomNumberSuccess(operation) }
return r1.await() + r2.await() + r3.await()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment