From 6ea5be228c81fe229a938d67589bae0480c2b39b Mon Sep 17 00:00:00 2001 From: timotheyca Date: Wed, 7 Sep 2022 23:38:29 +0300 Subject: [PATCH] remove sequence/stacked/stages/chain --- rainbowadn/flow/bridge/_listbridge.py | 2 +- rainbowadn/flow/core/_mapper.py | 8 ++- rainbowadn/flow/core/_mapreduce.py | 14 ++-- rainbowadn/flow/core/_purereduce.py | 2 +- rainbowadn/flow/core/_reduce.py | 10 ++- rainbowadn/flow/sequence/__init__.py | 11 --- .../flow/sequence/_compositiondispatch.py | 42 ----------- .../flow/sequence/_compositiondispatcher.py | 50 ------------- rainbowadn/flow/sequence/_dispatchmapper.py | 26 ------- rainbowadn/flow/sequence/_sequencedispatch.py | 17 ----- .../flow/sequence/_sequencedispatcher.py | 41 ----------- rainbowadn/flow/stacked/__init__.py | 7 -- rainbowadn/flow/stacked/_stackeddispatch.py | 26 ------- rainbowadn/flow/stacked/_stackedreduce.py | 31 -------- rainbowadn/flow/stacked/_stackedreducer.py | 37 ---------- .../flow/verification/chainverification.py | 45 ------------ .../flow/verification/stages/__init__.py | 7 -- .../verification/stages/_stageverification.py | 71 ------------------- .../stages/_stageverificationdispatch.py | 51 ------------- .../stages/_stageverificationprotocol.py | 20 ------ rainbowadn/flow13/_binaryflow.py | 11 +-- rainbowadn/flow13/_flowcheque.py | 4 +- rainbowadn/flow13/_flowiterate.py | 10 +-- rainbowadn/testing/test_bridge.py | 2 +- trace_flow.py | 2 +- 25 files changed, 40 insertions(+), 507 deletions(-) delete mode 100644 rainbowadn/flow/sequence/__init__.py delete mode 100644 rainbowadn/flow/sequence/_compositiondispatch.py delete mode 100644 rainbowadn/flow/sequence/_compositiondispatcher.py delete mode 100644 rainbowadn/flow/sequence/_dispatchmapper.py delete mode 100644 rainbowadn/flow/sequence/_sequencedispatch.py delete mode 100644 rainbowadn/flow/sequence/_sequencedispatcher.py delete mode 100644 rainbowadn/flow/stacked/__init__.py delete mode 100644 rainbowadn/flow/stacked/_stackeddispatch.py delete mode 100644 rainbowadn/flow/stacked/_stackedreduce.py delete mode 100644 rainbowadn/flow/stacked/_stackedreducer.py delete mode 100644 rainbowadn/flow/verification/chainverification.py delete mode 100644 rainbowadn/flow/verification/stages/__init__.py delete mode 100644 rainbowadn/flow/verification/stages/_stageverification.py delete mode 100644 rainbowadn/flow/verification/stages/_stageverificationdispatch.py delete mode 100644 rainbowadn/flow/verification/stages/_stageverificationprotocol.py diff --git a/rainbowadn/flow/bridge/_listbridge.py b/rainbowadn/flow/bridge/_listbridge.py index 4034814..7006851 100644 --- a/rainbowadn/flow/bridge/_listbridge.py +++ b/rainbowadn/flow/bridge/_listbridge.py @@ -35,7 +35,7 @@ class ListBridge( left_bridge.reduce(reduce), right_bridge.reduce(reduce), ) - return reduce.merge(left, right) + return await reduce.merge(left, right) def loose(self) -> Reducer[Element, Out]: return self diff --git a/rainbowadn/flow/core/_mapper.py b/rainbowadn/flow/core/_mapper.py index f3ca26d..fdd72fd 100644 --- a/rainbowadn/flow/core/_mapper.py +++ b/rainbowadn/flow/core/_mapper.py @@ -1,4 +1,4 @@ -from typing import Generic, TypeVar +from typing import Any, Callable, Coroutine, Generic, TypeVar __all__ = ('Mapper',) @@ -9,3 +9,9 @@ Mapped = TypeVar('Mapped') class Mapper(Generic[Element, Mapped]): async def map(self, element: Element) -> Mapped: raise NotImplementedError + + def bind(self, target: Callable[[], Coroutine[Any, Any, Element]]) -> Callable[[], Coroutine[Any, Any, Mapped]]: + async def bound() -> Mapped: + return await self.map(await target()) + + return bound diff --git a/rainbowadn/flow/core/_mapreduce.py b/rainbowadn/flow/core/_mapreduce.py index a7adbe5..04d9dc3 100644 --- a/rainbowadn/flow/core/_mapreduce.py +++ b/rainbowadn/flow/core/_mapreduce.py @@ -27,13 +27,17 @@ class MapReduce(Reduce[Element, Out], Generic[Element, Out, Mapped]): outc: Callable[[], Coroutine[Any, Any, Out]], elementc: Callable[[], Coroutine[Any, Any, Element]], ) -> Out: - async def element(): - return await self.mapper.map(await elementc()) + return await self.reduce_mapped.reducec(outc, self.mapper.bind(elementc)) - return await self.reduce_mapped.reducec(outc, lambda: element()) + async def merge(self, left: Out, right: Out) -> Out: + return await self.reduce_mapped.merge(left, right) - def merge(self, left: Out, right: Out) -> Out: - return self.reduce_mapped.merge(left, right) + async def mergec( + self, + leftc: Callable[[], Coroutine[Any, Any, Out]], + rightc: Callable[[], Coroutine[Any, Any, Out]], + ) -> Out: + return await self.reduce_mapped.mergec(leftc, rightc) def loose(self) -> Reduce[Element, Out]: return self diff --git a/rainbowadn/flow/core/_purereduce.py b/rainbowadn/flow/core/_purereduce.py index 41b3d16..c51f647 100644 --- a/rainbowadn/flow/core/_purereduce.py +++ b/rainbowadn/flow/core/_purereduce.py @@ -14,7 +14,7 @@ class PureReduce(Reduce[Pure, Pure], Generic[Pure]): async def reduce(self, out: Pure, element: Pure) -> Pure: return self.pure(out, element) - def merge(self, left: Pure, right: Pure) -> Pure: + async def merge(self, left: Pure, right: Pure) -> Pure: return self.pure(left, right) def pure(self, left: Pure, right: Pure) -> Pure: diff --git a/rainbowadn/flow/core/_reduce.py b/rainbowadn/flow/core/_reduce.py index af1e008..a0b5cca 100644 --- a/rainbowadn/flow/core/_reduce.py +++ b/rainbowadn/flow/core/_reduce.py @@ -23,5 +23,13 @@ class Reduce(Generic[Element, Out]): out, element = await gather(out2(), element2()) return await self.reduce(out, element) - def merge(self, left: Out, right: Out) -> Out: + 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) diff --git a/rainbowadn/flow/sequence/__init__.py b/rainbowadn/flow/sequence/__init__.py deleted file mode 100644 index c951c2a..0000000 --- a/rainbowadn/flow/sequence/__init__.py +++ /dev/null @@ -1,11 +0,0 @@ -__all__ = ( - 'CompositionDispatcher', - 'DispatchMapper', - 'SequenceDispatch', - 'SequenceDispatcher', 'FirstDispatcher', 'LastDispatcher', 'PairDispatcher', -) - -from ._compositiondispatcher import CompositionDispatcher -from ._dispatchmapper import DispatchMapper -from ._sequencedispatch import SequenceDispatch -from ._sequencedispatcher import FirstDispatcher, LastDispatcher, PairDispatcher, SequenceDispatcher diff --git a/rainbowadn/flow/sequence/_compositiondispatch.py b/rainbowadn/flow/sequence/_compositiondispatch.py deleted file mode 100644 index 1ad55e7..0000000 --- a/rainbowadn/flow/sequence/_compositiondispatch.py +++ /dev/null @@ -1,42 +0,0 @@ -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 - ) diff --git a/rainbowadn/flow/sequence/_compositiondispatcher.py b/rainbowadn/flow/sequence/_compositiondispatcher.py deleted file mode 100644 index 1e7aba9..0000000 --- a/rainbowadn/flow/sequence/_compositiondispatcher.py +++ /dev/null @@ -1,50 +0,0 @@ -from typing import Generic, TypeVar - -from rainbowadn.flow.core import * -from ._compositiondispatch import * -from ._sequencedispatch import * -from ._sequencedispatcher import * - -__all__ = ('CompositionDispatcher',) - -Element = TypeVar('Element') -Out = TypeVar('Out') -Middle = TypeVar('Middle') - - -class CompositionDispatcher( - SequenceDispatcher[Middle, Out], - Generic[Middle, Out, Element] -): - def __init__( - self, - mapper: Mapper[Element, Middle], - dispatcher: SequenceDispatcher[Element, Out] - ): - assert isinstance(mapper, Mapper) - assert isinstance(dispatcher, SequenceDispatcher) - self.mapper = mapper - self.dispatcher = dispatcher - - async def dispatch(self, dispatch: SequenceDispatch[Middle, Out]) -> Out: - cd: SequenceDispatch[Element, Out] = CompositionDispatch(self.mapper, dispatch) - assert isinstance(cd, SequenceDispatch) - return await self.dispatcher.dispatch( - cd - ) - - @classmethod - def mapper(cls, mapper: Mapper[Element, Middle]) -> Mapper[ - SequenceDispatcher[Element, Out], - SequenceDispatcher[Middle, Out], - ]: - assert isinstance(mapper, Mapper) - - def wrap(dispatcher: SequenceDispatcher[Element, Out]) -> SequenceDispatcher[Middle, Out]: - assert isinstance(dispatcher, SequenceDispatcher) - return CompositionDispatcher( - mapper, - dispatcher - ) - - return CallableMapper(wrap) diff --git a/rainbowadn/flow/sequence/_dispatchmapper.py b/rainbowadn/flow/sequence/_dispatchmapper.py deleted file mode 100644 index 10a8ee9..0000000 --- a/rainbowadn/flow/sequence/_dispatchmapper.py +++ /dev/null @@ -1,26 +0,0 @@ -from typing import Generic, TypeVar - -from rainbowadn.flow.core import * -from ._sequencedispatch import * -from ._sequencedispatcher import * - -__all__ = ('DispatchMapper',) - -Element = TypeVar('Element') -Out = TypeVar('Out') - - -class DispatchMapper( - Mapper[SequenceDispatcher[Element, Out], Out], - Generic[Out, Element] -): - def __init__(self, dispatch: SequenceDispatch[Element, Out]): - assert isinstance(dispatch, SequenceDispatch) - self.dispatch = dispatch - - async def map(self, element: SequenceDispatcher[Element, Out]) -> Out: - assert isinstance(element, SequenceDispatcher) - return await element.dispatch(self.dispatch) - - def loose(self) -> Mapper[SequenceDispatcher[Element, Out], Out]: - return self diff --git a/rainbowadn/flow/sequence/_sequencedispatch.py b/rainbowadn/flow/sequence/_sequencedispatch.py deleted file mode 100644 index b0a9cd7..0000000 --- a/rainbowadn/flow/sequence/_sequencedispatch.py +++ /dev/null @@ -1,17 +0,0 @@ -from typing import Generic, TypeVar - -__all__ = ('SequenceDispatch',) - -Element = TypeVar('Element') -Out = TypeVar('Out') - - -class SequenceDispatch(Generic[Element, Out]): - async def on_first(self, element: Element) -> Out: - raise NotImplementedError - - async def on_last(self, element: Element) -> Out: - raise NotImplementedError - - async def on_pair(self, previous: Element, element: Element) -> Out: - raise NotImplementedError diff --git a/rainbowadn/flow/sequence/_sequencedispatcher.py b/rainbowadn/flow/sequence/_sequencedispatcher.py deleted file mode 100644 index 5c727d2..0000000 --- a/rainbowadn/flow/sequence/_sequencedispatcher.py +++ /dev/null @@ -1,41 +0,0 @@ -from typing import Generic, TypeVar - -from ._sequencedispatch import * - -__all__ = ('SequenceDispatcher', 'FirstDispatcher', 'LastDispatcher', 'PairDispatcher',) - -Element = TypeVar('Element') -Out = TypeVar('Out') - - -class SequenceDispatcher(Generic[Element, Out]): - async def dispatch(self, dispatch: SequenceDispatch[Element, Out]) -> Out: - raise NotImplementedError - - -class FirstDispatcher(SequenceDispatcher[Element, Out]): - def __init__(self, element: Element): - self.element = element - - async def dispatch(self, dispatch: SequenceDispatch[Element, Out]) -> Out: - assert isinstance(dispatch, SequenceDispatch) - return await dispatch.on_first(self.element) - - -class LastDispatcher(SequenceDispatcher[Element, Out]): - def __init__(self, element: Element): - self.element = element - - async def dispatch(self, dispatch: SequenceDispatch[Element, Out]) -> Out: - assert isinstance(dispatch, SequenceDispatch) - return await dispatch.on_last(self.element) - - -class PairDispatcher(SequenceDispatcher[Element, Out]): - def __init__(self, previous: Element, element: Element): - self.previous = previous - self.element = element - - async def dispatch(self, dispatch: SequenceDispatch[Element, Out]) -> Out: - assert isinstance(dispatch, SequenceDispatch) - return await dispatch.on_pair(self.previous, self.element) diff --git a/rainbowadn/flow/stacked/__init__.py b/rainbowadn/flow/stacked/__init__.py deleted file mode 100644 index ad8de28..0000000 --- a/rainbowadn/flow/stacked/__init__.py +++ /dev/null @@ -1,7 +0,0 @@ -__all__ = ( - 'StackedReduce', - 'StackedReducer', -) - -from ._stackedreduce import StackedReduce -from ._stackedreducer import StackedReducer diff --git a/rainbowadn/flow/stacked/_stackeddispatch.py b/rainbowadn/flow/stacked/_stackeddispatch.py deleted file mode 100644 index e403c02..0000000 --- a/rainbowadn/flow/stacked/_stackeddispatch.py +++ /dev/null @@ -1,26 +0,0 @@ -from typing import Generic, TypeVar - -from rainbowadn.flow.core import * -from rainbowadn.flow.sequence import * -from rainbowadn.nullability import * - -__all__ = ('StackedDispatch',) - -Element = TypeVar('Element') -Out = TypeVar('Out') - - -class StackedDispatch(SequenceDispatch[Element, Out], Generic[Element, Out]): - def __init__(self, stacked: Reduce[tuple[Nullable[Element], Element], Out], out: Out): - assert isinstance(stacked, Reduce) - self.stacked = stacked - self.out = out - - async def on_first(self, element: Element) -> Out: - return await self.stacked.reduce(self.out, (Null(), element)) - - async def on_last(self, element: Element) -> Out: - return self.out - - async def on_pair(self, previous: Element, element: Element) -> Out: - return await self.stacked.reduce(self.out, (NotNull(previous), element)) diff --git a/rainbowadn/flow/stacked/_stackedreduce.py b/rainbowadn/flow/stacked/_stackedreduce.py deleted file mode 100644 index d48efa1..0000000 --- a/rainbowadn/flow/stacked/_stackedreduce.py +++ /dev/null @@ -1,31 +0,0 @@ -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 diff --git a/rainbowadn/flow/stacked/_stackedreducer.py b/rainbowadn/flow/stacked/_stackedreducer.py deleted file mode 100644 index e864cec..0000000 --- a/rainbowadn/flow/stacked/_stackedreducer.py +++ /dev/null @@ -1,37 +0,0 @@ -from typing import Generic, TypeVar - -from rainbowadn.flow.core import * -from rainbowadn.flow.sequence import * -from rainbowadn.nullability import * -from ._stackedreduce import * - -__all__ = ('StackedReducer',) - -Stacked = TypeVar('Stacked') -Out = TypeVar('Out') - - -class StackedReducer( - Reducer[tuple[Nullable[Stacked], Stacked], Out], - Generic[Out, Stacked] -): - def __init__(self, stacked: Reducer[SequenceDispatcher[Stacked, Out], Out]): - assert isinstance(stacked, Reducer) - self.stacked = stacked - - async def reduce(self, reduce: Reduce[tuple[Nullable[Stacked], Stacked], Out]) -> Out: - assert isinstance(reduce, Reduce) - stacked_reduce: StackedReduce[Out, Stacked] = StackedReduce(reduce) - dispatcher_reduce: Reduce[SequenceDispatcher[Stacked, Out], Out] = stacked_reduce.loose() - assert isinstance(dispatcher_reduce, Reduce) - return await self.stacked.reduce(dispatcher_reduce) - - def loose(self) -> Reducer[tuple[Nullable[Stacked], Stacked], Out]: - return self - - @classmethod - def mapper(cls) -> Mapper[ - Reducer[SequenceDispatcher[Stacked, Out], Out], - Reducer[tuple[Nullable[Stacked], Stacked], Out], - ]: - return CallableMapper(cls) diff --git a/rainbowadn/flow/verification/chainverification.py b/rainbowadn/flow/verification/chainverification.py deleted file mode 100644 index 1eb2328..0000000 --- a/rainbowadn/flow/verification/chainverification.py +++ /dev/null @@ -1,45 +0,0 @@ -from typing import TypeVar - -from rainbowadn.flow.core import * -from rainbowadn.flow.sequence import * -from rainbowadn.flow.stacked import * -from rainbowadn.flow.verification.core import * -from rainbowadn.nullability import * - -__all__ = ('chain_verification',) - -Link = TypeVar('Link') -Chain = TypeVar('Chain') - - -def chain_verification( - chain_to_dispatched: Mapper[Chain, Reducer[SequenceDispatcher[Link, bool], bool]], - link_verification: Verification[tuple[Nullable[Link], Link]], -) -> Verification[ - Chain -]: - assert isinstance(chain_to_dispatched, Mapper) - assert isinstance(link_verification, Verification) - dispatched_to_stacked: Mapper[ - Reducer[SequenceDispatcher[Link, bool], bool], - Reducer[tuple[Nullable[Link], Link], bool] - ] = StackedReducer.mapper() - assert isinstance(dispatched_to_stacked, Mapper) - chain_to_stacked: Mapper[ - Chain, - Reducer[tuple[Nullable[Link], Link], bool] - ] = Composition( - chain_to_dispatched, - dispatched_to_stacked - ) - assert isinstance(chain_to_stacked, Mapper) - stacked_verification: Verification[ - Reducer[tuple[Nullable[Link], Link], bool] - ] = ReduceVerification(link_verification).loose() - assert isinstance(stacked_verification, Verification) - verification: Verification[Chain] = CompositionVerification( - chain_to_stacked, - stacked_verification - ) - assert isinstance(verification, Verification) - return verification diff --git a/rainbowadn/flow/verification/stages/__init__.py b/rainbowadn/flow/verification/stages/__init__.py deleted file mode 100644 index 1dc8555..0000000 --- a/rainbowadn/flow/verification/stages/__init__.py +++ /dev/null @@ -1,7 +0,0 @@ -__all__ = ( - 'StageVerification', - 'StageVerificationProtocol', -) - -from ._stageverification import StageVerification -from ._stageverificationprotocol import StageVerificationProtocol diff --git a/rainbowadn/flow/verification/stages/_stageverification.py b/rainbowadn/flow/verification/stages/_stageverification.py deleted file mode 100644 index 9ddd038..0000000 --- a/rainbowadn/flow/verification/stages/_stageverification.py +++ /dev/null @@ -1,71 +0,0 @@ -from typing import Generic, TypeVar - -from rainbowadn.core import * -from rainbowadn.flow.core import * -from rainbowadn.flow.sequence import * -from rainbowadn.flow.verification.core import * -from rainbowadn.nullability import * -from ._stageverificationdispatch import * -from ._stageverificationprotocol import * - -__all__ = ('StageVerification',) - -Base = TypeVar('Base') -Stage = TypeVar('Stage') -Header = TypeVar('Header') -State = TypeVar('State') - - -class StageVerification( - Verification[tuple[Nullable[State], Header, State]], - Generic[Base, Stage, Header, State] -): - def __init__( - self, - protocol: StageVerificationProtocol[Base, Stage, Header], - mapper: Mapper[State, tuple[Base, Reducer[SequenceDispatcher[Stage, bool], bool]]], - ): - assert isinstance(protocol, StageVerificationProtocol) - assert isinstance(mapper, Mapper) - self.protocol = protocol - self.mapper = mapper - - async def _base(self, state: Nullable[State]) -> Nullable[State]: - assert isinstance(state, Nullable) - if state.null(): - return Null() - else: - base: Base - base, _ = await self.mapper.map(state.resolve()) - return NotNull(base) - - async def verify(self, element: tuple[Nullable[State], Header, State]) -> bool: - assert isinstance(element, tuple) - previous: Nullable[State] - header: Header - state: State - previous, header, state = element - assert isinstance(previous, Nullable) - previous_base: Nullable[State] - base: Base - reducer: Reducer[SequenceDispatcher[Stage, bool], bool] - previous_base, (base, reducer) = await gather( - self._base(previous), - self.mapper.map(state), - ) - assert isinstance(previous_base, Nullable) - assert isinstance(reducer, Reducer) - verification: Verification[Reducer[SequenceDispatcher[Stage, bool], bool]] = StageVerificationDispatch( - self.protocol, - previous_base, - header, - base - ).reduce_verification() - assert isinstance(verification, Verification) - assert_true( - await verification.verify(reducer) - ) - return True - - def loose(self) -> Verification[tuple[Nullable[State], Header, State]]: - return self diff --git a/rainbowadn/flow/verification/stages/_stageverificationdispatch.py b/rainbowadn/flow/verification/stages/_stageverificationdispatch.py deleted file mode 100644 index 67f6f1d..0000000 --- a/rainbowadn/flow/verification/stages/_stageverificationdispatch.py +++ /dev/null @@ -1,51 +0,0 @@ -from typing import Generic, TypeVar - -from rainbowadn.core import * -from rainbowadn.flow.core import * -from rainbowadn.flow.sequence import * -from rainbowadn.flow.verification.core import * -from rainbowadn.nullability import * -from ._stageverificationprotocol import * - -__all__ = ('StageVerificationDispatch',) - -Base = TypeVar('Base') -Stage = TypeVar('Stage') -Header = TypeVar('Header') - - -class StageVerificationDispatch( - SequenceDispatch[Stage, bool], - Generic[Base, Stage, Header] -): - def __init__( - self, - protocol: StageVerificationProtocol[Base, Stage, Header], - previous: Nullable[Base], - header: Header, - base: Base, - ): - assert isinstance(protocol, StageVerificationProtocol) - assert isinstance(previous, Nullable) - self.protocol = protocol - self.previous = previous - self.header = header - self.base = base - - async def on_first(self, element: Stage) -> bool: - assert_true(await self.protocol.initial(self.previous, self.header, element)) - return True - - async def on_last(self, element: Stage) -> bool: - assert_true(await self.protocol.final(element, self.base)) - return True - - async def on_pair(self, previous: Stage, element: Stage) -> bool: - assert_true(await self.protocol.intermediate(previous, element)) - return True - - def _verification(self) -> Verification[SequenceDispatcher[Stage, bool]]: - return MapperVerification(DispatchMapper(self).loose()) - - def reduce_verification(self) -> Verification[Reducer[SequenceDispatcher[Stage, bool], bool]]: - return ReduceVerification(self._verification()).loose() diff --git a/rainbowadn/flow/verification/stages/_stageverificationprotocol.py b/rainbowadn/flow/verification/stages/_stageverificationprotocol.py deleted file mode 100644 index 48d6cbc..0000000 --- a/rainbowadn/flow/verification/stages/_stageverificationprotocol.py +++ /dev/null @@ -1,20 +0,0 @@ -from typing import Generic, TypeVar - -from rainbowadn.nullability import * - -__all__ = ('StageVerificationProtocol',) - -Base = TypeVar('Base') -Stage = TypeVar('Stage') -Header = TypeVar('Header') - - -class StageVerificationProtocol(Generic[Base, Stage, Header]): - async def initial(self, previous: Nullable[Base], header: Header, stage: Stage) -> bool: - raise NotImplementedError - - async def final(self, stage: Stage, base: Base) -> bool: - raise NotImplementedError - - async def intermediate(self, previous: Stage, stage: Stage) -> bool: - raise NotImplementedError diff --git a/rainbowadn/flow13/_binaryflow.py b/rainbowadn/flow13/_binaryflow.py index 4a8e593..665f6e7 100644 --- a/rainbowadn/flow13/_binaryflow.py +++ b/rainbowadn/flow13/_binaryflow.py @@ -58,14 +58,9 @@ class BinaryReducerAction( lambda: aidentity(case.split.key), ) - left, right = await gather( - reduce_left(), - self.on(case.protocolizedr()), - ) - - return self.reduce.merge( - left, - right, + return await self.reduce.mergec( + reduce_left, + lambda: self.on(case.protocolizedr()), ) diff --git a/rainbowadn/flow13/_flowcheque.py b/rainbowadn/flow13/_flowcheque.py index 99e0db4..13f2ee0 100644 --- a/rainbowadn/flow13/_flowcheque.py +++ b/rainbowadn/flow13/_flowcheque.py @@ -145,12 +145,12 @@ class FlowCheque(StaticMentionable, RecursiveMentionable): @classmethod async def _transaction_minted(cls, transaction: FlowTransaction) -> Iterable[FlowCoin]: assert isinstance(transaction, FlowTransaction) - return await FlowIterate.iterate(ResolveMapper.wrap_reducer(await transaction.minted_reducer()), []) + return await FlowIterate.iterate(ResolveMapper.wrap_reducer(await transaction.minted_reducer())) @classmethod async def _transaction_used(cls, transaction: FlowTransaction) -> Iterable[FlowCoin]: assert isinstance(transaction, FlowTransaction) - return await FlowIterate.iterate(ResolveMapper.wrap_reducer(await transaction.used_reducer()), []) + return await FlowIterate.iterate(ResolveMapper.wrap_reducer(await transaction.used_reducer())) @classmethod async def _transaction_usedx(cls, transaction: FlowTransaction) -> Iterable[KeyValue[FlowCoin, FlowTransaction]]: diff --git a/rainbowadn/flow13/_flowiterate.py b/rainbowadn/flow13/_flowiterate.py index 25d7428..9922e73 100644 --- a/rainbowadn/flow13/_flowiterate.py +++ b/rainbowadn/flow13/_flowiterate.py @@ -11,6 +11,9 @@ class FlowIterate( Reduce[Element, Iterable[Element]], Generic[Element] ): + def __init__(self): + super().__init__(()) + async def reduce(self, out: Iterable[Element], element: Element) -> Iterable[Element]: def wrap() -> Iterable[Element]: yield from out @@ -18,7 +21,7 @@ class FlowIterate( return wrap() - def merge(self, left: Iterable[Element], right: Iterable[Element]) -> Iterable[Element]: + async def merge(self, left: Iterable[Element], right: Iterable[Element]) -> Iterable[Element]: def wrap() -> Iterable[Element]: yield from left yield from right @@ -31,7 +34,6 @@ class FlowIterate( @classmethod async def iterate( cls, - reducer: Reducer[Element, Iterable[Element]], - initial: Iterable[Element] + reducer: Reducer[Element, Iterable[Element]] ) -> Iterable[Element]: - return await reducer.reduce(cls(initial)) + return await reducer.reduce(cls()) diff --git a/rainbowadn/testing/test_bridge.py b/rainbowadn/testing/test_bridge.py index 8a95661..3d9dd7f 100644 --- a/rainbowadn/testing/test_bridge.py +++ b/rainbowadn/testing/test_bridge.py @@ -19,7 +19,7 @@ class TestBridge(unittest.IsolatedAsyncioTestCase): async def test_iterator(self): set_gather_linear() bridge = ListBridge(list(range(13))) - print(list(await bridge.reduce(FlowIterate([]).loose()))) + print(list(await bridge.reduce(FlowIterate().loose()))) @classmethod async def abt_of(cls, *plains: bytes) -> ActiveBinaryTree[Plain, Integer]: diff --git a/trace_flow.py b/trace_flow.py index 0b2f191..1f44f0e 100644 --- a/trace_flow.py +++ b/trace_flow.py @@ -57,7 +57,7 @@ async def _generate_transaction( ], keys ) - for coinhp in await (await transaction.minted_reducer()).reduce(FlowIterate([])): + for coinhp in await (await transaction.minted_reducer()).reduce(FlowIterate()): minted.add(coinhp) return transaction