Search code examples
arraysreactive-programmingfrpreactive-cocoa-4

Transform signal of elements into arrays with size using ReactiveCocoa


I'm trying to find a way to transform a signal that sends X element into arrays of X elements limited by size.

Something like:

signal.take(2).collect().observeNext{changes in myFunction(changes) }

But that dies after completed. I need it to be:

  • Take 2 elements
  • Send array to function
  • Repeat

Any idea?


Solution

  • I solved this task (for location) and it's my solution

    extention SignalProducer {
        func accumulate(size: Int) -> SignalProducer<[Value], Error> {
            var values: [Value] = []
            func next(value: Value) {
                if values.count >= size {
                    values.removeAll()
                }
                values.append(value)
            }
            return on(next: next)
                .filter { _ in values.count < size }
                .map { _ -> [Value] in return values }
        }
    }
    

    https://github.com/ReactiveCocoa/ReactiveCocoa/pull/2817