Oversimplified multiplatform result monad library for Kotlin.
// given message processing functions
fun receiveMessage(): Res<String, Error> =
"Message in a bottle".asOk
fun decryptMessage(message: String): Res<String, Error> =
if (message.isNotBlank()) message.asOk else Error.asErr
fun readMessage(message: String): Res<String, Error> =
runCatchingRes { /* ... */ message }.mapErr { Error }
fun forwardMessage(message: String): Res<String, Error> =
runCatchingRes { /* ... */ message }.mapErr { Error }
// process the message and log results
receiveMessage()
.andThen(::decryptMessage)
.andThen(::readMessage)
.andThen(::forwardMessage)
.onOk { message -> println("Message processed: $message") }
.onErr { error -> println("Processing failed: $error") }
In gradle/libs.versions.toml
[versions]
kotlin = "2.0.20"
okres = "5.3"
[libraries]
okres = { module = "de.halfbit:okres", version.ref = "okres" }
[plugins]
kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
In shared/build.gradle.kts
plugins {
alias(libs.plugins.kotlin.multiplatform)
}
kotlin {
sourceSets {
commonMain.dependencies {
implementation(libs.okres)
}
}
}
- Bump version in
root.publication.gradle.kts
./gradlew clean build publishAllPublicationsToCentralRepository
Copyright 2023-2024 Sergej Shafarenka, www.halfbit.de
You are FREE to use, copy, redistribute, remix, transform, and build
upon the material or its derivative FOR ANY LEGAL PURPOSES.
Any distributed derivative work containing this material or parts
of it must have this copyright attribution notices.
The material is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
OR CONDITIONS OF ANY KIND, either express or implied.