コード例 #1
0
def test_compose_associative(x: int, y: int, z: int):
    """Rearranging the parentheses in an expression will not change the result."""
    fn: Func = lambda a: a + x
    gn: Func = lambda b: b - y
    hn: Func = lambda c: c * z

    cn = compose(fn, gn, hn)
    cn_: Func = lambda x: hn(gn(fn(x)))

    rn = compose(fn, compose(gn, hn))
    rn_: Func = lambda x: (lambda b: hn(gn(b)))(fn(x))  # right associative
    ln = compose(compose(fn, gn), hn)
    ln_: Func = lambda x: hn((lambda b: gn(fn(b)))(x))  # left associative

    assert cn(x) == cn_(x) == rn(x) == rn_(x) == ln(x) == ln_(x)
コード例 #2
0
def concat_map(
    mapper: Callable[[TSource], AsyncObservable[TResult]]
) -> Projection[TSource, TResult]:
    return compose(
        map(mapper),
        merge_inner(1),
    )
コード例 #3
0
def test_compose_many(x: int):
    fn: Func = lambda x: x + 42
    gn: Func = lambda x: x - 3
    hn: Func = lambda x: x * 2

    cn = compose(fn, gn, hn, fn, hn, gn, fn)

    assert cn(x) == fn(gn(hn(fn(hn(gn(fn(x)))))))
コード例 #4
0
def filteri(
    predicate: Callable[[TSource, int], bool]
) -> Callable[[AsyncObservable[TSource]],
              AsyncObservable[TSource]]:  # Projection[TSource, TSource]:
    ret = compose(
        zip_seq(seq.infinite),
        starfilter(predicate),
        map(seq.head),
    )
    return cast(Projection[TSource, TSource], ret)  # FIXME: pyright issue?
コード例 #5
0
def mapi(
        mapper: Callable[[TSource, int],
                         TResult]) -> Projection[TSource, TResult]:
    """Map with index.

    Returns an observable sequence whose elements are the result of
    invoking the mapper function and incorporating the element's index
    on each element of the source.
    """
    return compose(
        zip_seq(seq.infinite),
        starmap(mapper),
    )
コード例 #6
0
def flat_map_latest(
    mapper: Callable[[TSource], AsyncObservable[TResult]]
) -> Projection[TSource, TResult]:
    """Flat map latest.


    Transforms the items emitted by an source sequence into observable
    streams, and mirror those items emitted by the most-recently
    transformed observable sequence.

    Args:
        mapper (Callable[[TSource, AsyncObservable[TResult]]): [description]

    Returns:
        Stream[TSource, TResult]: [description]
    """
    return compose(map(mapper), switch_latest)
コード例 #7
0
def flat_map(
    mapper: Callable[[TSource], AsyncObservable[TResult]]
) -> Projection[TSource, TResult]:
    """Flap map the observable sequence.

    Projects each element of an observable sequence into an observable
    sequence and merges the resulting observable sequences back into one
    observable sequence.

    Args:
        mapper: Function to transform each item in the stream.

    Returns:
        The result stream.
    """

    return compose(
        map(mapper),
        merge_inner(0),
    )
コード例 #8
0
def flat_mapi_async(
    mapper: Callable[[TSource, int], Awaitable[AsyncObservable[TResult]]]
) -> Projection[TSource, TResult]:
    """Flat map async with index.

    Asynchronously projects each element of an observable sequence into
    an observable sequence by incorporating the element's index on each
    element of the source. Merges the resulting observable sequences
    back into one observable sequence.

    Args:
        mapperAsync ([type]): [description]
        Awaitable ([type]): [description]

    Returns:
        Stream[TSource, TResult]: [description]
    """
    return compose(
        mapi_async(mapper),
        merge_inner(0),
    )
コード例 #9
0
def flat_map_async(
    mapper: Callable[[TSource], Awaitable[AsyncObservable[TResult]]]
) -> Projection[TSource, TResult]:
    """Flap map async.

    Asynchronously projects each element of an observable sequence into
    an observable sequence and merges the resulting observable sequences
    back into one observable sequence.


    Args:
        mapperCallable ([type]): [description]
        Awaitable ([type]): [description]

    Returns:
        Stream[TSource, TResult]: [description]
    """
    return compose(
        map_async(mapper),
        merge_inner(0),
    )
コード例 #10
0
def test_compose_left_identity(x: int):
    fn: Func = lambda x: x + 42

    cn: Func = compose(identity, fn)

    assert cn(x) == fn(x)
コード例 #11
0
def test_compose_rigth_identity(x: int):
    fn: Func = lambda x: x + 42

    cn = compose(fn, identity)

    assert cn(x) == fn(x)
コード例 #12
0
def test_compose_2(x: int):
    fn: Func = lambda x: x + 42
    gn: Func = lambda x: x - 3
    hn = compose(fn, gn)

    assert hn(x) == gn(fn(x))
コード例 #13
0
def test_compose_1(x: int):
    fn: Callable[[int], int] = lambda x: x + 42
    gn = compose(fn)

    assert gn(x) == fn(x) == x + 42
コード例 #14
0
def test_compose_identity(x: int):
    fn: Func = compose(identity)

    assert fn(x) == x
コード例 #15
0
def test_compose_identity_implicit(x: int):
    fn = compose()

    assert fn(x) == x
コード例 #16
0
# HttpFunc[TNext, TResult]
HttpFunc = Callable[
    [Context[TNext]],
    HttpFuncResultAsync[TResult],
]

# HttpHandler[TNext, TResult, TSource]
HttpHandler = Callable[
    [
        HttpFunc[TNext, TResult],
        Context[TSource],
    ],
    HttpFuncResultAsync[TResult],
]

finish_early = cast(HttpFunc[Any, Any], compose(Success, aiotools.from_result))


class HttpHandlerFn(Protocol[TSource, TNext]):
    def __call__(self, __next: HttpFunc[TNext, TResult], __context: Context[TSource]) -> HttpFuncResultAsync[TResult]:
        raise NotImplementedError


async def run_async(
    ctx: Context[TSource],
    handler: HttpHandler[TResult, TResult, TSource],
) -> Try[TResult]:
    result = await handler(finish_early, ctx)

    def mapper(x: Context[TResult]) -> TResult:
        return x.Response