rainbowadn/rainbowadn/flow/core/_reduce.py

36 lines
1021 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)
async def merge(self, left: Out, right: Out) -> Out:
raise NotImplementedError
async def mergec(
self,
leftc: Callable[[], Coroutine[Any, Any, Out]],
rightc: Callable[[], Coroutine[Any, Any, Out]],
) -> Out:
left, right = await gather(leftc(), rightc())
return await self.merge(left, right)