Arrow: Add `asSequence()` functions

Created on 12 May 2018  路  2Comments  路  Source: arrow-kt/arrow

Hi Folks!

i am not sure this should belong to arrow or rxjava.

Anyway, I have an idea to a function asSequence(), my idea is like this. Btw, i am speaking in android world, but this should be can implemented in another platform. Supposed, we have an view which is an Activity, that calling multiple function in certain callback.

class FooActivity() : AppCompatActivity(), BasePresenter<FooPresenter, FooPresenter.View>, FooPresenter.FooView {

    @Inject
    lateinit var presenter: FooPresenter

    override fun onCreate(/* args */) {
        presenter.beginWith(doSomeWork(1)).then(doGreatWork(2))
    }
}

ini onCreate callback we call a functions from presenter
i am trying to by using asSequence()
we can solve this by using rxjava flatmap. but, i believe by using asSequence() we can saving more code. That second call waiting until first done (onComplete or onErrror)

Below is sample code for presenter.

class FooPresenter : BasePresenter<FooPresenter, FooPresenter.View> {
    fun doSomeWork(id: Int) {
        // todo
        view.someWorkSuccess()
    }

    fun doGreatWork(id: Int) {
        // todo
        view.greatWorkSuccess()
    }

    interface View : BaseView {
        view.someWorkSuccess()
        view.greatWorkSuccess()
    }
}

what do you guys think about this? thanks

Most helpful comment

@radityagumay a few things in mind:

  • Sequences are iterable only once and presumably if you wanted a result back from greatWorkSuccess or any of those functions you'd have to provide it in a synchronous way as well.
  • We have started building arrow-stream which will power arrow-android, an integration layer to handle the lifecycle callbacks in Contexts in a purely functional way.
  • Stream<F, A> in arrow-stream will allow F to be any effect type which at the moment those are Deferred from Kotlin Coroutines, Observable, Flowable from RxJava and IO from arrow.effects. This approach to streaming is a much better way to describe effects in a stream and can be adapted to any existing frameworks since the user may already have in place such as Rx or just Coroutines. Really any data time that can provide an instance for the Effect type class.

While I think what you describe is a good idea in principle I'm not sure Sequence is the right abstraction or data type for it and this kind of issue is what we are wanting to address in arrow-android and arrow-stream. @franciscodr is taking the lead with arrow-stream and I believe many of us including @pakoito @JorgeCastilloPrz @nomisRev and others are interested in a pure functional solution for handling Android's inheritance based lifecycle callbacks.

All 2 comments

@radityagumay a few things in mind:

  • Sequences are iterable only once and presumably if you wanted a result back from greatWorkSuccess or any of those functions you'd have to provide it in a synchronous way as well.
  • We have started building arrow-stream which will power arrow-android, an integration layer to handle the lifecycle callbacks in Contexts in a purely functional way.
  • Stream<F, A> in arrow-stream will allow F to be any effect type which at the moment those are Deferred from Kotlin Coroutines, Observable, Flowable from RxJava and IO from arrow.effects. This approach to streaming is a much better way to describe effects in a stream and can be adapted to any existing frameworks since the user may already have in place such as Rx or just Coroutines. Really any data time that can provide an instance for the Effect type class.

While I think what you describe is a good idea in principle I'm not sure Sequence is the right abstraction or data type for it and this kind of issue is what we are wanting to address in arrow-android and arrow-stream. @franciscodr is taking the lead with arrow-stream and I believe many of us including @pakoito @JorgeCastilloPrz @nomisRev and others are interested in a pure functional solution for handling Android's inheritance based lifecycle callbacks.

@raulraja Ya, i means is a stream abstraction.

So, in implementation detail we have two constructs. that using RxJava subscribe or / and coroutine suspended functions.

 presenter.beginWith(doSomeWork(1)).then(doGreatWork(2)).subscribe { success ->
    // consume the data
 }

 val data = presenter.beginWith(doSomeWork(1)).then(doGreatWork(2)) -> 
    // doSomeWork and doGreatWork are suspended functions
Was this page helpful?
0 / 5 - 0 ratings

Related issues

JorgeCastilloPrz picture JorgeCastilloPrz  路  5Comments

ovu picture ovu  路  4Comments

pakoito picture pakoito  路  3Comments

lgtout picture lgtout  路  4Comments

raulraja picture raulraja  路  3Comments