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
def unique_everseen(items: Iterable, get_hashable: Callable[[Any], Any]=None) -> Iterable: | |
""" | |
Returns generator of unique items. | |
If get_hashable(item) is not None or all elements hashable, fast method would be used. | |
""" | |
if callable(get_hashable): | |
seen = set() | |
return (i for i in items for h in (get_hashable(i),) if not (h in seen or seen.add(h))) | |
elif all(isinstance(i, collections.Hashable) for i in items): | |
seen = set() |