Skip to content

Instantly share code, notes, and snippets.

@kastiglione
Last active December 16, 2015 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kastiglione/5475944 to your computer and use it in GitHub Desktop.
Save kastiglione/5475944 to your computer and use it in GitHub Desktop.
Compose a signal corresponding to whether a source signal has sent an equal number of YESes and NOs.
[[[signal
// Map YES to 1, NO to -1
map:^(NSNumber* b) {
return @(b.boolValue ? 1 : -1);
}]
// Running sum of yes/1's and no/-1's
startWithScan:@0 combine:^(NSNumber* running, NSNumber* next) {
return @(running.intValue + next.intValue);
}]
// A 0-sum means equal number of YES/NO's; Map 0 to YES
map:^(NSNumber* sum) {
return @(sum.intValue == 0);
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment