Benji Cooper
Benji Cooper

Reputation: 13

Performing operation on each element of a list, returning a Flowable

I'm working with RxJava, in Kotlin.

I have a function, transform(f: Foo): Single<Bar>

How can I take a List of Foos, and perform transform on each of them, to get a result of Flowable<Bar>, where each next in the Flowable is the results of calling transform?

Basically, I need a function

fun getFlowable(foos: List<Foo>): Flowable<Bar> {
    // Runs transform() on each element of foos
    // Concatenates the results to the flowable.
}

Upvotes: 0

Views: 173

Answers (1)

ardenit
ardenit

Reputation: 3890

fun getFlowable(foos: List<Foo>, transform: (Foo) -> Bar): Flowable<Bar> =
        Flowable.fromIterable(foos.map(transform))

or better version

fun getFlowable(foos: List<Foo>, transform: (Foo) -> Bar): Flowable<Bar> =
        Flowable.fromIterable(foos).map(transform)

Upvotes: 1

Related Questions