32 lines
969 B
Python
32 lines
969 B
Python
from typing import Generic, TypeVar
|
|
|
|
from rainbowadn.flow.core import *
|
|
from rainbowadn.flow.sequence import *
|
|
from rainbowadn.nullability import *
|
|
from ._stackeddispatch import *
|
|
|
|
__all__ = ('StackedReduce',)
|
|
|
|
Stacked = TypeVar('Stacked')
|
|
Out = TypeVar('Out')
|
|
|
|
|
|
class StackedReduce(
|
|
Reduce[SequenceDispatcher[Stacked, Out], Out],
|
|
Generic[Out, Stacked]
|
|
):
|
|
def __init__(self, stacked: Reduce[tuple[Nullable[Stacked], Stacked], Out]):
|
|
assert isinstance(stacked, Reduce)
|
|
super().__init__(stacked.initial)
|
|
self.stacked = stacked
|
|
|
|
async def reduce(self, out: Out, element: SequenceDispatcher[Stacked, Out]) -> Out:
|
|
assert isinstance(element, SequenceDispatcher)
|
|
return await element.dispatch(StackedDispatch(self.stacked, out))
|
|
|
|
def merge(self, left: Out, right: Out) -> Out:
|
|
return self.stacked.merge(left, right)
|
|
|
|
def loose(self) -> Reduce[SequenceDispatcher[Stacked, Out], Out]:
|
|
return self
|