Created
March 21, 2014 16:59
-
-
Save timuruski/9690683 to your computer and use it in GitHub Desktop.
Not sure what to call this type of operation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Input | |
events = [ | |
event(status: 'IDLE', job: 1, date: '2014-01-01'), | |
event(status: 'BUSY', job: 1, date: '2014-01-02'), | |
event(status: 'BUSY', job: 1, date: '2014-01-03'), | |
event(status: 'BUSY', job: 2, date: '2014-01-04'), | |
event(status: 'BUSY', job: 2, date: '2014-01-05'), | |
event(status: 'BUSY', job: 2, date: '2014-01-06'), | |
event(status: 'IDLE', job: 2, date: '2014-01-07'), | |
event(status: 'BUSY', job: 3, date: '2014-01-08'), | |
event(status: 'BUSY', job: 3, date: '2014-01-09'), | |
event(status: 'IDLE', job: 3, date: '2014-01-10') | |
] | |
# Output | |
activity_feed = [ | |
activity(status: 'IDLE', job: 1, date: '2014-01-01'), | |
activity(status: 'BUSY', job: 1, date: '2014-01-03'), | |
activity(status: 'BUSY', job: 2, date: '2014-01-06'), | |
activity(status: 'IDLE', job: 2, date: '2014-01-07'), | |
activity(status: 'BUSY', job: 3, date: '2014-01-09'), | |
activity(status: 'IDLE', job: 3, date: '2014-01-10') | |
] | |
expect { | |
build_activity_feed(events) | |
}.to eql(activity_feed) | |
# Basically we are folding events with the same status or job starting | |
# from the right and folding left. I want to call this foldr, but | |
# descriptions I've found imply fold algorithms convert a vector into a | |
# scalar. ie. [1,2,3] => 6 | |
# | |
# Is there a common name for this operation? Squeeze? Compact? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment