Skip to content

Instantly share code, notes, and snippets.

View pencelab's full-sized avatar
☯️

Glenn pencelab

☯️
View GitHub Profile
fun sumIsEvenAsync() = async {
val r1 = async { myRandomNumberSuccess(OPERATION_IS_EVEN) }
val r2 = async { myRandomNumberSuccess(OPERATION_IS_EVEN) }
val r3 = async { myRandomNumberSuccess(OPERATION_IS_EVEN) }
val sum = r1.await() + r2.await() + r3.await()
log("$OPERATION_IS_EVEN: Sum = $sum")
sum % 2 == 0
}
runBlocking {
val defIsEven = operator.sumIsEvenAsync()
launch {
try {
log("${Operator.OPERATION_IS_EVEN} Result = ${defIsEven.await()}")
} catch (e: Exception) {
log("${Operator.OPERATION_IS_EVEN} Caught: [ $e ]")
} finally {
log("--- NOTHING CONTAINING \"${Operator.OPERATION_IS_EVEN}\" SHOULD APPEAR AFTER THIS LINE ---")
fun sumAverageAsync() = async {
val r1 = async { myRandomNumberSuccess(OPERATION_AVERAGE) }
val r2 = async { myRandomNumberSuccess(OPERATION_AVERAGE) }
val r3 = async { myRandomNumberSuccess(OPERATION_AVERAGE) }
val sum = r1.await() + r2.await() + r3.await()
log("$OPERATION_AVERAGE: Sum = $sum")
sum.toFloat() / 3
}
runBlocking {
val defIsEven = operator.sumIsEvenAsync()
val defAverage = operator.sumAverageAsync()
launch {
try {
log("Operation [Is Even] Result = ${defIsEven.await()}")
} catch (e: Exception) {
log("Operation [Is Even] Caught: [ $e ]")
} finally {
fun sumSquareAsync() = async {
val r1 = async { myRandomNumberSuccess(OPERATION_SQUARE) }
val r2 = async { myRandomNumberSuccess(OPERATION_SQUARE) }
val r3 = async { myRandomNumberSuccess(OPERATION_SQUARE) }
val sum = r1.await() + r2.await() + r3.await()
log("$OPERATION_SQUARE: Sum = $sum")
sum * sum
}
runBlocking {
val defIsEven = operator.sumIsEvenAsync()
val defAverage = operator.sumAverageAsync()
val defSquare = operator.sumSquareAsync()
launch {
try {
log("${Operator.OPERATION_IS_EVEN} Result = ${defIsEven.await()}")
} catch (e: Exception) {
log("${Operator.OPERATION_IS_EVEN} Caught: [ $e ]")
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
private suspend fun sumThreeRandomNumbers(operation: String): Int {
val sum = withContext(Dispatchers.Default) {
val r1 = async { myRandomNumberFail(operation) }
val r2 = async { myRandomNumberSuccess(operation) }
val r3 = async { myRandomNumberSuccess(operation) }
r1.await() + r2.await() + r3.await()
}
return sum
private suspend fun sumThreeRandomNumbers(operation: String): Int {
val sum = coroutineScope {
val r1 = async { myRandomNumberFail(operation) }
val r2 = async { myRandomNumberSuccess(operation) }
val r3 = async { myRandomNumberSuccess(operation) }
r1.await() + r2.await() + r3.await()
}
return sum
private suspend fun sumThreeRandomNumbers(operation: String): Int = coroutineScope {
val r1 = async { myRandomNumberFail(operation) }
val r2 = async { myRandomNumberSuccess(operation) }
val r3 = async { myRandomNumberSuccess(operation) }
r1.await() + r2.await() + r3.await()
}