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

1 from collections.abc import Callable, Iterable

2 from typing import TypeVar, cast, overload

3

4 T = TypeVar('T')

5 U = TypeVar('U')

6

7

8@overload

9 def reduce(data: Iterable[T], callbackfn: Callable[[U, T], U], initial_value: U, /) -> U: ...

10

11

12@overload

13 def reduce(callbackfn: Callable[[U, T], U], initial_value: U, /) -> Callable[[Iterable[T]], U]: ...

14

15

16 def reduce(

17 param1: Iterable[T] | Callable[[U, T], U],

18 param2: Callable[[U, T], U] | U,

19 param3: U | None = None,

20 /,

21) -> U | Callable[[Iterable[T]], U]:

22 if param3 is None:

23 callbackfn = cast(Callable[[U, T], U], param1)

24 initial_value = cast(U, param2)

25 return lambda x: reduce(x, callbackfn, initial_value)

26 data = cast(Iterable[T], param1)

27 callbackfn = cast(Callable[[U, T], U], param2)

28 initial_value = param3

29 for item in data:

30 initial_value = callbackfn(initial_value, item)

31 return initial_value