I would like to know your opinion. I had following...
# codereview
m
I would like to know your opinion. I had following function:
Copy code
subscriptions += repository.getQuests()
        .applySchedulers()
        .smartSubscribe(
                onSuccess = view::showList and this::updateUpTrackingList,
                onError = view::handleError,
                onFinish = view::hideLoader
        )
Which is, as you can guess, RxJava usage. On success I use both show list on view instance (this is in presenter) and I update tracking list:
Copy code
private fun updateUpTrackingList(quests: List<Quest>) {
    AppDatabase.addAppsToInstall(*quests.map { it.pkg }.toTypedArray())
}
and
is obviously commonly used function in my projects:
Copy code
infix fun <T> ((T) -> Unit).and(f: (T) -> Unit): (T) -> Unit = { this(it); f(it) }
I had to add buffering, but I wanted to keep nice descriptive form of functions passing. So I made following function:
Copy code
private var previousList: List<Quest>? = null

private fun ifChanged(onListChanged: (List<Quest>) -> Unit) = fun(list: List<Quest>) {
    if(previousList != list) {
        previousList = list
        onListChanged(list)
    }
}
And I use it this way:
Copy code
subscriptions += repository.getQuests()
        .applySchedulers()
        .smartSubscribe(
                onSuccess = ifChanged(view::showList and this::updateUpTrackingList),
                onError = view::handleError,
                onFinish = view::hideLoader
        )
For me it looks great but I am not sure if it is not too complex for other people. What do you think? @mg6maciej? Functional programming passionates? Everyone? PS. If I would have function delegate then I could even extract this buffering as a common patter (my proposition for this functionality is waiting on KEEP)
d
If changed can't be an Rx distinct operator? And a side effect like update could be a doOnNext? I think that Rx is already one abstraction that's not being used to its full, so you jave extra layer of abstraction on the subscriber side to 'make up' for it... if you like the logic on subscriber side, you should maybe look into coroutines and it's channels... I personally also don't like so many operators for simple things, but Rx has its uses...
m
Looks like a good idea
d
Next article in Kotlin academy on coroutines vs. rx K ! By the way, thanks for the articles on delegates... ๐Ÿ˜ƒ
m
Thanks ๐Ÿ™‚ I am currently going deeply into Kotlin Coroutines too. It is especially fascinated how they can replace nearly every concurrence model ๐Ÿ˜„ Two days ago I also started "7 programming languages in 7 weeks" book, and now there is only one chapter left (about Haskell). Book especially concentrated on concurrence models of languages, and for every one I was thinking how it can be replaced with coroutines. And they all can! Even Erlang, with its cool concurrence model, can be quite easily replaced with functions and Kotlin coroutines ๐Ÿ˜„ And next book book in my queue is "Seven Concurrency Models in Seven Weeks" ๐Ÿ˜‰
๐Ÿ‘๐Ÿผ 1
d
Looking forward to the article... ๐Ÿ˜‰
btw, you can join #coroutines if you haven't already...
m
I follow it, thanks ๐Ÿ™‚