thanh
08/27/2019, 6:40 PMdarkmoon_uk
08/28/2019, 12:48 AMscan
function as well. It could be implemented by having a var
share some limited scope with a Flow.map
function of course; but like you I'd prefer something idiomatic if it exists.darkmoon_uk
08/28/2019, 12:51 AMConflatedBroadcastChannel
in Kotlin coroutines.
See: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.channels/-conflated-broadcast-channel/tseisel
08/28/2019, 6:21 AMObservable
has scanWith(Callable<R>, BiFunction<R, T, R>)
, which is scan
with a deferred first value.thanh
08/28/2019, 8:02 AMscanWith
, the different is I want to produce the first value from the first emitted item with initialValueProvider
.ilyagulya
08/28/2019, 9:03 AMobservable
.startWith(Observable.fromCallable({YOUR_CALLABLE))
.scan({YOUR_ACCUMULATOR})
darkmoon_uk
08/28/2019, 9:06 AMtseisel
08/28/2019, 9:16 AMfun <T : Any, R : Any> Observable<T>.scanFirst(firstValueMapper: (T) -> R, accumulator: (R, T) -> R): Observable<R> {
val first = this.firstElement().map(firstValueMapper)
val rest = this.skip(1)
return rest.scanWith({ first.blockingGet() }, accumulator)
}
Not sure it performs well regarding the Rx spec, but that's way simpler than writing a custom operator.tseisel
08/28/2019, 9:20 AM<T, R> Flow<T>.scan(initial: R, operation: (accumulator: R, value: T) -> R): Flow<R>
function available in coroutines-core
match your need ? I think it performs the same as Rx's scan
.darkmoon_uk
08/28/2019, 9:21 AMilyagulya
08/28/2019, 9:52 AMthanh
08/28/2019, 10:56 AMthanh
08/28/2019, 10:58 AMfun <T, R> Observable<T>.scan(initialValueProvider: (T) -> R, accumulator: (R, R) -> R): Observable<R> {
return this.map(initialValueProvider).scan(accumulator)
}
It is identical with my original function but it works in my case.thanh
08/28/2019, 2:27 PM