Search code examples
iosswiftfunctional-programmingreactive-cocoa

Transform SignalProducer that emits arrays to SignalProducer that emits all elements of the original array


Let's say that I have a SignalProducer<[Element], Error> that emits an array of elements when started.

I would like to transform that SignalProducer to a new SignalProducer<Element, Error> that emits each element of the [Element] array consecutively.

What is the best approach to do so?


Solution

  • You can do something like this:

    let firstProducer: SignalProducer<[Element], Error> = // something
    let toSingleElement: [Element] -> SignalProducer<Element, Error> = { SignalProducer(values: $0) }
    
    let secondProducer = firstProducer.flatMap(.Concat, transform: toSingleElement)
    

    You can also use Rex's operator: uncollect.