Coverage for src/remedapy/difference.py: 100%

1 from collections import Counter

2 from collections.abc import Callable, Iterable

3 from typing import TypeVar, overload

4

5 T = TypeVar('T')

6

7

8@overload

9 def difference(

10 data: Iterable[T],

11 other: Iterable[T],

12 /,

13) -> Iterable[T]: ...

14

15

16@overload

17 def difference(

18 other: Iterable[T],

19 /,

20) -> Callable[[Iterable[T]], Iterable[T]]: ...

21

22

23 def difference_helper(

24 data: Iterable[T],

25 other: Iterable[T],

26 /,

27) -> Iterable[T]:

28 to_omit = Counter(other)

29 for i in data:

30 if to_omit[i] > 0:

31 to_omit[i] -= 1

32 else:

33 yield i

34

35

36 def difference(

37 data: Iterable[T],

38 other: Iterable[T] | None = None,

39 /,

40) -> Iterable[T] | Callable[[Iterable[T]], Iterable[T]]:

41 if other is None:

42 return lambda x: difference_helper(x, data) # pyright: ignore[reportUnknownVariableType, reportUnknownArgumentType, reportUnknownLambdaType]

43 return difference_helper(data, other)