Created
May 28, 2023 19:43
-
-
Save superatomic/65ba271f8ab5d1a2f966dcc4f9e458ae to your computer and use it in GitHub Desktop.
A python 3.12 `unique` function, with type hints.
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
from collections.abc import Hashable, Iterable, Iterator | |
def unique[T: Hashable](items: Iterable[T]) -> Iterator[T]: | |
seen = set() | |
for item in items: | |
if item not in seen: | |
seen.add(item) | |
yield item |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment