https://kotlinlang.org logo
#codereview
Title
# codereview
m

marcinmoskala

02/10/2018, 5:38 AM
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

dave08

02/11/2018, 3:21 AM
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

marcinmoskala

02/12/2018, 5:14 AM
Looks like a good idea
d

dave08

02/12/2018, 7:43 AM
Next article in Kotlin academy on coroutines vs. rx K ! By the way, thanks for the articles on delegates... 😃
m

marcinmoskala

02/12/2018, 2:35 PM
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

dave08

02/12/2018, 2:36 PM
Looking forward to the article... 😉
btw, you can join #coroutines if you haven't already...
m

marcinmoskala

02/12/2018, 3:29 PM
I follow it, thanks 🙂
6 Views