Created
November 8, 2018 17:33
-
-
Save Richard-Mathie/419779ce26307e43171c71b515afb6b5 to your computer and use it in GitHub Desktop.
Python Iterator Partitioner
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
import itertools | |
def partition(items, predicate=int, n=2): | |
""" Partition an iterable of `items` into `n` streams by `predicate` | |
""" | |
def filterer(i, tee): | |
return (item for pred, item in tee if pred == i) | |
pred_items = ((predicate(item), item) for item in items) | |
teed_items = enumerate(itertools.tee(pred_items, n)) | |
return (filterer(x, t) for x, t in teed_items) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment