Hey guys I would like to invite you to try out this library named Konad.
https://github.com/lucapiccinelli/konad
Leave a star if you find it helpful
It aims to provide a simple and OOP-oriented API to compose Monads and Monads-like data structures. Kotlin nullables can be easily composed with it.
Complete usage example in the documentation.
Other use-cases in these articles:
Example of composing nullables:
val foo: Int? = 1
val bar: String? = "2"
val baz: Float? = 3.0f
fun useThem(x: Int, y: String, z: Float): Int = x + y.toInt() + z.toInt()
val result: Int? = ::useThem.curry()
.on(foo.maybe)
.on(bar.maybe)
.on(baz.maybe)
.nullable
// or even
val result: Result<Int> = ::useThem.curry()
.on(foo.ifNull("Foo should not be null"))
.on(bar.ifNull("Bar should not be null"))
.on(baz.ifNull("Baz should not be null"))
.result
It also has a composable Result
class that can be in Ok
or Error
states.
Result example:
val userResult: Result<User> = ...
when(userResult){
is Result.Ok -> userResult.toString()
is Result.Errors -> userResult.toList().joinToString(" - ")
}.run(::println)
// or
userResult
.map{ user -> user.toString() }
.ifError { errors -> errors.description(errorDescriptionsSeparator = " - ") }
.run(::println)
Thank you for your attention. Any feedback would be much appreciated.