28 lines
742 B
Python
28 lines
742 B
Python
from typing import Any, Callable, Coroutine, Generic, TypeVar
|
|
from rainbowadn.core import *
|
|
|
|
__all__ = ('Reduce',)
|
|
|
|
|
|
Element = TypeVar('Element')
|
|
Out = TypeVar('Out')
|
|
|
|
|
|
class Reduce(Generic[Element, Out]):
|
|
def __init__(self, initial: Out):
|
|
self.initial = initial
|
|
|
|
async def reduce(self, out: Out, element: Element) -> Out:
|
|
raise NotImplementedError
|
|
|
|
async def reducec(
|
|
self,
|
|
out2: Callable[[], Coroutine[Any, Any, Out]],
|
|
element2: Callable[[], Coroutine[Any, Any, Element]],
|
|
) -> Out:
|
|
out, element = await gather(out2(), element2())
|
|
return await self.reduce(out, element)
|
|
|
|
def merge(self, left: Out, right: Out) -> Out:
|
|
raise NotImplementedError
|