I have been tooling around with ReactiveCocoa and I've come upon an interesting problem. I can envision any number of ugly, stateful solutions, but I'm pretty confident there is an elegant, functional way that's just not materializing in my head for whatever reason. Maybe you can help!
The input signal here is two part strings, like "<letter>,<number>"
. The desired sort rules are that for a given letter, the input values should appear at the output in the order of <number>
(i.e. A,2
should never appear before A,1
) and that across all letters <letter>
outputs should not violate alpha order. (i.e. no string starting with B
should appear until at least one string starting with A
has appeared.) Other than as dictated by those rules, the expectation is that things will arrive on the output in the order they are submitted to the input.
Consider the following code:
RACSubject* input = [RACSubject subject];
RACSignal* output = [input <SOME CHAIN OF SIGNAL FUNCTIONS>];
[output subscribeNext:^(id x) { NSLog(@"(%@)",x); }];
[input sendNext: @"A,2"]; // Expect no output
[input sendNext: @"B,4"]; // Expect no output
[input sendNext: @"B,2"]; // Expect no output
[input sendNext: @"B,1"]; // Expect no output
[input sendNext: @"A,1"]; // Expect output: (A,1) (A,2) (B,1) (B,2)
// Note: (A,1) (B,1) (B,2) (A,2) would *not* be right because A,2 appeared on the input before B,1
[input sendNext: @"C,1"]; // Expect output: (C,1)
[input sendNext: @"B,3"]; // Expect output: (B,3) (B,4)
[input sendNext: @"C,3"]; // Expect no output
[input sendNext: @"C,2"]; // Expect output: (C,2) (C,3)
Also output should be produced "eagerly". It's not useful if I have to wait until the input signal is completed before seeing output (unless, of course, the sorting rules dictate that that's the case, i.e. if A,1
came last)
Any ideas?