Created
September 25, 2014 18:00
-
-
Save zahlman/a5bc019cd42ca2650e56 to your computer and use it in GitHub Desktop.
Generator for grouping adjacent values
This file contains hidden or 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
| def group_adjacent(things, predicate, distance): | |
| """Make groups of things that are within distance of each other when the predicate is applied.""" | |
| result = [] | |
| for thing in things: | |
| if result and abs(predicate(thing) - predicate(result[-1])) > distance: | |
| yield result | |
| result = [] | |
| result.append(thing) | |
| if result: | |
| yield result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment