Last active
August 29, 2015 13:56
-
-
Save carymrobbins/8867308 to your computer and use it in GitHub Desktop.
Unique iterator for Python.
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 identity(x): | |
| return x | |
| class unique(object): | |
| def __init__(self, xs, predicate=identity): | |
| """ :type xs: Iterable """ | |
| self._xs = xs | |
| self._predicate = predicate | |
| def __iter__(self): | |
| """ :rtype: Iterable """ | |
| p = self._predicate | |
| s = set() | |
| for x in self._xs: | |
| p_x = p(x) | |
| if p_x not in s: | |
| s.add(p_x) | |
| yield x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment