Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As the question says that <code>(A,1) (A,2) (B,1) (B,2) (C,1) (B,3) (B,4)</code> is a valid output, we'll find that we need to store the waiting number for each letter when you looking at the <code>(C,1)</code> between <code>(B,2)</code> and <code>(B,3)</code>.</p> <p>What's more, as the question says:</p> <blockquote> <p>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.</p> </blockquote> <p>So after forwarding some values, we may still have some queued values. Take the question's output as an example, we must still have <code>(B,4)</code> queued for the coming <code>(B,3)</code> after forwarding <code>(C,1)</code>.</p> <p>Here is a code example that RAC parts are almost the same as Justin's while I highlight the differences in comments:</p> <p>Also, I post a full and runnable code example: <a href="http://d.pr/X59S/9p9bT58U" rel="nofollow">http://d.pr/X59S/9p9bT58U</a>.</p> <pre class="lang-c prettyprint-override"><code>RACSubject *input = [RACSubject subject]; RACSignal *output = [[[[input map:^(NSString *combo) { NSArray *components = [combo componentsSeparatedByString:@","]; NSInteger number = [components[1] integerValue]; return RACTuplePack(components[0], @(number)); }] // !!!: DIFF // We need there state parameters: // 1. The letters and numbers we're waiting for. // 2. Values received that cannot be forwarded until a certain // letter/number. // 3. The values to forward at each step. scanWithStart:RACTuplePack(@{ @"A": @1 }, @[], @[]) reduce:^id(RACTuple *state, RACTuple *letterAndNumber) { __block NSDictionary *waitingLettersAndNumbers = state[0]; __block NSArray *queuedValues = state[1]; // Enqueue this value until we're ready to send it (which may or may not // occur on this step of the scan). queuedValues = [queuedValues arrayByAddingObject:letterAndNumber]; char letterChar = [letterAndNumber.first characterAtIndex:0]; // !!!: DIFF // (letter + 1, number) may be a valid output after forwarding (letter, number + 1) if ( [[waitingLettersAndNumbers objectForKey:letterAndNumber.first] isEqualToNumber:letterAndNumber.second] &amp;&amp; (letterChar == 'A' || [[waitingLettersAndNumbers objectForKey:[NSString stringWithFormat:@"%c", letterChar - 1]] integerValue] &gt; [letterAndNumber.second integerValue]) ) { NSMutableDictionary *mutableWaitingLettersAndNumbers = [NSMutableDictionary dictionaryWithDictionary:waitingLettersAndNumbers]; // Sort queuedValues lexically and numerically. queuedValues = ... // Determine the next letter and number. NSMutableArray *forwardValues = [NSMutableArray array]; NSMutableArray *remindValues = [NSMutableArray array]; [queuedValues enumerateObjectsUsingBlock:^(RACTuple *tuple, NSUInteger idx, BOOL *stop) { ... }]; queuedValues = [remindValues copy]; waitingLettersAndNumbers = [mutableWaitingLettersAndNumbers copy]; // !!!: DIFF // After forwarding some values, we may still have some queued values. return RACTuplePack(waitingLettersAndNumbers, queuedValues, forwardValues); } else { // No values should escape the scan yet. Just pass on our queued // values. return RACTuplePack(waitingLettersAndNumbers, queuedValues, @[]); } }] map:^(RACTuple *state) { // Convert the array of values into a signal. NSArray *forwardValues = state.last; return [forwardValues.rac_sequence signalWithScheduler:[RACScheduler immediateScheduler]]; }] // Forward values from each inner signal in the correct, sorted order. concat]; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload