Skip to content

Commit

Permalink
refactor: add "key" parameter for "stable_unique" function
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Cherng <[email protected]>
  • Loading branch information
jfcherng committed Feb 18, 2024
1 parent e502ed0 commit 26a6fef
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions plugin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,10 @@ def nth(items: Iterable[_T], n: int, default: _U | None = None) -> _T | _U | Non
return next(islice(iter(items), n, None), default)


def stable_unique(items: Iterable[_T]) -> Generator[_T, None, None]:
def stable_unique(items: Iterable[_T], *, key: Callable[[_T], Any] | None = None) -> Generator[_T, None, None]:
"""Lists unique items from the iterable, in their original order."""
yield from {item: True for item in items}.keys()
key = key or (lambda x: x)
yield from {key(item): item for item in items}.values()


@clearable_lru_cache()
Expand Down

0 comments on commit 26a6fef

Please sign in to comment.