rainbowadn/rainbowadn/flow/sequence/_compositiondispatch.py
2022-07-12 06:28:21 +03:00

43 lines
1.2 KiB
Python

from typing import Generic, TypeVar
from rainbowadn.core import *
from rainbowadn.flow.core import *
from ._sequencedispatch import *
__all__ = ('CompositionDispatch',)
Element = TypeVar('Element')
Out = TypeVar('Out')
Middle = TypeVar('Middle')
class CompositionDispatch(
SequenceDispatch[Element, Out],
Generic[Element, Out, Middle],
):
def __init__(
self,
domain: Mapper[Element, Middle],
codomain: SequenceDispatch[Middle, Out]
):
assert isinstance(domain, Mapper)
assert isinstance(codomain, SequenceDispatch)
self.domain = domain
self.codomain = codomain
async def on_first(self, element: Element) -> Out:
return await self.codomain.on_first(await self.domain.map(element))
async def on_last(self, element: Element) -> Out:
return await self.codomain.on_last(await self.domain.map(element))
async def on_pair(self, previous: Element, element: Element) -> Out:
previous_middle, element_middle = await gather(
self.domain.map(previous),
self.domain.map(element),
)
return await self.codomain.on_pair(
previous_middle,
element_middle
)