peter-tackage/kotlin-options

Updating readme with kotlin's version of mapping

tomaszpolanski opened this issue · 3 comments

How about showing in README first how kotlin would do it's null mapping, something like this:

getCurrentUserId()
    ?.takeIf { it.isNotEmpty() && it != "Invalid Id" }
    ?.let { getCurrentUserFromDatabase(it) }
    ?.username
    ?.let { "Logged in user: $it" }
    .apply {
        if (this != null) {
            log(this)
        } else {
            log("No user to login!")
        }
    }

Of course we would need to mentione that those versions of getCurrentUserId and getCurrentUserFromDatabase return kotlin's nullable

This makes me wonder to what extend we could remove methods from the API and just use Standard.kt, despite its currently confusing names.

and (just an idea, nothing concrete) perhaps end the call with:

fun <R, T : Any?> T.apply(someAction : (T) -> R, noneAction : () -> R) : R {
    return if (this == null) noneAction() else someAction()
}

I do not know if we should rely on Standard, even with takeIf they went with it instead of filter (like in normal list)

The use of filter might be a reflection of the use of filter in Java 8 Streams. It all depends if we think that aliasing takeIf into filter (and similarly for other operators) makes sense. I'm inclined to say "yes", but it's a difficult call to make when such idiomatic Kotlin practices are still being figured out.