Last active
July 2, 2022 16:59
-
-
Save Jian-Min-Huang/76004099e298f7a87b65827af9c37b9a to your computer and use it in GitHub Desktop.
Some note from learning Arrow-kt
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
fun <T> T?.catchErrWhenNull(tw: Throwable): Either<Throwable, T> = Either.Companion.catch { this ?: throw tw } | |
fun <T> Either<Throwable, T?>.flattenCatchErrWhenNull(tw: Throwable): Either<Throwable, T> = | |
when (this) { | |
is Either.Left -> this | |
is Either.Right -> Either.Companion.catch { | |
this.value ?: throw tw | |
} | |
} | |
suspend fun <T> T.catchErrWhenTrue(tw: Throwable, block: suspend (t: T) -> Boolean): Either<Throwable, T> = | |
Either.Companion.catch { | |
if (block.invoke(this)) throw tw | |
else this | |
} | |
suspend fun <T> Either<Throwable, T>.flattenCatchErrWhenTrue(tw: Throwable, block: suspend (t: T) -> Boolean): Either<Throwable, T> = | |
when (this) { | |
is Either.Left -> this | |
is Either.Right -> Either.Companion.catch { | |
if (block.invoke(this.value)) throw tw | |
else this.value | |
} | |
} | |
suspend fun <T, R> T.catchErrWhenMap(tw: Throwable, block: suspend (t: T) -> R): Either<Throwable, R> = | |
Either.Companion.catch { | |
try { | |
block.invoke(this) | |
} catch (_: Throwable) { | |
throw tw | |
} | |
} | |
suspend fun <T, R> Either<Throwable, T>.flattenCatchErrWhenMap(tw: Throwable, block: suspend (t: T) -> R): Either<Throwable, R> = | |
when (this) { | |
is Either.Left -> this | |
is Either.Right -> Either.Companion.catch { | |
try { | |
block.invoke(this.value) | |
} catch (_: Throwable) { | |
throw tw | |
} | |
} | |
} | |
fun eitherValidateLogic1(): Either<Throwable, Boolean> = catch { false } | |
fun eitherValidateLogic2(): Either<Throwable, Boolean> = catch { false } | |
fun eitherCalculateLogic1(): Either<Throwable, String?> = catch { "null ?" } | |
fun eitherCalculateLogic2(): Either<Throwable, Map<String, String>> = catch { mapOf("" to "") } | |
fun demoArrow(input: Map<String, Any>): Either<Throwable, Map<String, Any>> = | |
catch { | |
input | |
}.flatMap { | |
eitherValidateLogic1() | |
}.flatMap { | |
eitherValidateLogic2() | |
}.flatMap { | |
eitherCalculateLogic1() | |
}.flatMap { | |
catch { it ?: throw RuntimeException("") } | |
}.flatMap { | |
eitherCalculateLogic2() | |
} | |
fun validateLogic1(): Boolean = false | |
fun validateLogic2(): Boolean = false | |
fun calculateLogic1(): String? = "null ?" | |
fun calculateLogic2(): Map<String, String> = mapOf("" to "") | |
fun demoMyArrow(input: Map<String, Any>): Either<Throwable, Map<String, Any>> = runBlocking { | |
input.catchErrWhenTrue(RuntimeException("")) { validateLogic1() } | |
.flattenCatchErrWhenTrue(RuntimeException("")) { validateLogic2() } | |
.flattenCatchErrWhenMap(RuntimeException("")) { calculateLogic1() } | |
.flattenCatchErrWhenNull(RuntimeException("")) | |
.flattenCatchErrWhenMap(RuntimeException("")) { calculateLogic2() } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment