Stream[A, E, R]: writes items of type A in non-empty tuples
(chunks), then fails with E or ends; needs the services R.
Build streams with the functions in pyfect.stream; methods that
keep or add to the type parameters are here. Combinators that remove
something from a type (scoped, catch_tag, catch_if,
provide*, update_context) are
functions in pyfect.stream, used with .pipe.
Functions#
__init__#
__init__(channel) -> None| Parameter | Type | Description |
|---|---|---|
channel | StreamChannel[A_co, E_co, R_co] |
def __init__(self, channel: StreamChannel[A_co, E_co, R_co]) -> None:
self._channel = channelpipe#
pipe(f) -> BApply f to this stream (one step; see Effect.pipe).
| Parameter | Type | Description |
|---|---|---|
f | Callable[[Self], B] |
Returns B
def pipe[B](self, f: Callable[[Self], B], /) -> B:
"""Apply ``f`` to this stream (one step; see ``Effect.pipe``)."""
return f(self)pipe_through_channel#
pipe_through_channel(channel) -> Stream[B, E2, R_co | R2]The stream of what channel writes when this stream's chunks
are its input. The channel sees this stream's failure (and decides
what to do with it); its end value is dropped.
| Parameter | Type | Description |
|---|---|---|
channel | Channel[tuple[B, ...], E2, object, tuple[A_co, ...], E_co, None, R2] |
Returns Stream[B, E2, R_co | R2]
def pipe_through_channel[B, E2 = Never, R2 = Never](
self, channel: Channel[tuple[B, ...], E2, object, tuple[A_co, ...], E_co, None, R2]
) -> Stream[B, E2, R_co | R2]:
"""The stream of what ``channel`` writes when this stream's chunks
are its input. The channel sees this stream's failure (and decides
what to do with it); its end value is dropped."""
from pyfect.stream import _text
return _text.pipe_through_channel(self, channel)pipe_through_channel_or_fail#
pipe_through_channel_or_fail(channel) -> Stream[B, E_co | E2, R_co | R2]pipe_through_channel for a channel that cannot see this
stream's failures: they pass through it untouched and fail the
stream.
| Parameter | Type | Description |
|---|---|---|
channel | Channel[tuple[B, ...], E2, object, tuple[A_co, ...], Never, None, R2] |
Returns Stream[B, E_co | E2, R_co | R2]
def pipe_through_channel_or_fail[B, E2 = Never, R2 = Never](
self, channel: Channel[tuple[B, ...], E2, object, tuple[A_co, ...], Never, None, R2]
) -> Stream[B, E_co | E2, R_co | R2]:
"""``pipe_through_channel`` for a channel that cannot see this
stream's failures: they pass through it untouched and fail the
stream."""
from pyfect.stream import _text
return _text.pipe_through_channel_or_fail(self, channel)decode_text#
decode_text(encoding='utf-8') -> Stream[str, E, R]Decode byte strings as text in encoding, keeping a character
split across byte strings whole; bad bytes become U+FFFD and a UTF-8
byte-order mark is dropped (channel.decode_text).
| Parameter | Type | Description |
|---|---|---|
encoding | str = 'utf-8' |
Returns Stream[str, E, R]
def decode_text[E, R](
self: ChannelMethods[bytes, E, R], encoding: str = "utf-8"
) -> Stream[str, E, R]:
"""Decode byte strings as text in ``encoding``, keeping a character
split across byte strings whole; bad bytes become U+FFFD and a UTF-8
byte-order mark is dropped (``channel.decode_text``)."""
from pyfect.stream import _text
return _text.decode_text(self, encoding)encode_text#
encode_text() -> Stream[bytes, E, R]Encode each string as UTF-8.
Returns Stream[bytes, E, R]
def encode_text[E, R](self: ChannelMethods[str, E, R]) -> Stream[bytes, E, R]:
"""Encode each string as UTF-8."""
from pyfect.stream import _text
return _text.encode_text(self)split_lines#
split_lines() -> Stream[str, E, R]Split text into lines at \n, \r\n and \r, wherever
the chunks break, without the line breaks. The text after the last
break is the last line; a trailing break adds no empty line.
Returns Stream[str, E, R]
def split_lines[E, R](self: ChannelMethods[str, E, R]) -> Stream[str, E, R]:
"""Split text into lines at ``\\n``, ``\\r\\n`` and ``\\r``, wherever
the chunks break, without the line breaks. The text after the last
break is the last line; a trailing break adds no empty line."""
from pyfect.stream import _text
return _text.split_lines(self)run_into_queue#
run_into_queue(queue) -> Effect[None, Never, R_co]Offer every item to queue, then end it, or fail it with the
stream's cause. The queue is finished even if the run is
interrupted.
| Parameter | Type | Description |
|---|---|---|
queue | Enqueue[A_co, E_co | Done] |
Returns Effect[None, Never, R_co]
def run_into_queue(self, queue: Enqueue[A_co, E_co | Done]) -> Effect[None, Never, R_co]:
"""Offer every item to ``queue``, then end it, or fail it with the
stream's cause. The queue is finished even if the run is
interrupted."""
return self._channel.run_into_queue_array(queue)to_queue#
to_queue(*, capacity, strategy='suspend') -> Effect[Dequeue[A_co, E_co | Done], Never, R_co | Scope]Run the stream on a fiber in the current Scope, offering its
items to a new queue of capacity; the queue ends (or fails) as
the stream does and shuts down with the scope.
| Parameter | Type | Description |
|---|---|---|
capacity | Capacity | |
strategy | Strategy = 'suspend' |
Returns Effect[Dequeue[A_co, E_co | Done], Never, R_co | Scope]
def to_queue(
self, *, capacity: Capacity, strategy: Strategy = "suspend"
) -> Effect[Dequeue[A_co, E_co | Done], Never, R_co | Scope]:
"""Run the stream on a fiber in the current ``Scope``, offering its
items to a new queue of ``capacity``; the queue ends (or fails) as
the stream does and shuts down with the scope."""
if capacity == "unbounded":
return self._channel.to_queue_array(capacity=capacity)
return self._channel.to_queue_array(capacity=capacity, strategy=strategy)run_into_pubsub#
run_into_pubsub(pubsub, *, shutdown_on_end=False) -> Effect[None, E_co, R_co]Publish every item to pubsub; with shutdown_on_end, shut it
down when the run ends.
| Parameter | Type | Description |
|---|---|---|
pubsub | PubSub[A_co] | |
shutdown_on_end | bool = False |
Returns Effect[None, E_co, R_co]
def run_into_pubsub(
self, pubsub: PubSub[A_co], *, shutdown_on_end: bool = False
) -> Effect[None, E_co, R_co]:
"""Publish every item to ``pubsub``; with ``shutdown_on_end``, shut it
down when the run ends."""
return self._channel.run_into_pubsub_array(pubsub, shutdown_on_end=shutdown_on_end)to_pubsub#
to_pubsub(*, capacity, strategy='suspend', replay=None, shutdown_on_end=True) -> Effect[PubSub[A_co], Never, R_co | Scope]Run the stream on a fiber in the current Scope, publishing its
items to a new pubsub, shut down with the scope (and, unless
shutdown_on_end is off, when the run ends).
| Parameter | Type | Description |
|---|---|---|
capacity | Capacity | |
strategy | Strategy = 'suspend' | |
replay | int | None = None | |
shutdown_on_end | bool = True |
Returns Effect[PubSub[A_co], Never, R_co | Scope]
def to_pubsub(
self,
*,
capacity: Capacity,
strategy: Strategy = "suspend",
replay: int | None = None,
shutdown_on_end: bool = True,
) -> Effect[PubSub[A_co], Never, R_co | Scope]:
"""Run the stream on a fiber in the current ``Scope``, publishing its
items to a new pubsub, shut down with the scope (and, unless
``shutdown_on_end`` is off, when the run ends)."""
if capacity == "unbounded":
return self._channel.to_pubsub_array(
capacity=capacity, replay=replay, shutdown_on_end=shutdown_on_end
)
return self._channel.to_pubsub_array(
capacity=capacity, strategy=strategy, replay=replay, shutdown_on_end=shutdown_on_end
)to_pubsub_take#
to_pubsub_take(*, capacity, strategy='suspend', replay=None) -> Effect[PubSub[Take[A_co, E_co]], Never, R_co | Scope]Run the stream on a fiber in the current Scope, publishing
each chunk as a Take, then its exit, so every subscriber (one
that subscribes late too) sees the end or the failure.
| Parameter | Type | Description |
|---|---|---|
capacity | Capacity | |
strategy | Strategy = 'suspend' | |
replay | int | None = None |
Returns Effect[PubSub[Take[A_co, E_co]], Never, R_co | Scope]
def to_pubsub_take(
self, *, capacity: Capacity, strategy: Strategy = "suspend", replay: int | None = None
) -> Effect[PubSub[Take[A_co, E_co]], Never, R_co | Scope]:
"""Run the stream on a fiber in the current ``Scope``, publishing
each chunk as a ``Take``, then its exit, so every subscriber (one
that subscribes late too) sees the end or the failure."""
if capacity == "unbounded":
return self._channel.to_pubsub_take(capacity=capacity, replay=replay)
return self._channel.to_pubsub_take(capacity=capacity, strategy=strategy, replay=replay)to_async_iterable#
to_async_iterable() -> StreamAsyncIterable[A]The stream as an async for source, for asyncio code. Each
async for runs the stream afresh on the running loop; a failure
raises the squashed error. Close an iterator left early with
contextlib.aclosing (or aclose()).
Returns StreamAsyncIterable[A]
def to_async_iterable[A, E](self: BridgeMethods[A, E, Never]) -> StreamAsyncIterable[A]:
"""The stream as an ``async for`` source, for asyncio code. Each
``async for`` runs the stream afresh on the running loop; a failure
raises the squashed error. Close an iterator left early with
``contextlib.aclosing`` (or ``aclose()``)."""
from pyfect.context import Context
from pyfect.stream._async_iterable import StreamAsyncIterable
return StreamAsyncIterable(self._channel, Context.empty())to_async_iterable_with#
to_async_iterable_with(context) -> StreamAsyncIterable[A_co]to_async_iterable for a stream that needs the services in
context.
| Parameter | Type | Description |
|---|---|---|
context | Context[R_co] |
Returns StreamAsyncIterable[A_co]
def to_async_iterable_with(self, context: Context[R_co]) -> StreamAsyncIterable[A_co]:
"""``to_async_iterable`` for a stream that needs the services in
``context``."""
from pyfect.stream._async_iterable import StreamAsyncIterable
return StreamAsyncIterable(self._channel, context)to_async_iterable_effect#
to_async_iterable_effect() -> Effect[StreamAsyncIterable[A_co], Never, R_co]to_async_iterable_with the current fiber's services.
Returns Effect[StreamAsyncIterable[A_co], Never, R_co]
def to_async_iterable_effect(self) -> Effect[StreamAsyncIterable[A_co], Never, R_co]:
"""``to_async_iterable_with`` the current fiber's services."""
from pyfect._context_effects import context
from pyfect.stream._async_iterable import StreamAsyncIterable
return context().map(lambda ctx: StreamAsyncIterable(self._channel, ctx))group_by#
group_by(f, *, buffer_size=GROUP_BUFFER_SIZE, idle_time_to_live=None) -> Stream[tuple[K, Stream[V]], E_co | E2, R_co | R2]Write (key, group) once per new key f(item) gives, where
group is the stream of that key's values. Upstream runs on a
fiber and waits while a group holds buffer_size values; a group
read by nobody for idle_time_to_live ends, and the key's next
value starts a new one. A group that stops early drops its values.
| Parameter | Type | Description |
|---|---|---|
f | Callable[[A_co], Effect[tuple[K, V], E2, R2]] | |
buffer_size | int = GROUP_BUFFER_SIZE | |
idle_time_to_live | Duration | None = None |
Returns Stream[tuple[K, Stream[V]], E_co | E2, R_co | R2]
def group_by[K: Hashable, V, E2 = Never, R2 = Never](
self,
f: Callable[[A_co], Effect[tuple[K, V], E2, R2]],
*,
buffer_size: int = GROUP_BUFFER_SIZE,
idle_time_to_live: Duration | None = None,
) -> Stream[tuple[K, Stream[V]], E_co | E2, R_co | R2]:
"""Write ``(key, group)`` once per new key ``f(item)`` gives, where
``group`` is the stream of that key's values. Upstream runs on a
fiber and waits while a group holds ``buffer_size`` values; a group
read by nobody for ``idle_time_to_live`` ends, and the key's next
value starts a new one. A group that stops early drops its values."""
from pyfect.stream import _grouping
return _grouping.group_by(self, f, buffer_size, idle_time_to_live)group_by_key#
group_by_key(f, *, buffer_size=GROUP_BUFFER_SIZE, idle_time_to_live=None) -> Stream[tuple[K, Stream[A_co]], E_co, R_co]group_by with the items as values, keyed by f(item).
| Parameter | Type | Description |
|---|---|---|
f | Callable[[A_co], K] | |
buffer_size | int = GROUP_BUFFER_SIZE | |
idle_time_to_live | Duration | None = None |
Returns Stream[tuple[K, Stream[A_co]], E_co, R_co]
def group_by_key[K: Hashable](
self,
f: Callable[[A_co], K],
*,
buffer_size: int = GROUP_BUFFER_SIZE,
idle_time_to_live: Duration | None = None,
) -> Stream[tuple[K, Stream[A_co]], E_co, R_co]:
"""``group_by`` with the items as values, keyed by ``f(item)``."""
from pyfect.stream import _grouping
return _grouping.group_by_key(self, f, buffer_size, idle_time_to_live)group_adjacent_by#
group_adjacent_by(f) -> Stream[tuple[K, tuple[A_co, ...]], E_co, R_co]Write each run of neighbouring items with equal keys f(item)
as (key, items); a key that comes back later starts a new run.
| Parameter | Type | Description |
|---|---|---|
f | Callable[[A_co], K] |
Returns Stream[tuple[K, tuple[A_co, ...]], E_co, R_co]
def group_adjacent_by[K](
self, f: Callable[[A_co], K]
) -> Stream[tuple[K, tuple[A_co, ...]], E_co, R_co]:
"""Write each run of neighbouring items with equal keys ``f(item)``
as ``(key, items)``; a key that comes back later starts a new run."""
from pyfect.stream import _grouping
return _grouping.group_adjacent_by(self, f)partition#
partition(f, *, capacity=16) -> Effect[tuple[Stream[P, E_co], Stream[F, E_co]], Never, R_co | Scope]Split the items into the Ok values of f(item) and the
Err ones. Upstream runs on a fiber in the current Scope; the
faster reader gets at most capacity items ahead of the slower.
| Parameter | Type | Description |
|---|---|---|
f | Callable[[A_co], Result[P, F]] | |
capacity | Capacity = 16 |
Returns Effect[tuple[Stream[P, E_co], Stream[F, E_co]], Never, R_co | Scope]
def partition[P, F](
self, f: Callable[[A_co], Result[P, F]], *, capacity: Capacity = 16
) -> Effect[tuple[Stream[P, E_co], Stream[F, E_co]], Never, R_co | Scope]:
"""Split the items into the ``Ok`` values of ``f(item)`` and the
``Err`` ones. Upstream runs on a fiber in the current ``Scope``; the
faster reader gets at most ``capacity`` items ahead of the slower."""
from pyfect.stream import _fanout
return _fanout.partition(self, f, capacity)partition_effect#
partition_effect(f, *, capacity=4096, concurrency=None) -> Effect[tuple[Stream[P, E_co | E2], Stream[F, E_co | E2]], Never, R_co | R2 | Scope]partition with an effectful f, run on up to
concurrency items at once (in order).
| Parameter | Type | Description |
|---|---|---|
f | Callable[[A_co], Effect[Result[P, F], E2, R2]] | |
capacity | Capacity = 4096 | |
concurrency | Concurrency = None |
Returns Effect[tuple[Stream[P, E_co | E2], Stream[F, E_co | E2]], Never, R_co | R2 | Scope]
def partition_effect[P, F, E2 = Never, R2 = Never](
self,
f: Callable[[A_co], Effect[Result[P, F], E2, R2]],
*,
capacity: Capacity = 4096,
concurrency: Concurrency = None,
) -> Effect[tuple[Stream[P, E_co | E2], Stream[F, E_co | E2]], Never, R_co | R2 | Scope]:
"""``partition`` with an effectful ``f``, run on up to
``concurrency`` items at once (in order)."""
from pyfect.stream import _fanout
return _fanout.partition_effect(self, f, capacity, concurrency)partition_queue#
partition_queue(f, *, capacity=4096) -> Effect[tuple[Dequeue[P, E_co | Done], Dequeue[F, E_co | Done]], Never, R_co | Scope]partition into two queues, which end (or fail) as the stream
does.
| Parameter | Type | Description |
|---|---|---|
f | Callable[[A_co], Result[P, F]] | |
capacity | Capacity = 4096 |
Returns Effect[tuple[Dequeue[P, E_co | Done], Dequeue[F, E_co | Done]], Never, R_co | Scope]
def partition_queue[P, F](
self, f: Callable[[A_co], Result[P, F]], *, capacity: Capacity = 4096
) -> Effect[tuple[Dequeue[P, E_co | Done], Dequeue[F, E_co | Done]], Never, R_co | Scope]:
"""``partition`` into two queues, which end (or fail) as the stream
does."""
from pyfect.stream import _fanout
return _fanout.partition_queue(self, f, capacity)broadcast#
broadcast(*, capacity, strategy='suspend', replay=None) -> Effect[Stream[A_co, E_co], Never, R_co | Scope]Run the stream on a fiber in the current Scope, publishing its
chunks to a pubsub of capacity chunks; each run of the returned
stream reads what is published from then on (and the last
replay chunks), then the end or failure.
| Parameter | Type | Description |
|---|---|---|
capacity | Capacity | |
strategy | Strategy = 'suspend' | |
replay | int | None = None |
Returns Effect[Stream[A_co, E_co], Never, R_co | Scope]
def broadcast(
self, *, capacity: Capacity, strategy: Strategy = "suspend", replay: int | None = None
) -> Effect[Stream[A_co, E_co], Never, R_co | Scope]:
"""Run the stream on a fiber in the current ``Scope``, publishing its
chunks to a pubsub of ``capacity`` chunks; each run of the returned
stream reads what is published from then on (and the last
``replay`` chunks), then the end or failure."""
from pyfect.stream import _fanout
return _fanout.broadcast(self, capacity, strategy, replay)broadcast_n#
broadcast_n(n, *, capacity, strategy='suspend', replay=None) -> Effect[tuple[Stream[A_co, E_co], ...], Never, R_co | Scope]n streams (none below 1) each reading every chunk. They
subscribe now, so none misses the start; with "suspend" the
upstream stays at most capacity chunks ahead of the slowest. A
stream that stops unsubscribes.
| Parameter | Type | Description |
|---|---|---|
n | int | |
capacity | Capacity | |
strategy | Strategy = 'suspend' | |
replay | int | None = None |
Returns Effect[tuple[Stream[A_co, E_co], ...], Never, R_co | Scope]
def broadcast_n(
self,
n: int,
*,
capacity: Capacity,
strategy: Strategy = "suspend",
replay: int | None = None,
) -> Effect[tuple[Stream[A_co, E_co], ...], Never, R_co | Scope]:
"""``n`` streams (none below 1) each reading every chunk. They
subscribe now, so none misses the start; with ``"suspend"`` the
upstream stays at most ``capacity`` chunks ahead of the slowest. A
stream that stops unsubscribes."""
from pyfect.stream import _fanout
return _fanout.broadcast_n(self, n, capacity, strategy, replay)share#
share(*, capacity, strategy='suspend', replay=None, idle_time_to_live=None) -> Effect[Stream[A_co, E_co], Never, R_co | Scope]A stream that broadcasts this one when its first reader
starts and stops it when the last one stops (or idle_time_to_live
after, so a later reader continues where it left off).
| Parameter | Type | Description |
|---|---|---|
capacity | Capacity | |
strategy | Strategy = 'suspend' | |
replay | int | None = None | |
idle_time_to_live | Duration | None = None |
Returns Effect[Stream[A_co, E_co], Never, R_co | Scope]
def share(
self,
*,
capacity: Capacity,
strategy: Strategy = "suspend",
replay: int | None = None,
idle_time_to_live: Duration | None = None,
) -> Effect[Stream[A_co, E_co], Never, R_co | Scope]:
"""A stream that ``broadcast``s this one when its first reader
starts and stops it when the last one stops (or ``idle_time_to_live``
after, so a later reader continues where it left off)."""
from pyfect.stream import _fanout
return _fanout.share(self, capacity, strategy, replay, idle_time_to_live)run#
run(sink) -> Effect[B, E_co | E2, R_co | R2]Run the stream into sink and succeed with the sink's value.
The stream's failures pass by the sink's error handlers.
| Parameter | Type | Description |
|---|---|---|
sink | Sink[B, A_co, L, E2, R2] |
Returns Effect[B, E_co | E2, R_co | R2]
def run[B, L, E2 = Never, R2 = Never](
self, sink: Sink[B, A_co, L, E2, R2]
) -> Effect[B, E_co | E2, R_co | R2]:
"""Run the stream into ``sink`` and succeed with the sink's value.
The stream's failures pass by the sink's error handlers."""
from pyfect.stream import _sinking
_require_sink(sink)
return _sinking.run(self, sink)transduce#
transduce(sink) -> Stream[Any, Any, Any]Run sink again and again on the stream, writing each value it
ends with; its leftovers start the next run. A stream failure ends
the current run first.
| Parameter | Type | Description |
|---|---|---|
sink | Sink[Any, Any, Any, Any, Any] |
Returns Stream[Any, Any, Any]
def transduce(self, sink: Sink[Any, Any, Any, Any, Any]) -> Stream[Any, Any, Any]:
"""Run ``sink`` again and again on the stream, writing each value it
ends with; its leftovers start the next run. A stream failure ends
the current run first."""
from pyfect.stream import _sinking
_require_sink(sink)
return _sinking.transduce(self, sink)aggregate#
aggregate(sink) -> Stream[Any, Any, Any]transduce while the stream keeps running: a fiber pulls the
stream as sink asks for input, and each run of the sink ends
when the sink is done.
| Parameter | Type | Description |
|---|---|---|
sink | Sink[Any, Any, Any, Any, Any] |
Returns Stream[Any, Any, Any]
def aggregate(self, sink: Sink[Any, Any, Any, Any, Any]) -> Stream[Any, Any, Any]:
"""``transduce`` while the stream keeps running: a fiber pulls the
stream as ``sink`` asks for input, and each run of the sink ends
when the sink is done."""
from pyfect import schedule
from pyfect.stream import _sinking
_require_sink(sink)
return _sinking.aggregate_within(self, sink, schedule.forever)aggregate_within#
aggregate_within(sink, schedule) -> Stream[Any, Any, Any]aggregate, also ending each run of sink when schedule
says so. The schedule steps once input arrives (with the last value
written) and its clock leaves out the time between runs; a schedule
that stops ends the stream once the sink has drained its
leftovers.
| Parameter | Type | Description |
|---|---|---|
sink | Sink[Any, Any, Any, Any, Any] | |
schedule | Schedule[Any, Any, Any, Any] |
Returns Stream[Any, Any, Any]
def aggregate_within(
self,
sink: Sink[Any, Any, Any, Any, Any],
schedule: Schedule[Any, Any, Any, Any],
) -> Stream[Any, Any, Any]:
"""``aggregate``, also ending each run of ``sink`` when ``schedule``
says so. The schedule steps once input arrives (with the last value
written) and its clock leaves out the time between runs; a schedule
that stops ends the stream once the sink has drained its
leftovers."""
from pyfect.stream import _sinking
_require_sink(sink)
return _sinking.aggregate_within(self, sink, schedule)grouped_within#
grouped_within(chunk_size, duration) -> Stream[list[A_co], E_co, R_co]Group the items in lists of up to chunk_size (at least 1),
ending a group early once duration has passed since its first
item.
| Parameter | Type | Description |
|---|---|---|
chunk_size | int | |
duration | Duration |
Returns Stream[list[A_co], E_co, R_co]
def grouped_within(self, chunk_size: int, duration: Duration) -> Stream[list[A_co], E_co, R_co]:
"""Group the items in lists of up to ``chunk_size`` (at least 1),
ending a group early once ``duration`` has passed since its first
item."""
from pyfect import schedule
from pyfect.sink import take
from pyfect.stream import _sinking
return _sinking.aggregate_within(self, take(max(chunk_size, 1)), schedule.spaced(duration))pipe_through#
pipe_through(sink) -> Stream[L, E_co | E2, R_co | R2]Run the stream into sink and write the sink's leftovers. The
stream's failures pass by the sink's error handlers.
| Parameter | Type | Description |
|---|---|---|
sink | Sink[B, A_co, L, E2, R2] |
Returns Stream[L, E_co | E2, R_co | R2]
def pipe_through[B, L, E2 = Never, R2 = Never](
self, sink: Sink[B, A_co, L, E2, R2]
) -> Stream[L, E_co | E2, R_co | R2]:
"""Run the stream into ``sink`` and write the sink's leftovers. The
stream's failures pass by the sink's error handlers."""
from pyfect.stream import _sinking
_require_sink(sink)
return _sinking.pipe_through(self, sink)tap_sink#
tap_sink(sink) -> Stream[A_co, E_co | E2, R_co | R2]Send each chunk to sink too, as it is written. The stream
waits for the sink to take each chunk; once the sink is done it
goes on alone, and a sink failure fails the stream.
| Parameter | Type | Description |
|---|---|---|
sink | Sink[object, A_co, object, E2, R2] |
Returns Stream[A_co, E_co | E2, R_co | R2]
def tap_sink[E2 = Never, R2 = Never](
self, sink: Sink[object, A_co, object, E2, R2]
) -> Stream[A_co, E_co | E2, R_co | R2]:
"""Send each chunk to ``sink`` too, as it is written. The stream
waits for the sink to take each chunk; once the sink is done it
goes on alone, and a sink failure fails the stream."""
from pyfect.stream import _sinking
_require_sink(sink)
return _sinking.tap_sink(self, sink)peel#
peel(sink) -> Effect[Any, Any, Any]Run sink on the start of the stream and succeed with its value
and the rest of the stream (the sink's leftovers first). The rest
reads the stream's pull, which lives in the current Scope.
| Parameter | Type | Description |
|---|---|---|
sink | Sink[Any, Any, Any, Any, Any] |
Returns Effect[Any, Any, Any]
def peel(self, sink: Sink[Any, Any, Any, Any, Any]) -> Effect[Any, Any, Any]:
"""Run ``sink`` on the start of the stream and succeed with its value
and the rest of the stream (the sink's leftovers first). The rest
reads the stream's pull, which lives in the current ``Scope``."""
from pyfect.stream import _sinking
_require_sink(sink)
return _sinking.peel(self, sink)run_collect#
run_collect() -> Effect[list[A_co], E_co, R_co]Run the stream and collect its items.
Returns Effect[list[A_co], E_co, R_co]
def run_collect(self) -> Effect[list[A_co], E_co, R_co]:
"""Run the stream and collect its items."""
from pyfect.stream import _run
return _run.run_collect(self)run_drain#
run_drain() -> Effect[None, E_co, R_co]Run the stream, dropping its items.
Returns Effect[None, E_co, R_co]
def run_drain(self) -> Effect[None, E_co, R_co]:
"""Run the stream, dropping its items."""
return self._channel.run_drain()run_count#
run_count() -> Effect[int, E_co, R_co]Run the stream and count its items.
Returns Effect[int, E_co, R_co]
def run_count(self) -> Effect[int, E_co, R_co]:
"""Run the stream and count its items."""
from pyfect.stream import _run
return _run.run_count(self)run_sum#
run_sum() -> Effect[N, E, R]Run a stream of numbers and add them up (0 when empty).
Returns Effect[N, E, R]
def run_sum[N: (int, float), E, R](self: RunMethods[N, E, R]) -> Effect[N, E, R]:
"""Run a stream of numbers and add them up (``0`` when empty)."""
from pyfect.stream import _run
return _run.run_sum(self)run_fold#
run_fold(initial, f) -> Effect[Z, E_co, R_co]Fold the items into initial() with f.
| Parameter | Type | Description |
|---|---|---|
initial | Callable[[], Z] | |
f | Callable[[Z, A_co], Z] |
Returns Effect[Z, E_co, R_co]
def run_fold[Z](
self, initial: Callable[[], Z], f: Callable[[Z, A_co], Z]
) -> Effect[Z, E_co, R_co]:
"""Fold the items into ``initial()`` with ``f``."""
from pyfect.stream import _run
return _run.run_fold(self, initial, f)run_fold_effect#
run_fold_effect(initial, f) -> Effect[Z, E_co | E2, R_co | R2]Fold the items into initial() with the effect f, one item
at a time.
| Parameter | Type | Description |
|---|---|---|
initial | Callable[[], Z] | |
f | Callable[[Z, A_co], Effect[Z, E2, R2]] |
Returns Effect[Z, E_co | E2, R_co | R2]
def run_fold_effect[Z, E2 = Never, R2 = Never](
self, initial: Callable[[], Z], f: Callable[[Z, A_co], Effect[Z, E2, R2]]
) -> Effect[Z, E_co | E2, R_co | R2]:
"""Fold the items into ``initial()`` with the effect ``f``, one item
at a time."""
from pyfect.stream import _run
return _run.run_fold_effect(self, initial, f)run_head#
run_head() -> Effect[Option[A_co], E_co, R_co]The first item (Some), or None for an empty stream. Stops
the stream after its first chunk.
Returns Effect[Option[A_co], E_co, R_co]
def run_head(self) -> Effect[Option[A_co], E_co, R_co]:
"""The first item (``Some``), or ``None`` for an empty stream. Stops
the stream after its first chunk."""
from pyfect.stream import _run
return _run.run_head(self)run_last#
run_last() -> Effect[Option[A_co], E_co, R_co]The last item (Some), or None for an empty stream.
Returns Effect[Option[A_co], E_co, R_co]
def run_last(self) -> Effect[Option[A_co], E_co, R_co]:
"""The last item (``Some``), or ``None`` for an empty stream."""
from pyfect.stream import _run
return _run.run_last(self)run_for_each#
run_for_each(f) -> Effect[None, E_co | E2, R_co | R2]Run f on each item, in order.
| Parameter | Type | Description |
|---|---|---|
f | Callable[[A_co], Effect[object, E2, R2]] |
Returns Effect[None, E_co | E2, R_co | R2]
def run_for_each[E2 = Never, R2 = Never](
self, f: Callable[[A_co], Effect[object, E2, R2]]
) -> Effect[None, E_co | E2, R_co | R2]:
"""Run ``f`` on each item, in order."""
from pyfect.stream import _run
return _run.run_for_each(self, f)run_for_each_while#
run_for_each_while(f) -> Effect[None, E_co | E2, R_co | R2]Run f on each item until it succeeds with False; the
stream stops there.
| Parameter | Type | Description |
|---|---|---|
f | Callable[[A_co], Effect[bool, E2, R2]] |
Returns Effect[None, E_co | E2, R_co | R2]
def run_for_each_while[E2 = Never, R2 = Never](
self, f: Callable[[A_co], Effect[bool, E2, R2]]
) -> Effect[None, E_co | E2, R_co | R2]:
"""Run ``f`` on each item until it succeeds with ``False``; the
stream stops there."""
from pyfect.stream import _run
return _run.run_for_each_while(self, f)run_for_each_array#
run_for_each_array(f) -> Effect[None, E_co | E2, R_co | R2]Run f on each chunk (a non-empty tuple), in order.
| Parameter | Type | Description |
|---|---|---|
f | Callable[[tuple[A_co, ...]], Effect[object, E2, R2]] |
Returns Effect[None, E_co | E2, R_co | R2]
def run_for_each_array[E2 = Never, R2 = Never](
self, f: Callable[[tuple[A_co, ...]], Effect[object, E2, R2]]
) -> Effect[None, E_co | E2, R_co | R2]:
"""Run ``f`` on each chunk (a non-empty tuple), in order."""
return self._channel.run_for_each(f)run_string#
run_string() -> Effect[str, E, R]Run a stream of strings and join them (Effect's mkString).
Returns Effect[str, E, R]
def run_string[E, R](self: RunMethods[str, E, R]) -> Effect[str, E, R]:
"""Run a stream of strings and join them (Effect's ``mkString``)."""
from pyfect.stream import _run
return _run.run_string(self)run_bytes#
run_bytes() -> Effect[bytes, E, R]Run a stream of byte strings and join them (Effect's
mkUint8Array and mkArrayBuffer).
Returns Effect[bytes, E, R]
def run_bytes[E, R](self: RunMethods[bytes, E, R]) -> Effect[bytes, E, R]:
"""Run a stream of byte strings and join them (Effect's
``mkUint8Array`` and ``mkArrayBuffer``)."""
from pyfect.stream import _run
return _run.run_bytes(self)to_pull#
to_pull() -> Effect[Pull[tuple[A_co, ...], E_co], Never, R_co | Scope]Build the stream's pull in the current Scope, which holds its
resources. Each pull succeeds with the next chunk or fails with
Done() at the end; fibers pulling at once take turns.
Returns Effect[Pull[tuple[A_co, ...], E_co], Never, R_co | Scope]
def to_pull(self) -> Effect[Pull[tuple[A_co, ...], E_co], Never, R_co | Scope]:
"""Build the stream's pull in the current ``Scope``, which holds its
resources. Each pull succeeds with the next chunk or fails with
``Done()`` at the end; fibers pulling at once take turns."""
return self._channel.to_pull()update_service#
update_service(tag, f) -> Stream[A_co, E_co, R_co | T]Run with f applied to the service of tag, which this
stream then needs.
| Parameter | Type | Description |
|---|---|---|
tag | TagLike[S, T] | |
f | Callable[[S], S] |
Returns Stream[A_co, E_co, R_co | T]
def update_service[S, T](
self, tag: TagLike[S, T], f: Callable[[S], S]
) -> Stream[A_co, E_co, R_co | T]:
"""Run with ``f`` applied to the service of ``tag``, which this
stream then needs."""
from pyfect.stream._stream import Stream
return Stream(self._channel.update_service(tag, f))with_span#
with_span(name, **options) -> Stream[A_co, E_co, R_co]Run inside a new span, the parent of the spans made inside. It ends with the stream's exit.
| Parameter | Type | Description |
|---|---|---|
name | str | |
options | Unpack[SpanOptions] = {} |
Returns Stream[A_co, E_co, R_co]
def with_span(self, name: str, **options: Unpack[SpanOptions]) -> Stream[A_co, E_co, R_co]:
"""Run inside a new span, the parent of the spans made inside. It
ends with the stream's exit."""
from pyfect.stream._stream import Stream
return Stream(self._channel.with_span(name, **options))schedule#
schedule(schedule) -> Stream[A_co, E_co | EE, R_co | RR]Step schedule with each item, writing it (one per chunk) after
the step's delay; end when the schedule stops, dropping the item
that stopped it.
| Parameter | Type | Description |
|---|---|---|
schedule | Schedule[object, A_co, EE, RR] |
Returns Stream[A_co, E_co | EE, R_co | RR]
def schedule[EE = Never, RR = Never](
self, schedule: Schedule[object, A_co, EE, RR]
) -> Stream[A_co, E_co | EE, R_co | RR]:
"""Step ``schedule`` with each item, writing it (one per chunk) after
the step's delay; end when the schedule stops, dropping the item
that stopped it."""
from pyfect.stream import _timing
return _timing.schedule(self, schedule)repeat#
repeat(schedule) -> Stream[Any, Any, Any]Run the stream again after each end, while schedule recurs.
Each run sees its step's schedule.CurrentMetadata.
| Parameter | Type | Description |
|---|---|---|
schedule | Schedule[Any, Any, Any, Any] | Callable[[Any], Any] |
Returns Stream[Any, Any, Any]
def repeat(
self, schedule: Schedule[Any, Any, Any, Any] | Callable[[Any], Any], /
) -> Stream[Any, Any, Any]:
"""Run the stream again after each end, while ``schedule`` recurs.
Each run sees its step's ``schedule.CurrentMetadata``."""
from pyfect.stream._stream import Stream
return Stream(self._channel.repeat(schedule))repeat_elements#
repeat_elements(schedule) -> Stream[A_co, E_co | EE, R_co | RR]Write each item, then again each time schedule (stepped with
it, fresh for each item) recurs.
| Parameter | Type | Description |
|---|---|---|
schedule | Schedule[object, A_co, EE, RR] |
Returns Stream[A_co, E_co | EE, R_co | RR]
def repeat_elements[EE = Never, RR = Never](
self, schedule: Schedule[object, A_co, EE, RR]
) -> Stream[A_co, E_co | EE, R_co | RR]:
"""Write each item, then again each time ``schedule`` (stepped with
it, fresh for each item) recurs."""
from pyfect.stream import _timing
return _timing.repeat_elements(self, schedule)forever#
forever() -> Stream[A_co, E_co, R_co]Run the stream again each time it ends, until it fails.
Returns Stream[A_co, E_co, R_co]
def forever(self) -> Stream[A_co, E_co, R_co]:
"""Run the stream again each time it ends, until it fails."""
from pyfect.stream._stream import Stream
forever: Any = self._channel.forever()
return Stream(forever)timeout#
timeout(duration) -> Stream[A_co, E_co, R_co]End the stream if a pull waits duration for its chunk.
| Parameter | Type | Description |
|---|---|---|
duration | Duration |
Returns Stream[A_co, E_co, R_co]
def timeout(self, duration: Duration) -> Stream[A_co, E_co, R_co]:
"""End the stream if a pull waits ``duration`` for its chunk."""
from pyfect.stream import _timing
return _timing.timeout_or_else(self, duration, None)timeout_or_else#
timeout_or_else(duration, or_else) -> Stream[A_co | B, E_co | E2, R_co | R2]Go on with or_else() if a pull waits duration for its
chunk.
| Parameter | Type | Description |
|---|---|---|
duration | Duration | |
or_else | Callable[[], Stream[B, E2, R2]] |
Returns Stream[A_co | B, E_co | E2, R_co | R2]
def timeout_or_else[B, E2 = Never, R2 = Never](
self, duration: Duration, or_else: Callable[[], Stream[B, E2, R2]]
) -> Stream[A_co | B, E_co | E2, R_co | R2]:
"""Go on with ``or_else()`` if a pull waits ``duration`` for its
chunk."""
from pyfect.stream import _timing
return _timing.timeout_or_else(self, duration, or_else)debounce#
debounce(duration) -> Stream[A_co, E_co, R_co]Write the last item of a burst once duration passes without a
new chunk; earlier items of the burst are dropped.
| Parameter | Type | Description |
|---|---|---|
duration | Duration |
Returns Stream[A_co, E_co, R_co]
def debounce(self, duration: Duration) -> Stream[A_co, E_co, R_co]:
"""Write the last item of a burst once ``duration`` passes without a
new chunk; earlier items of the burst are dropped."""
from pyfect.stream import _timing
return _timing.debounce(self, duration)throttle#
throttle(*, cost, units, duration, burst=0, strategy='shape') -> Stream[A_co, E_co, R_co]Limit the stream to units of cost per duration, with a
bucket of up to units + burst tokens: "shape" delays a chunk
until its tokens are there, "enforce" drops it.
| Parameter | Type | Description |
|---|---|---|
cost | Callable[[tuple[A_co, ...]], float] | |
units | float | |
duration | Duration | |
burst | float = 0 | |
strategy | ThrottleStrategy = 'shape' |
Returns Stream[A_co, E_co, R_co]
def throttle(
self,
*,
cost: Callable[[tuple[A_co, ...]], float],
units: float,
duration: Duration,
burst: float = 0,
strategy: ThrottleStrategy = "shape",
) -> Stream[A_co, E_co, R_co]:
"""Limit the stream to ``units`` of ``cost`` per ``duration``, with a
bucket of up to ``units + burst`` tokens: ``"shape"`` delays a chunk
until its tokens are there, ``"enforce"`` drops it."""
from pyfect.effect import succeed
from pyfect.stream import _timing
return _timing.throttle(
self,
lambda chunk: succeed(cost(chunk)),
units,
duration,
burst=burst,
strategy=strategy,
)throttle_effect#
throttle_effect(*, cost, units, duration, burst=0, strategy='shape') -> Stream[A_co, E_co | E2, R_co | R2]throttle with an effectful cost.
| Parameter | Type | Description |
|---|---|---|
cost | Callable[[tuple[A_co, ...]], Effect[float, E2, R2]] | |
units | float | |
duration | Duration | |
burst | float = 0 | |
strategy | ThrottleStrategy = 'shape' |
Returns Stream[A_co, E_co | E2, R_co | R2]
def throttle_effect[E2 = Never, R2 = Never](
self,
*,
cost: Callable[[tuple[A_co, ...]], Effect[float, E2, R2]],
units: float,
duration: Duration,
burst: float = 0,
strategy: ThrottleStrategy = "shape",
) -> Stream[A_co, E_co | E2, R_co | R2]:
"""``throttle`` with an effectful ``cost``."""
from pyfect.stream import _timing
return _timing.throttle(self, cost, units, duration, burst=burst, strategy=strategy)buffer#
buffer(*, capacity, strategy='suspend') -> Stream[A_co, E_co, R_co]Pull ahead on a fiber, keeping up to capacity items for the
reader; a full buffer waits, or drops the new ("dropping") or
oldest ("sliding") items.
| Parameter | Type | Description |
|---|---|---|
capacity | int | Literal['unbounded'] | |
strategy | Strategy = 'suspend' |
Returns Stream[A_co, E_co, R_co]
def buffer(
self, *, capacity: int | Literal["unbounded"], strategy: Strategy = "suspend"
) -> Stream[A_co, E_co, R_co]:
"""Pull ahead on a fiber, keeping up to ``capacity`` items for the
reader; a full buffer waits, or drops the new (``"dropping"``) or
oldest (``"sliding"``) items."""
from pyfect.stream._stream import Stream
if capacity == "unbounded":
return Stream(self._channel.buffer_array(capacity=capacity))
return Stream(self._channel.buffer_array(capacity=capacity, strategy=strategy))buffer_array#
buffer_array(*, capacity, strategy='suspend') -> Stream[A_co, E_co, R_co]buffer counting chunks rather than items.
| Parameter | Type | Description |
|---|---|---|
capacity | int | Literal['unbounded'] | |
strategy | Strategy = 'suspend' |
Returns Stream[A_co, E_co, R_co]
def buffer_array(
self, *, capacity: int | Literal["unbounded"], strategy: Strategy = "suspend"
) -> Stream[A_co, E_co, R_co]:
"""``buffer`` counting chunks rather than items."""
from pyfect.stream._stream import Stream
if capacity == "unbounded":
return Stream(self._channel.buffer(capacity=capacity))
return Stream(self._channel.buffer(capacity=capacity, strategy=strategy))halt_when#
halt_when(eff) -> Stream[A_co, E_co | E2, R_co | R2]Run eff alongside; once it has finished, the next pull ends
the stream (or fails with eff's failure) instead of pulling.
| Parameter | Type | Description |
|---|---|---|
eff | Effect[object, E2, R2] |
Returns Stream[A_co, E_co | E2, R_co | R2]
def halt_when[E2 = Never, R2 = Never](
self, eff: Effect[object, E2, R2]
) -> Stream[A_co, E_co | E2, R_co | R2]:
"""Run ``eff`` alongside; once it has finished, the next pull ends
the stream (or fails with ``eff``'s failure) instead of pulling."""
from pyfect._effect_args import require_effect
from pyfect.stream._stream import Stream
require_effect(eff, "eff")
halted: Any = self._channel.halt_when(eff)
return Stream(halted)interrupt_when#
interrupt_when(eff) -> Stream[A_co, E_co | E2, R_co | R2]Run eff alongside; when it succeeds, interrupt the stream and
end; when it fails, fail.
| Parameter | Type | Description |
|---|---|---|
eff | Effect[object, E2, R2] |
Returns Stream[A_co, E_co | E2, R_co | R2]
def interrupt_when[E2 = Never, R2 = Never](
self, eff: Effect[object, E2, R2]
) -> Stream[A_co, E_co | E2, R_co | R2]:
"""Run ``eff`` alongside; when it succeeds, interrupt the stream and
end; when it fails, fail."""
from pyfect._effect_args import require_effect
from pyfect.stream._stream import Stream
require_effect(eff, "eff")
interrupted: Any = self._channel.interrupt_when(eff)
return Stream(interrupted)on_start#
on_start(eff) -> Stream[A_co, E_co | E2, R_co | R2]Run eff on the first pull, before the stream starts.
| Parameter | Type | Description |
|---|---|---|
eff | Effect[object, E2, R2] |
Returns Stream[A_co, E_co | E2, R_co | R2]
def on_start[E2 = Never, R2 = Never](
self, eff: Effect[object, E2, R2]
) -> Stream[A_co, E_co | E2, R_co | R2]:
"""Run ``eff`` on the first pull, before the stream starts."""
from pyfect._effect_args import require_effect
from pyfect.stream._stream import Stream
require_effect(eff, "eff")
return Stream(self._channel.on_start(eff))on_first#
on_first(f) -> Stream[A_co, E_co | E2, R_co | R2]Run f on the first item, before writing it.
| Parameter | Type | Description |
|---|---|---|
f | Callable[[A_co], Effect[object, E2, R2]] |
Returns Stream[A_co, E_co | E2, R_co | R2]
def on_first[E2 = Never, R2 = Never](
self, f: Callable[[A_co], Effect[object, E2, R2]]
) -> Stream[A_co, E_co | E2, R_co | R2]:
"""Run ``f`` on the first item, before writing it."""
from pyfect.stream._stream import Stream
return Stream(self._channel.on_first(lambda chunk: f(chunk[0])))on_end#
on_end(eff) -> Stream[A_co, E_co | E2, R_co | R2]Run eff when the stream ends (not when it fails).
| Parameter | Type | Description |
|---|---|---|
eff | Effect[object, E2, R2] |
Returns Stream[A_co, E_co | E2, R_co | R2]
def on_end[E2 = Never, R2 = Never](
self, eff: Effect[object, E2, R2]
) -> Stream[A_co, E_co | E2, R_co | R2]:
"""Run ``eff`` when the stream ends (not when it fails)."""
from pyfect._effect_args import require_effect
from pyfect.stream._stream import Stream
require_effect(eff, "eff")
return Stream(self._channel.on_end(eff))on_exit#
on_exit(f) -> Stream[A_co, E_co, R_co | R2]Run f(exit) as soon as the stream ends or fails, or when its
run stops early.
| Parameter | Type | Description |
|---|---|---|
f | Callable[[Exit[None, E_co]], Effect[object, Never, R2]] |
Returns Stream[A_co, E_co, R_co | R2]
def on_exit[R2 = Never](
self, f: Callable[[Exit[None, E_co]], Effect[object, Never, R2]]
) -> Stream[A_co, E_co, R_co | R2]:
"""Run ``f(exit)`` as soon as the stream ends or fails, or when its
run stops early."""
from pyfect.stream._stream import Stream
return Stream(self._channel.on_exit(f))on_error#
on_error(f) -> Stream[A_co, E_co, R_co | R2]on_exit for a failure (or an early stop that interrupts).
| Parameter | Type | Description |
|---|---|---|
f | Callable[[Cause[E_co]], Effect[object, Never, R2]] |
Returns Stream[A_co, E_co, R_co | R2]
def on_error[R2 = Never](
self, f: Callable[[Cause[E_co]], Effect[object, Never, R2]]
) -> Stream[A_co, E_co, R_co | R2]:
"""``on_exit`` for a failure (or an early stop that interrupts)."""
from pyfect.stream._stream import Stream
return Stream(self._channel.on_error(f))ensuring#
ensuring(finalizer) -> Stream[A_co, E_co, R_co | R2]Run finalizer however the stream stops.
| Parameter | Type | Description |
|---|---|---|
finalizer | Effect[object, Never, R2] |
Returns Stream[A_co, E_co, R_co | R2]
def ensuring[R2 = Never](
self, finalizer: Effect[object, Never, R2]
) -> Stream[A_co, E_co, R_co | R2]:
"""Run ``finalizer`` however the stream stops."""
from pyfect._effect_args import require_effect
from pyfect.stream._stream import Stream
require_effect(finalizer, "finalizer")
return Stream(self._channel.ensuring(finalizer))catch_cause#
catch_cause(f) -> Stream[A_co | B, E2, R_co | R2]On any failure, defects included, release this stream's resources
and go on with f(cause).
| Parameter | Type | Description |
|---|---|---|
f | Callable[[Cause[E_co]], Stream[B, E2, R2]] |
Returns Stream[A_co | B, E2, R_co | R2]
def catch_cause[B, E2 = Never, R2 = Never](
self, f: Callable[[Cause[E_co]], Stream[B, E2, R2]]
) -> Stream[A_co | B, E2, R_co | R2]:
"""On any failure, defects included, release this stream's resources
and go on with ``f(cause)``."""
from pyfect.stream._stream import Stream
return Stream(self._channel.catch_cause(lambda cause: f(cause)._channel))catch_cause_if#
catch_cause_if(predicate, f) -> Stream[A_co | B, E_co | E2, R_co | R2]catch_cause for the causes that pass predicate.
| Parameter | Type | Description |
|---|---|---|
predicate | Callable[[Cause[E_co]], bool] | |
f | Callable[[Cause[E_co]], Stream[B, E2, R2]] |
Returns Stream[A_co | B, E_co | E2, R_co | R2]
def catch_cause_if[B, E2 = Never, R2 = Never](
self,
predicate: Callable[[Cause[E_co]], bool],
f: Callable[[Cause[E_co]], Stream[B, E2, R2]],
) -> Stream[A_co | B, E_co | E2, R_co | R2]:
"""``catch_cause`` for the causes that pass ``predicate``."""
from pyfect.stream._stream import Stream
return Stream(self._channel.catch_cause_if(predicate, lambda cause: f(cause)._channel))catch_all#
catch_all(f) -> Stream[A_co | B, E2, R_co | R2]Go on with f(error) after a typed failure (Effect's
Stream.catch). Defects and interruptions pass through.
| Parameter | Type | Description |
|---|---|---|
f | Callable[[E_co], Stream[B, E2, R2]] |
Returns Stream[A_co | B, E2, R_co | R2]
def catch_all[B, E2 = Never, R2 = Never](
self, f: Callable[[E_co], Stream[B, E2, R2]]
) -> Stream[A_co | B, E2, R_co | R2]:
"""Go on with ``f(error)`` after a typed failure (Effect's
``Stream.catch``). Defects and interruptions pass through."""
from pyfect.stream._stream import Stream
return Stream(self._channel.catch_all(lambda error: f(error)._channel))catch_if#
catch_if(predicate, f, *, or_else=None) -> Stream[Any, Any, Any]Go on with f(error) after a typed failure that passes
predicate; or_else, if given, handles the rest. To remove the
caught errors from the type, use pipe(stream.catch_if(...)) with
a TypeIs refinement.
| Parameter | Type | Description |
|---|---|---|
predicate | Callable[[Any], bool] | |
f | Callable[[Any], Stream[Any, Any, Any]] | |
or_else | Callable[[Any], Stream[Any, Any, Any]] | None = None |
Returns Stream[Any, Any, Any]
def catch_if(
self,
predicate: Callable[[Any], bool],
f: Callable[[Any], Stream[Any, Any, Any]],
*,
or_else: Callable[[Any], Stream[Any, Any, Any]] | None = None,
) -> Stream[Any, Any, Any]:
"""Go on with ``f(error)`` after a typed failure that passes
``predicate``; ``or_else``, if given, handles the rest. To remove the
caught errors from the type, use ``pipe(stream.catch_if(...))`` with
a ``TypeIs`` refinement."""
from pyfect.stream import _catch
return _catch.catch_if(self, predicate, f, or_else)catch_filter#
catch_filter(filter, f, *, or_else=None) -> Stream[Any, Any, Any]Go on with f(value) after a typed failure filter maps to
Ok(value). One it maps to Err(x) goes to or_else(x), or
fails with x.
| Parameter | Type | Description |
|---|---|---|
filter | Callable[[Any], Result[Any, Any]] | |
f | Callable[[Any], Stream[Any, Any, Any]] | |
or_else | Callable[[Any], Stream[Any, Any, Any]] | None = None |
Returns Stream[Any, Any, Any]
def catch_filter(
self,
filter: Callable[[Any], Result[Any, Any]],
f: Callable[[Any], Stream[Any, Any, Any]],
*,
or_else: Callable[[Any], Stream[Any, Any, Any]] | None = None,
) -> Stream[Any, Any, Any]:
"""Go on with ``f(value)`` after a typed failure ``filter`` maps to
``Ok(value)``. One it maps to ``Err(x)`` goes to ``or_else(x)``, or
fails with ``x``."""
from pyfect.stream._constructors import fail
from pyfect.stream._stream import Stream
def handle(error: Any) -> Any:
match filter(error):
case Ok(value):
return f(value)._channel
case Err(rest):
return (fail(rest) if or_else is None else or_else(rest))._channel
return Stream(self._channel.catch_all(handle))catch_cause_filter#
catch_cause_filter(filter, f) -> Stream[A_co | B, X | E2, R_co | R2]Go on with f(value, cause) after a failure filter maps to
Ok(value); one it maps to Err(rest) fails with rest.
| Parameter | Type | Description |
|---|---|---|
filter | Callable[[Cause[E_co]], Result[EB, Cause[X]]] | |
f | Callable[[EB, Cause[E_co]], Stream[B, E2, R2]] |
Returns Stream[A_co | B, X | E2, R_co | R2]
def catch_cause_filter[EB, X, B, E2 = Never, R2 = Never](
self,
filter: Callable[[Cause[E_co]], Result[EB, Cause[X]]],
f: Callable[[EB, Cause[E_co]], Stream[B, E2, R2]],
) -> Stream[A_co | B, X | E2, R_co | R2]:
"""Go on with ``f(value, cause)`` after a failure ``filter`` maps to
``Ok(value)``; one it maps to ``Err(rest)`` fails with ``rest``."""
from pyfect.stream._constructors import fail_cause
from pyfect.stream._stream import Stream
def handle(cause: Cause[E_co]) -> Any:
match filter(cause):
case Ok(value):
return f(value, cause)._channel
case Err(rest):
return fail_cause(rest)._channel
return Stream(self._channel.catch_cause(handle))catch_defect#
catch_defect(f) -> Stream[A_co | B, E_co | E2, R_co | R2]Go on with f(defect) after a failure with a defect. Typed
errors pass through.
| Parameter | Type | Description |
|---|---|---|
f | Callable[[BaseException], Stream[B, E2, R2]] |
Returns Stream[A_co | B, E_co | E2, R_co | R2]
def catch_defect[B, E2 = Never, R2 = Never](
self, f: Callable[[BaseException], Stream[B, E2, R2]]
) -> Stream[A_co | B, E_co | E2, R_co | R2]:
"""Go on with ``f(defect)`` after a failure with a defect. Typed
errors pass through."""
from pyfect.stream._stream import Stream
return Stream(self._channel.catch_defect(lambda defect: f(defect)._channel))or_else_if_empty#
or_else_if_empty(or_else) -> Stream[A_co | B, E_co | E2, R_co | R2]Go on with or_else() if this stream ends without writing.
| Parameter | Type | Description |
|---|---|---|
or_else | Callable[[], Stream[B, E2, R2]] |
Returns Stream[A_co | B, E_co | E2, R_co | R2]
def or_else_if_empty[B, E2 = Never, R2 = Never](
self, or_else: Callable[[], Stream[B, E2, R2]]
) -> Stream[A_co | B, E_co | E2, R_co | R2]:
"""Go on with ``or_else()`` if this stream ends without writing."""
from pyfect.stream._stream import Stream
return Stream(self._channel.or_else_if_empty(lambda _: or_else()._channel))or_else_succeed#
or_else_succeed(f) -> Stream[A_co | B, Never, R_co]Write f(error) and end instead of failing with a typed error.
| Parameter | Type | Description |
|---|---|---|
f | Callable[[E_co], B] |
Returns Stream[A_co | B, Never, R_co]
def or_else_succeed[B](self, f: Callable[[E_co], B]) -> Stream[A_co | B, Never, R_co]:
"""Write ``f(error)`` and end instead of failing with a typed error."""
from pyfect.stream._constructors import succeed
return self.catch_all(lambda error: succeed(f(error)))tap_cause#
tap_cause(f) -> Stream[A_co, E_co | E2, R_co | R2]Run f(cause) when the stream fails, then fail the same way.
| Parameter | Type | Description |
|---|---|---|
f | Callable[[Cause[E_co]], Effect[object, E2, R2]] |
Returns Stream[A_co, E_co | E2, R_co | R2]
def tap_cause[E2 = Never, R2 = Never](
self, f: Callable[[Cause[E_co]], Effect[object, E2, R2]]
) -> Stream[A_co, E_co | E2, R_co | R2]:
"""Run ``f(cause)`` when the stream fails, then fail the same way."""
from pyfect.stream._stream import Stream
return Stream(self._channel.tap_cause(f))tap_error#
tap_error(f) -> Stream[A_co, E_co | E2, R_co | R2]Run f(error) when the stream fails with a typed error.
| Parameter | Type | Description |
|---|---|---|
f | Callable[[E_co], Effect[object, E2, R2]] |
Returns Stream[A_co, E_co | E2, R_co | R2]
def tap_error[E2 = Never, R2 = Never](
self, f: Callable[[E_co], Effect[object, E2, R2]]
) -> Stream[A_co, E_co | E2, R_co | R2]:
"""Run ``f(error)`` when the stream fails with a typed error."""
from pyfect.stream._stream import Stream
return Stream(self._channel.tap_error(f))tap_error_tag#
tap_error_tag(tag, f) -> Stream[Any, Any, Any]tap_error for the errors that are instances of tag (a
class or a tuple of classes).
| Parameter | Type | Description |
|---|---|---|
tag | type[Any] | tuple[type[Any], ...] | |
f | Callable[[Any], Effect[object, Any, Any]] |
Returns Stream[Any, Any, Any]
def tap_error_tag(
self,
tag: type[Any] | tuple[type[Any], ...],
f: Callable[[Any], Effect[object, Any, Any]],
) -> Stream[Any, Any, Any]:
"""``tap_error`` for the errors that are instances of ``tag`` (a
class or a tuple of classes)."""
from pyfect.effect import void
return self.tap_error(lambda e: f(e) if isinstance(e, tag) else void)tap_defect#
tap_defect(f) -> Stream[A_co, E_co | E2, R_co | R2]Run f(defect) when the stream fails with a defect.
| Parameter | Type | Description |
|---|---|---|
f | Callable[[BaseException], Effect[object, E2, R2]] |
Returns Stream[A_co, E_co | E2, R_co | R2]
def tap_defect[E2 = Never, R2 = Never](
self, f: Callable[[BaseException], Effect[object, E2, R2]]
) -> Stream[A_co, E_co | E2, R_co | R2]:
"""Run ``f(defect)`` when the stream fails with a defect."""
from pyfect.effect import void
def tap(cause: Cause[E_co]) -> Effect[object, E2, R2]:
defect = cause.find_defect()
return void if defect is None else f(defect)
return self.tap_cause(tap)map_error#
map_error(f) -> Stream[A_co, E2, R_co]Apply f to the typed error. Defects and interruptions stay.
| Parameter | Type | Description |
|---|---|---|
f | Callable[[E_co], E2] |
Returns Stream[A_co, E2, R_co]
def map_error[E2](self, f: Callable[[E_co], E2]) -> Stream[A_co, E2, R_co]:
"""Apply ``f`` to the typed error. Defects and interruptions stay."""
from pyfect.stream._stream import Stream
return Stream(self._channel.map_error(f))or_die#
or_die() -> Stream[A_co, Never, R_co]Die with the typed error instead of failing with it.
Returns Stream[A_co, Never, R_co]
def or_die(self) -> Stream[A_co, Never, R_co]:
"""Die with the typed error instead of failing with it."""
from pyfect.stream._stream import Stream
return Stream(self._channel.or_die())ignore#
ignore(*, log=False) -> Stream[A_co, Never, R_co]End instead of failing with a typed error. With log, log the
cause first, at the current log level (True) or the level
given.
| Parameter | Type | Description |
|---|---|---|
log | bool | Severity = False |
Returns Stream[A_co, Never, R_co]
def ignore(self, *, log: bool | Severity = False) -> Stream[A_co, Never, R_co]:
"""End instead of failing with a typed error. With ``log``, log the
cause first, at the current log level (``True``) or the level
given."""
from pyfect.stream._stream import Stream
ignored: Any = self._channel.ignore(log=log)
return Stream(ignored)ignore_cause#
ignore_cause(*, log=False) -> Stream[A_co, Never, R_co]End instead of failing in any way. log as for ignore.
| Parameter | Type | Description |
|---|---|---|
log | bool | Severity = False |
Returns Stream[A_co, Never, R_co]
def ignore_cause(self, *, log: bool | Severity = False) -> Stream[A_co, Never, R_co]:
"""End instead of failing in any way. ``log`` as for ``ignore``."""
from pyfect.stream._stream import Stream
ignored: Any = self._channel.ignore_cause(log=log)
return Stream(ignored)retry#
retry(policy) -> Stream[Any, Any, Any]Run the stream again from the start after each typed failure,
while policy recurs; then fail with the last error. Items
already written stay written. A run that writes an item resets the
policy; each run sees its step's schedule.CurrentMetadata.
| Parameter | Type | Description |
|---|---|---|
policy | Schedule[Any, Any, Any, Any] | Callable[[Any], Any] |
Returns Stream[Any, Any, Any]
def retry(
self, policy: Schedule[Any, Any, Any, Any] | Callable[[Any], Any], /
) -> Stream[Any, Any, Any]:
"""Run the stream again from the start after each typed failure,
while ``policy`` recurs; then fail with the last error. Items
already written stay written. A run that writes an item resets the
policy; each run sees its step's ``schedule.CurrentMetadata``."""
from pyfect.stream._stream import Stream
return Stream(self._channel.retry(policy))zip#
zip(that) -> Stream[tuple[A_co, B], E_co | E2, R_co | R2]Pair the items of both streams in order, pulling both at once; end when either ends.
| Parameter | Type | Description |
|---|---|---|
that | Stream[B, E2, R2] |
Returns Stream[tuple[A_co, B], E_co | E2, R_co | R2]
def zip[B, E2 = Never, R2 = Never](
self, that: Stream[B, E2, R2]
) -> Stream[tuple[A_co, B], E_co | E2, R_co | R2]:
"""Pair the items of both streams in order, pulling both at once;
end when either ends."""
require_stream(that, "that")
return self.zip_with(that, _pair)zip_with#
zip_with(that, f) -> Stream[C, E_co | E2, R_co | R2]zip, writing f(left, right) for each pair.
| Parameter | Type | Description |
|---|---|---|
that | Stream[B, E2, R2] | |
f | Callable[[A_co, B], C] |
Returns Stream[C, E_co | E2, R_co | R2]
def zip_with[B, C, E2 = Never, R2 = Never](
self, that: Stream[B, E2, R2], f: Callable[[A_co, B], C]
) -> Stream[C, E_co | E2, R_co | R2]:
"""``zip``, writing ``f(left, right)`` for each pair."""
from pyfect.stream import _zipping
require_stream(that, "that")
return _zipping.zip_with(self, that, f)zip_with_array#
zip_with_array(that, f) -> Stream[C, E_co | E2, R_co | R2]Zip chunk by chunk: f(left, right) gives the chunk to write
and the left and right items it left over, which the next call gets
before anything new is pulled from that side.
| Parameter | Type | Description |
|---|---|---|
that | Stream[B, E2, R2] | |
f | Callable[[tuple[A_co, ...], tuple[B, ...]], tuple[tuple[C, ...], tuple[A_co, ...], tuple[B, ...]]] |
Returns Stream[C, E_co | E2, R_co | R2]
def zip_with_array[B, C, E2 = Never, R2 = Never](
self,
that: Stream[B, E2, R2],
f: Callable[
[tuple[A_co, ...], tuple[B, ...]],
tuple[tuple[C, ...], tuple[A_co, ...], tuple[B, ...]],
],
) -> Stream[C, E_co | E2, R_co | R2]:
"""Zip chunk by chunk: ``f(left, right)`` gives the chunk to write
and the left and right items it left over, which the next call gets
before anything new is pulled from that side."""
from pyfect.stream import _zipping
require_stream(that, "that")
return _zipping.zip_with_array(self, that, f)zip_left#
zip_left(that) -> Stream[A_co, E_co | E2, R_co | R2]zip, keeping the left items.
| Parameter | Type | Description |
|---|---|---|
that | Stream[object, E2, R2] |
Returns Stream[A_co, E_co | E2, R_co | R2]
def zip_left[E2 = Never, R2 = Never](
self, that: Stream[object, E2, R2]
) -> Stream[A_co, E_co | E2, R_co | R2]:
"""``zip``, keeping the left items."""
require_stream(that, "that")
return self.zip_with(that, _left)zip_right#
zip_right(that) -> Stream[B, E_co | E2, R_co | R2]zip, keeping the right items.
| Parameter | Type | Description |
|---|---|---|
that | Stream[B, E2, R2] |
Returns Stream[B, E_co | E2, R_co | R2]
def zip_right[B, E2 = Never, R2 = Never](
self, that: Stream[B, E2, R2]
) -> Stream[B, E_co | E2, R_co | R2]:
"""``zip``, keeping the right items."""
require_stream(that, "that")
return self.zip_with(that, _right)zip_flatten#
zip_flatten(that) -> Stream[tuple[*Ts, B], E_co | E2, R_co | R2]zip for a stream of tuples: append each right item to its
left tuple.
| Parameter | Type | Description |
|---|---|---|
that | Stream[B, E2, R2] |
Returns Stream[tuple[*Ts, B], E_co | E2, R_co | R2]
def zip_flatten[*Ts, B, E2 = Never, R2 = Never](
self: ZipMethods[tuple[*Ts], E_co, R_co], that: Stream[B, E2, R2]
) -> Stream[tuple[*Ts, B], E_co | E2, R_co | R2]:
"""``zip`` for a stream of tuples: append each right item to its
left tuple."""
require_stream(that, "that")
return self.zip_with(that, lambda left, right: (*left, right))zip_latest#
zip_latest(that) -> Stream[tuple[A_co, B], E_co | E2, R_co | R2]Once both streams have written, pair each new item with the other stream's latest; end when either ends.
| Parameter | Type | Description |
|---|---|---|
that | Stream[B, E2, R2] |
Returns Stream[tuple[A_co, B], E_co | E2, R_co | R2]
def zip_latest[B, E2 = Never, R2 = Never](
self, that: Stream[B, E2, R2]
) -> Stream[tuple[A_co, B], E_co | E2, R_co | R2]:
"""Once both streams have written, pair each new item with the
other stream's latest; end when either ends."""
from pyfect.stream import _zipping
require_stream(that, "that")
return _zipping.zip_latest_all(self, that)zip_latest_with#
zip_latest_with(that, f) -> Stream[C, E_co | E2, R_co | R2]zip_latest, writing f(left, right) for each pair.
| Parameter | Type | Description |
|---|---|---|
that | Stream[B, E2, R2] | |
f | Callable[[A_co, B], C] |
Returns Stream[C, E_co | E2, R_co | R2]
def zip_latest_with[B, C, E2 = Never, R2 = Never](
self, that: Stream[B, E2, R2], f: Callable[[A_co, B], C]
) -> Stream[C, E_co | E2, R_co | R2]:
"""``zip_latest``, writing ``f(left, right)`` for each pair."""
require_stream(that, "that")
return self.zip_latest(that).map(lambda pair: f(*pair))cross#
cross(that) -> Stream[tuple[A_co, B], E_co | E2, R_co | R2]Every pair of a left and a right item: that runs again for
each left item.
| Parameter | Type | Description |
|---|---|---|
that | Stream[B, E2, R2] |
Returns Stream[tuple[A_co, B], E_co | E2, R_co | R2]
def cross[B, E2 = Never, R2 = Never](
self, that: Stream[B, E2, R2]
) -> Stream[tuple[A_co, B], E_co | E2, R_co | R2]:
"""Every pair of a left and a right item: ``that`` runs again for
each left item."""
require_stream(that, "that")
return self.cross_with(that, _pair)cross_with#
cross_with(that, f) -> Stream[C, E_co | E2, R_co | R2]cross, writing f(left, right) for each pair.
| Parameter | Type | Description |
|---|---|---|
that | Stream[B, E2, R2] | |
f | Callable[[A_co, B], C] |
Returns Stream[C, E_co | E2, R_co | R2]
def cross_with[B, C, E2 = Never, R2 = Never](
self, that: Stream[B, E2, R2], f: Callable[[A_co, B], C]
) -> Stream[C, E_co | E2, R_co | R2]:
"""``cross``, writing ``f(left, right)`` for each pair."""
from pyfect.stream._base import as_stream
require_stream(that, "that")
return as_stream(self).flat_map(lambda left: that.map(lambda right: f(left, right)))merge#
merge(that, *, halt_strategy='both') -> Stream[A_co | B, E_co | E2, R_co | R2]Run both streams at once, writing items as either writes them.
The merge ends when both have ended, or just the "left" (this)
or "right" one, or "either"; a failure of either fails it.
| Parameter | Type | Description |
|---|---|---|
that | Stream[B, E2, R2] | |
halt_strategy | HaltStrategy = 'both' |
Returns Stream[A_co | B, E_co | E2, R_co | R2]
def merge[B, E2 = Never, R2 = Never](
self, that: Stream[B, E2, R2], *, halt_strategy: HaltStrategy = "both"
) -> Stream[A_co | B, E_co | E2, R_co | R2]:
"""Run both streams at once, writing items as either writes them.
The merge ends when both have ended, or just the ``"left"`` (this)
or ``"right"`` one, or ``"either"``; a failure of either fails it."""
from pyfect.stream._stream import Stream
require_stream(that, "that")
return Stream(self._channel.merge(that._channel, halt_strategy=halt_strategy))merge_effect#
merge_effect(eff) -> Stream[A_co, E_co | E2, R_co | R2]Run eff alongside the stream: its failure fails the stream;
it is interrupted when the stream ends.
| Parameter | Type | Description |
|---|---|---|
eff | Effect[object, E2, R2] |
Returns Stream[A_co, E_co | E2, R_co | R2]
def merge_effect[E2 = Never, R2 = Never](
self, eff: Effect[object, E2, R2]
) -> Stream[A_co, E_co | E2, R_co | R2]:
"""Run ``eff`` alongside the stream: its failure fails the stream;
it is interrupted when the stream ends."""
from pyfect._effect_args import require_effect
from pyfect.stream._stream import Stream
require_effect(eff, "eff")
return Stream(self._channel.merge_effect(eff))merge_result#
merge_result(that) -> Stream[Result[A_co, B], E_co | E2, R_co | R2]merge, writing this stream's items as Ok and that's as
Err.
| Parameter | Type | Description |
|---|---|---|
that | Stream[B, E2, R2] |
Returns Stream[Result[A_co, B], E_co | E2, R_co | R2]
def merge_result[B, E2 = Never, R2 = Never](
self, that: Stream[B, E2, R2]
) -> Stream[Result[A_co, B], E_co | E2, R_co | R2]:
"""``merge``, writing this stream's items as ``Ok`` and ``that``'s as
``Err``."""
from pyfect.result import Err, Ok
from pyfect.stream._base import as_stream
require_stream(that, "that")
lefts: Stream[Result[A_co, B], E_co, R_co] = as_stream(self).map(Ok)
return lefts.merge(that.map(Err))merge_left#
merge_left(that) -> Stream[A_co, E_co | E2, R_co | R2]Run that alongside for its effects, writing this stream's
items.
| Parameter | Type | Description |
|---|---|---|
that | Stream[object, E2, R2] |
Returns Stream[A_co, E_co | E2, R_co | R2]
def merge_left[E2 = Never, R2 = Never](
self, that: Stream[object, E2, R2]
) -> Stream[A_co, E_co | E2, R_co | R2]:
"""Run ``that`` alongside for its effects, writing this stream's
items."""
require_stream(that, "that")
return self.merge_effect(that.run_drain())merge_right#
merge_right(that) -> Stream[B, E_co | E2, R_co | R2]Run this stream alongside for its effects, writing that's
items.
| Parameter | Type | Description |
|---|---|---|
that | Stream[B, E2, R2] |
Returns Stream[B, E_co | E2, R_co | R2]
def merge_right[B, E2 = Never, R2 = Never](
self, that: Stream[B, E2, R2]
) -> Stream[B, E_co | E2, R_co | R2]:
"""Run this stream alongside for its effects, writing ``that``'s
items."""
from pyfect.stream._base import as_stream
require_stream(that, "that")
return that.merge_effect(as_stream(self).run_drain())combine#
combine(that, initial, f) -> Stream[Any, Any, Any]Write what f(state, pull_left, pull_right) makes, threading
its state; f pulls single items from either side as it likes and
ends the stream by failing with Done (as a pull of an ended side
does).
| Parameter | Type | Description |
|---|---|---|
that | Stream[Any, Any, Any] | |
initial | Callable[[], Any] | |
f | Callable[[Any, Any, Any], Effect[tuple[Any, Any], Any, Any]] |
Returns Stream[Any, Any, Any]
def combine(
self,
that: Stream[Any, Any, Any],
initial: Callable[[], Any],
f: Callable[[Any, Any, Any], Effect[tuple[Any, Any], Any, Any]],
) -> Stream[Any, Any, Any]:
"""Write what ``f(state, pull_left, pull_right)`` makes, threading
its state; ``f`` pulls single items from either side as it likes and
ends the stream by failing with ``Done`` (as a pull of an ended side
does)."""
from pyfect.stream._mapping import one
from pyfect.stream._stream import Stream
require_stream(that, "that")
items = self._channel.flatten_array()
combined = items.combine(that._channel.flatten_array(), initial, f)
return Stream(combined.map(one))combine_array#
combine_array(that, initial, f) -> Stream[Any, Any, Any]combine a chunk at a time: the pulls give chunks, and f
makes a non-empty tuple of items to write.
| Parameter | Type | Description |
|---|---|---|
that | Stream[Any, Any, Any] | |
initial | Callable[[], Any] | |
f | Callable[[Any, Any, Any], Effect[tuple[Any, Any], Any, Any]] |
Returns Stream[Any, Any, Any]
def combine_array(
self,
that: Stream[Any, Any, Any],
initial: Callable[[], Any],
f: Callable[[Any, Any, Any], Effect[tuple[Any, Any], Any, Any]],
) -> Stream[Any, Any, Any]:
"""``combine`` a chunk at a time: the pulls give chunks, and ``f``
makes a non-empty tuple of items to write."""
from pyfect.stream._stream import Stream
require_stream(that, "that")
return Stream(self._channel.combine(that._channel, initial, f))interleave#
interleave(that) -> Stream[A_co | B, E_co | E2, R_co | R2]Alternate items from this stream and that, starting here; when
one ends, write the rest of the other.
| Parameter | Type | Description |
|---|---|---|
that | Stream[B, E2, R2] |
Returns Stream[A_co | B, E_co | E2, R_co | R2]
def interleave[B, E2 = Never, R2 = Never](
self, that: Stream[B, E2, R2]
) -> Stream[A_co | B, E_co | E2, R_co | R2]:
"""Alternate items from this stream and ``that``, starting here; when
one ends, write the rest of the other."""
from pyfect.stream import _zipping
require_stream(that, "that")
return _zipping.interleave_with(self, that, _zipping.alternating())interleave_with#
interleave_with(that, decider) -> Stream[A_co | B, E_co | EE, R_co | RR]Take the next item from this stream when decider writes
True and from that when it writes False, skipping a side
that has ended; end when both have ended or decider does.
| Parameter | Type | Description |
|---|---|---|
that | Stream[B, EE, RR] | |
decider | Stream[bool, EE, RR] |
Returns Stream[A_co | B, E_co | EE, R_co | RR]
def interleave_with[B, EE = Never, RR = Never](
self, that: Stream[B, EE, RR], decider: Stream[bool, EE, RR]
) -> Stream[A_co | B, E_co | EE, R_co | RR]:
"""Take the next item from this stream when ``decider`` writes
``True`` and from ``that`` when it writes ``False``, skipping a side
that has ended; end when both have ended or ``decider`` does."""
from pyfect.stream import _zipping
require_stream(that, "that")
require_stream(decider, "decider")
return _zipping.interleave_with(self, that, decider)race#
race(that) -> Stream[A_co | B, E_co | E2, R_co | R2]The stream that writes first; the other is interrupted.
| Parameter | Type | Description |
|---|---|---|
that | Stream[B, E2, R2] |
Returns Stream[A_co | B, E_co | E2, R_co | R2]
def race[B, E2 = Never, R2 = Never](
self, that: Stream[B, E2, R2]
) -> Stream[A_co | B, E_co | E2, R_co | R2]:
"""The stream that writes first; the other is interrupted."""
from pyfect.stream import _zipping
require_stream(that, "that")
return _zipping.race_all(self, that)chunks#
chunks() -> Stream[tuple[A_co, ...], E_co, R_co]Write each chunk as one item.
Returns Stream[tuple[A_co, ...], E_co, R_co]
def chunks(self) -> Stream[tuple[A_co, ...], E_co, R_co]:
"""Write each chunk as one item."""
from pyfect.stream._stream import Stream
return Stream(self._channel.map(lambda chunk: (chunk,)))collect#
collect() -> Stream[list[A_co], E_co, R_co]Write every item as one list, once the stream ends.
Returns Stream[list[A_co], E_co, R_co]
def collect(self) -> Stream[list[A_co], E_co, R_co]:
"""Write every item as one list, once the stream ends."""
from pyfect.stream._base import as_stream
from pyfect.stream._constructors import from_effect
return from_effect(as_stream(self).run_collect())accumulate#
accumulate() -> Stream[tuple[A_co, ...], E_co, R_co]For each chunk, write a tuple of every item so far.
Returns Stream[tuple[A_co, ...], E_co, R_co]
def accumulate(self) -> Stream[tuple[A_co, ...], E_co, R_co]:
"""For each chunk, write a tuple of every item so far."""
from pyfect.stream import _chunking
return _chunking.accumulate(self)rechunk#
rechunk(size) -> Stream[A_co, E_co, R_co]Write the items in chunks of size (the last may be smaller).
| Parameter | Type | Description |
|---|---|---|
size | int |
Returns Stream[A_co, E_co, R_co]
def rechunk(self, size: int) -> Stream[A_co, E_co, R_co]:
"""Write the items in chunks of ``size`` (the last may be smaller)."""
from pyfect.stream import _chunking
return _chunking.rechunk(self, size)grouped#
grouped(size) -> Stream[tuple[A_co, ...], E_co, R_co]Write the items in tuples of size (the last may be smaller).
| Parameter | Type | Description |
|---|---|---|
size | int |
Returns Stream[tuple[A_co, ...], E_co, R_co]
def grouped(self, size: int) -> Stream[tuple[A_co, ...], E_co, R_co]:
"""Write the items in tuples of ``size`` (the last may be smaller)."""
return self.rechunk(size).chunks()sliding#
sliding(size) -> Stream[tuple[A_co, ...], E_co, R_co]Windows of size items, each one item on from the last.
| Parameter | Type | Description |
|---|---|---|
size | int |
Returns Stream[tuple[A_co, ...], E_co, R_co]
def sliding(self, size: int) -> Stream[tuple[A_co, ...], E_co, R_co]:
"""Windows of ``size`` items, each one item on from the last."""
from pyfect.stream import _chunking
return _chunking.sliding_size(self, size, 1)sliding_size#
sliding_size(size, step) -> Stream[tuple[A_co, ...], E_co, R_co]Windows of size items, each step items on from the last.
When the stream ends or fails, the items not yet in a full window
are written as one shorter window first.
| Parameter | Type | Description |
|---|---|---|
size | int | |
step | int |
Returns Stream[tuple[A_co, ...], E_co, R_co]
def sliding_size(self, size: int, step: int) -> Stream[tuple[A_co, ...], E_co, R_co]:
"""Windows of ``size`` items, each ``step`` items on from the last.
When the stream ends or fails, the items not yet in a full window
are written as one shorter window first."""
from pyfect.stream import _chunking
return _chunking.sliding_size(self, size, step)split#
split(predicate) -> Stream[tuple[A_co, ...], E_co, R_co]Write the runs of items between the items that pass predicate
(which are dropped); empty runs are skipped.
| Parameter | Type | Description |
|---|---|---|
predicate | Callable[[A_co], bool] |
Returns Stream[tuple[A_co, ...], E_co, R_co]
def split(self, predicate: Callable[[A_co], bool]) -> Stream[tuple[A_co, ...], E_co, R_co]:
"""Write the runs of items between the items that pass ``predicate``
(which are dropped); empty runs are skipped."""
from pyfect.stream import _chunking
return _chunking.split(self, predicate)scan#
scan(initial, f) -> Stream[S, E_co, R_co]Write initial(), then each state f folds the items into.
| Parameter | Type | Description |
|---|---|---|
initial | Callable[[], S] | |
f | Callable[[S, A_co], S] |
Returns Stream[S, E_co, R_co]
def scan[S](self, initial: Callable[[], S], f: Callable[[S, A_co], S]) -> Stream[S, E_co, R_co]:
"""Write ``initial()``, then each state ``f`` folds the items into."""
from pyfect.stream import _chunking
return _chunking.scan(self, initial, f)scan_effect#
scan_effect(initial, f) -> Stream[S, E_co | E2, R_co | R2]scan with an effectful f.
| Parameter | Type | Description |
|---|---|---|
initial | Callable[[], S] | |
f | Callable[[S, A_co], Effect[S, E2, R2]] |
Returns Stream[S, E_co | E2, R_co | R2]
def scan_effect[S, E2 = Never, R2 = Never](
self, initial: Callable[[], S], f: Callable[[S, A_co], Effect[S, E2, R2]]
) -> Stream[S, E_co | E2, R_co | R2]:
"""``scan`` with an effectful ``f``."""
from pyfect.stream import _chunking
return _chunking.scan_effect(self, initial, f)map_accum#
map_accum(initial, f, *, on_halt=None) -> Stream[B, E_co, R_co]Thread a state through the items: f(state, item) gives the
next state and the items to write. When the stream ends or fails,
on_halt(state) gives the last items to write.
| Parameter | Type | Description |
|---|---|---|
initial | Callable[[], S] | |
f | Callable[[S, A_co], tuple[S, Sequence[B]]] | |
on_halt | Callable[[S], Sequence[B]] | None = None |
Returns Stream[B, E_co, R_co]
def map_accum[S, B](
self,
initial: Callable[[], S],
f: Callable[[S, A_co], tuple[S, Sequence[B]]],
*,
on_halt: Callable[[S], Sequence[B]] | None = None,
) -> Stream[B, E_co, R_co]:
"""Thread a state through the items: ``f(state, item)`` gives the
next state and the items to write. When the stream ends or fails,
``on_halt(state)`` gives the last items to write."""
from pyfect.stream import _chunking
return _chunking.map_accum(self, initial, f, on_halt, array=False)map_accum_array#
map_accum_array(initial, f, *, on_halt=None) -> Stream[B, E_co, R_co]map_accum over each chunk.
| Parameter | Type | Description |
|---|---|---|
initial | Callable[[], S] | |
f | Callable[[S, tuple[A_co, ...]], tuple[S, Sequence[B]]] | |
on_halt | Callable[[S], Sequence[B]] | None = None |
Returns Stream[B, E_co, R_co]
def map_accum_array[S, B](
self,
initial: Callable[[], S],
f: Callable[[S, tuple[A_co, ...]], tuple[S, Sequence[B]]],
*,
on_halt: Callable[[S], Sequence[B]] | None = None,
) -> Stream[B, E_co, R_co]:
"""``map_accum`` over each chunk."""
from pyfect.stream import _chunking
return _chunking.map_accum(self, initial, f, on_halt, array=True)map_accum_effect#
map_accum_effect(initial, f, *, on_halt=None) -> Stream[B, E_co | E2, R_co | R2]map_accum with an effectful f, one item at a time.
| Parameter | Type | Description |
|---|---|---|
initial | Callable[[], S] | |
f | Callable[[S, A_co], Effect[tuple[S, Sequence[B]], E2, R2]] | |
on_halt | Callable[[S], Sequence[B]] | None = None |
Returns Stream[B, E_co | E2, R_co | R2]
def map_accum_effect[S, B, E2 = Never, R2 = Never](
self,
initial: Callable[[], S],
f: Callable[[S, A_co], Effect[tuple[S, Sequence[B]], E2, R2]],
*,
on_halt: Callable[[S], Sequence[B]] | None = None,
) -> Stream[B, E_co | E2, R_co | R2]:
"""``map_accum`` with an effectful ``f``, one item at a time."""
from pyfect.stream import _chunking
return _chunking.map_accum_effect(self, initial, f, on_halt, array=False)map_accum_array_effect#
map_accum_array_effect(initial, f, *, on_halt=None) -> Stream[B, E_co | E2, R_co | R2]map_accum_array with an effectful f.
| Parameter | Type | Description |
|---|---|---|
initial | Callable[[], S] | |
f | Callable[[S, tuple[A_co, ...]], Effect[tuple[S, Sequence[B]], E2, R2]] | |
on_halt | Callable[[S], Sequence[B]] | None = None |
Returns Stream[B, E_co | E2, R_co | R2]
def map_accum_array_effect[S, B, E2 = Never, R2 = Never](
self,
initial: Callable[[], S],
f: Callable[[S, tuple[A_co, ...]], Effect[tuple[S, Sequence[B]], E2, R2]],
*,
on_halt: Callable[[S], Sequence[B]] | None = None,
) -> Stream[B, E_co | E2, R_co | R2]:
"""``map_accum_array`` with an effectful ``f``."""
from pyfect.stream import _chunking
return _chunking.map_accum_effect(self, initial, f, on_halt, array=True)zip_with_index#
zip_with_index() -> Stream[tuple[A_co, int], E_co, R_co]Pair each item with its index, from 0.
Returns Stream[tuple[A_co, int], E_co, R_co]
def zip_with_index(self) -> Stream[tuple[A_co, int], E_co, R_co]:
"""Pair each item with its index, from 0."""
from pyfect.stream import _chunking
return _chunking.zip_with_index(self)zip_with_next#
zip_with_next() -> Stream[tuple[A_co, Option[A_co]], E_co, R_co]Pair each item with the next one (None for the last).
Returns Stream[tuple[A_co, Option[A_co]], E_co, R_co]
def zip_with_next(self) -> Stream[tuple[A_co, Option[A_co]], E_co, R_co]:
"""Pair each item with the next one (``None`` for the last)."""
from pyfect.stream import _chunking
return _chunking.zip_with_next(self)zip_with_previous#
zip_with_previous() -> Stream[tuple[Option[A_co], A_co], E_co, R_co]Pair each item with the one before it (None for the first).
Returns Stream[tuple[Option[A_co], A_co], E_co, R_co]
def zip_with_previous(self) -> Stream[tuple[Option[A_co], A_co], E_co, R_co]:
"""Pair each item with the one before it (``None`` for the first)."""
from pyfect.stream import _chunking
return _chunking.zip_with_previous(self)zip_with_previous_and_next#
zip_with_previous_and_next() -> Stream[tuple[Option[A_co], A_co, Option[A_co]], E_co, R_co]Each item between the one before it and the next one.
Returns Stream[tuple[Option[A_co], A_co, Option[A_co]], E_co, R_co]
def zip_with_previous_and_next(
self,
) -> Stream[tuple[Option[A_co], A_co, Option[A_co]], E_co, R_co]:
"""Each item between the one before it and the next one."""
from pyfect.stream import _chunking
return _chunking.zip_with_previous_and_next(self)filter#
filter(predicate) -> Stream[object, E_co, R_co]Keep the items that pass predicate.
| Parameter | Type | Description |
|---|---|---|
predicate | Callable[[A_co], bool] |
Returns Stream[object, E_co, R_co]
def filter(self, predicate: Callable[[A_co], bool]) -> Stream[object, E_co, R_co]:
"""Keep the items that pass ``predicate``."""
from pyfect.stream._stream import Stream
return Stream(self._channel.filter_array(predicate))filter_map#
filter_map(f) -> Stream[B, E_co, R_co]Write value for each item f maps to Ok(value); drop
the items it maps to Err.
| Parameter | Type | Description |
|---|---|---|
f | Callable[[A_co], Result[B, object]] |
Returns Stream[B, E_co, R_co]
def filter_map[B](self, f: Callable[[A_co], Result[B, object]]) -> Stream[B, E_co, R_co]:
"""Write ``value`` for each item ``f`` maps to ``Ok(value)``; drop
the items it maps to ``Err``."""
from pyfect.stream import _taking
return _taking.filter_map(self, f)filter_effect#
filter_effect(predicate) -> Stream[A_co, E_co | E2, R_co | R2]Keep the items whose predicate succeeds with True.
| Parameter | Type | Description |
|---|---|---|
predicate | Callable[[A_co], Effect[bool, E2, R2]] |
Returns Stream[A_co, E_co | E2, R_co | R2]
def filter_effect[E2 = Never, R2 = Never](
self, predicate: Callable[[A_co], Effect[bool, E2, R2]]
) -> Stream[A_co, E_co | E2, R_co | R2]:
"""Keep the items whose ``predicate`` succeeds with ``True``."""
from pyfect.stream._stream import Stream
return Stream(self._channel.filter_array_effect(predicate))filter_map_effect#
filter_map_effect(f) -> Stream[B, E_co | E2, R_co | R2]filter_map with an effectful f.
| Parameter | Type | Description |
|---|---|---|
f | Callable[[A_co], Effect[Result[B, object], E2, R2]] |
Returns Stream[B, E_co | E2, R_co | R2]
def filter_map_effect[B, E2 = Never, R2 = Never](
self, f: Callable[[A_co], Effect[Result[B, object], E2, R2]]
) -> Stream[B, E_co | E2, R_co | R2]:
"""``filter_map`` with an effectful ``f``."""
from pyfect.stream import _taking
return _taking.filter_map_effect(self, f)take#
take(n) -> Stream[A_co, E_co, R_co]The first n items; then end without pulling again.
| Parameter | Type | Description |
|---|---|---|
n | int |
Returns Stream[A_co, E_co, R_co]
def take(self, n: int) -> Stream[A_co, E_co, R_co]:
"""The first ``n`` items; then end without pulling again."""
from pyfect.stream import _taking
return _taking.take(self, n)take_right#
take_right(n) -> Stream[A_co, E_co, R_co]The last n items, written when the stream ends.
| Parameter | Type | Description |
|---|---|---|
n | int |
Returns Stream[A_co, E_co, R_co]
def take_right(self, n: int) -> Stream[A_co, E_co, R_co]:
"""The last ``n`` items, written when the stream ends."""
from pyfect.stream import _taking
return _taking.take_right(self, n)take_until#
take_until(predicate, *, exclude_last=False) -> Stream[A_co, E_co, R_co]Items up to and including the first that passes predicate
(excluding it with exclude_last); then end.
| Parameter | Type | Description |
|---|---|---|
predicate | Callable[[A_co], bool] | |
exclude_last | bool = False |
Returns Stream[A_co, E_co, R_co]
def take_until(
self, predicate: Callable[[A_co], bool], *, exclude_last: bool = False
) -> Stream[A_co, E_co, R_co]:
"""Items up to and including the first that passes ``predicate``
(excluding it with ``exclude_last``); then end."""
from pyfect.stream import _taking
return _taking.take_until(self, predicate, exclude_last)take_until_effect#
take_until_effect(predicate, *, exclude_last=False) -> Stream[A_co, E_co | E2, R_co | R2]take_until with an effectful predicate.
| Parameter | Type | Description |
|---|---|---|
predicate | Callable[[A_co], Effect[bool, E2, R2]] | |
exclude_last | bool = False |
Returns Stream[A_co, E_co | E2, R_co | R2]
def take_until_effect[E2 = Never, R2 = Never](
self, predicate: Callable[[A_co], Effect[bool, E2, R2]], *, exclude_last: bool = False
) -> Stream[A_co, E_co | E2, R_co | R2]:
"""``take_until`` with an effectful ``predicate``."""
from pyfect.stream import _taking
return _taking.take_until_effect(self, predicate, exclude_last)take_while#
take_while(predicate) -> Stream[object, E_co, R_co]Items while they pass predicate; end at the first that fails.
| Parameter | Type | Description |
|---|---|---|
predicate | Callable[[A_co], bool] |
Returns Stream[object, E_co, R_co]
def take_while(self, predicate: Callable[[A_co], bool]) -> Stream[object, E_co, R_co]:
"""Items while they pass ``predicate``; end at the first that fails."""
from pyfect.stream import _taking
return _taking.take_while(self, predicate)take_while_filter#
take_while_filter(f) -> Stream[B, E_co, R_co]Write value while f maps items to Ok(value); end at the
first Err.
| Parameter | Type | Description |
|---|---|---|
f | Callable[[A_co], Result[B, object]] |
Returns Stream[B, E_co, R_co]
def take_while_filter[B](self, f: Callable[[A_co], Result[B, object]]) -> Stream[B, E_co, R_co]:
"""Write ``value`` while ``f`` maps items to ``Ok(value)``; end at the
first ``Err``."""
from pyfect.stream import _taking
return _taking.take_while_filter(self, f)take_while_effect#
take_while_effect(predicate) -> Stream[A_co, E_co | E2, R_co | R2]take_while with an effectful predicate.
| Parameter | Type | Description |
|---|---|---|
predicate | Callable[[A_co], Effect[bool, E2, R2]] |
Returns Stream[A_co, E_co | E2, R_co | R2]
def take_while_effect[E2 = Never, R2 = Never](
self, predicate: Callable[[A_co], Effect[bool, E2, R2]]
) -> Stream[A_co, E_co | E2, R_co | R2]:
"""``take_while`` with an effectful ``predicate``."""
from pyfect.stream import _taking
return _taking.take_until_effect(self, lambda a: predicate(a).map(_not), True)limit_bytes#
limit_bytes(limit, on_limit_reached) -> Stream[bytes, E, R]Byte strings while their total length stays within limit;
the first one that would cross it is dropped and the stream goes on
with on_limit_reached() instead.
| Parameter | Type | Description |
|---|---|---|
limit | int | |
on_limit_reached | Callable[[], Stream[bytes, E, R]] |
Returns Stream[bytes, E, R]
def limit_bytes[E, R](
self: FilterMethods[bytes, E, R],
limit: int,
on_limit_reached: Callable[[], Stream[bytes, E, R]],
) -> Stream[bytes, E, R]:
"""Byte strings while their total length stays within ``limit``;
the first one that would cross it is dropped and the stream goes on
with ``on_limit_reached()`` instead."""
from pyfect.stream import _taking
return _taking.limit_bytes(self, limit, on_limit_reached)drop#
drop(n) -> Stream[A_co, E_co, R_co]All but the first n items.
| Parameter | Type | Description |
|---|---|---|
n | int |
Returns Stream[A_co, E_co, R_co]
def drop(self, n: int) -> Stream[A_co, E_co, R_co]:
"""All but the first ``n`` items."""
from pyfect.stream import _taking
return _taking.drop(self, n)drop_right#
drop_right(n) -> Stream[A_co, E_co, R_co]All but the last n items (each written once n later ones
have come).
| Parameter | Type | Description |
|---|---|---|
n | int |
Returns Stream[A_co, E_co, R_co]
def drop_right(self, n: int) -> Stream[A_co, E_co, R_co]:
"""All but the last ``n`` items (each written once ``n`` later ones
have come)."""
from pyfect.stream import _taking
return _taking.drop_right(self, n)drop_while#
drop_while(predicate) -> Stream[A_co, E_co, R_co]Drop items while they pass predicate; write the rest.
| Parameter | Type | Description |
|---|---|---|
predicate | Callable[[A_co], bool] |
Returns Stream[A_co, E_co, R_co]
def drop_while(self, predicate: Callable[[A_co], bool]) -> Stream[A_co, E_co, R_co]:
"""Drop items while they pass ``predicate``; write the rest."""
from pyfect.stream import _taking
return _taking.drop_while(self, predicate)drop_while_filter#
drop_while_filter(f) -> Stream[A_co, E_co, R_co]Drop items while f maps them to Ok; write the rest.
| Parameter | Type | Description |
|---|---|---|
f | Callable[[A_co], Result[object, object]] |
Returns Stream[A_co, E_co, R_co]
def drop_while_filter(
self, f: Callable[[A_co], Result[object, object]]
) -> Stream[A_co, E_co, R_co]:
"""Drop items while ``f`` maps them to ``Ok``; write the rest."""
from pyfect.stream import _taking
return _taking.drop_while(self, lambda a: isinstance(f(a), Ok))drop_while_effect#
drop_while_effect(predicate) -> Stream[A_co, E_co | E2, R_co | R2]drop_while with an effectful predicate.
| Parameter | Type | Description |
|---|---|---|
predicate | Callable[[A_co], Effect[bool, E2, R2]] |
Returns Stream[A_co, E_co | E2, R_co | R2]
def drop_while_effect[E2 = Never, R2 = Never](
self, predicate: Callable[[A_co], Effect[bool, E2, R2]]
) -> Stream[A_co, E_co | E2, R_co | R2]:
"""``drop_while`` with an effectful ``predicate``."""
from pyfect.stream import _taking
return _taking.drop_while_effect(self, predicate)drop_until#
drop_until(predicate) -> Stream[A_co, E_co, R_co]Drop items up to and including the first that passes
predicate.
| Parameter | Type | Description |
|---|---|---|
predicate | Callable[[A_co], bool] |
Returns Stream[A_co, E_co, R_co]
def drop_until(self, predicate: Callable[[A_co], bool]) -> Stream[A_co, E_co, R_co]:
"""Drop items up to and including the first that passes
``predicate``."""
return self.drop_while(lambda a: not predicate(a)).drop(1)drop_until_effect#
drop_until_effect(predicate) -> Stream[A_co, E_co | E2, R_co | R2]drop_until with an effectful predicate.
| Parameter | Type | Description |
|---|---|---|
predicate | Callable[[A_co], Effect[bool, E2, R2]] |
Returns Stream[A_co, E_co | E2, R_co | R2]
def drop_until_effect[E2 = Never, R2 = Never](
self, predicate: Callable[[A_co], Effect[bool, E2, R2]]
) -> Stream[A_co, E_co | E2, R_co | R2]:
"""``drop_until`` with an effectful ``predicate``."""
return self.drop_while_effect(lambda a: predicate(a).map(_not)).drop(1)changes#
changes() -> Stream[A_co, E_co, R_co]Drop each item equal (==) to the one written before it.
Returns Stream[A_co, E_co, R_co]
def changes(self) -> Stream[A_co, E_co, R_co]:
"""Drop each item equal (``==``) to the one written before it."""
from pyfect.stream import _taking
return _taking.changes_with(self, _equal)changes_with#
changes_with(f) -> Stream[A_co, E_co, R_co]Drop each item f(item, last_written) calls the same.
| Parameter | Type | Description |
|---|---|---|
f | Callable[[A_co, A_co], bool] |
Returns Stream[A_co, E_co, R_co]
def changes_with(self, f: Callable[[A_co, A_co], bool]) -> Stream[A_co, E_co, R_co]:
"""Drop each item ``f(item, last_written)`` calls the same."""
from pyfect.stream import _taking
return _taking.changes_with(self, f)changes_with_effect#
changes_with_effect(f) -> Stream[A_co, E_co | E2, R_co | R2]changes_with with an effectful f.
| Parameter | Type | Description |
|---|---|---|
f | Callable[[A_co, A_co], Effect[bool, E2, R2]] |
Returns Stream[A_co, E_co | E2, R_co | R2]
def changes_with_effect[E2 = Never, R2 = Never](
self, f: Callable[[A_co, A_co], Effect[bool, E2, R2]]
) -> Stream[A_co, E_co | E2, R_co | R2]:
"""``changes_with`` with an effectful ``f``."""
from pyfect.stream import _taking
return _taking.changes_with_effect(self, f)map#
map(f) -> Stream[B, E_co, R_co]Apply f to each item.
| Parameter | Type | Description |
|---|---|---|
f | Callable[[A_co], B] |
Returns Stream[B, E_co, R_co]
def map[B](self, f: Callable[[A_co], B]) -> Stream[B, E_co, R_co]:
"""Apply ``f`` to each item."""
from pyfect.stream import _mapping
return _mapping.map_(self, f)as_#
as_(value) -> Stream[B, E_co, R_co]Replace each item with value.
| Parameter | Type | Description |
|---|---|---|
value | B |
Returns Stream[B, E_co, R_co]
def as_[B](self, value: B) -> Stream[B, E_co, R_co]:
"""Replace each item with ``value``."""
from pyfect.stream import _mapping
return _mapping.map_array(self, lambda chunk: (value,) * len(chunk))map_both#
map_both(*, on_element, on_error) -> Stream[B, E2, R_co]Apply on_element to each item and on_error to the error.
| Parameter | Type | Description |
|---|---|---|
on_element | Callable[[A_co], B] | |
on_error | Callable[[E_co], E2] |
Returns Stream[B, E2, R_co]
def map_both[B, E2](
self, *, on_element: Callable[[A_co], B], on_error: Callable[[E_co], E2]
) -> Stream[B, E2, R_co]:
"""Apply ``on_element`` to each item and ``on_error`` to the error."""
return self.map(on_element).map_error(on_error)map_array#
map_array(f) -> Stream[B, E_co, R_co]Apply f to each chunk (a non-empty tuple); an empty result
writes nothing.
| Parameter | Type | Description |
|---|---|---|
f | Callable[[tuple[A_co, ...]], tuple[B, ...]] |
Returns Stream[B, E_co, R_co]
def map_array[B](self, f: Callable[[tuple[A_co, ...]], tuple[B, ...]]) -> Stream[B, E_co, R_co]:
"""Apply ``f`` to each chunk (a non-empty tuple); an empty result
writes nothing."""
from pyfect.stream import _mapping
return _mapping.map_array(self, f)map_effect#
map_effect(f, *, concurrency=None, unordered=False) -> Stream[B, E_co | E2, R_co | R2]Write what f(item) succeeds with, one item per chunk. With
concurrency above 1, up to that many run at once; their results
keep upstream's order unless unordered.
| Parameter | Type | Description |
|---|---|---|
f | Callable[[A_co], Effect[B, E2, R2]] | |
concurrency | Concurrency = None | |
unordered | bool = False |
Returns Stream[B, E_co | E2, R_co | R2]
def map_effect[B, E2 = Never, R2 = Never](
self,
f: Callable[[A_co], Effect[B, E2, R2]],
*,
concurrency: Concurrency = None,
unordered: bool = False,
) -> Stream[B, E_co | E2, R_co | R2]:
"""Write what ``f(item)`` succeeds with, one item per chunk. With
``concurrency`` above 1, up to that many run at once; their results
keep upstream's order unless ``unordered``."""
from pyfect.stream import _mapping
return _mapping.map_effect(self, f, concurrency, unordered)map_array_effect#
map_array_effect(f) -> Stream[B, E_co | E2, R_co | R2]Write the chunk f(chunk) succeeds with, for each chunk.
| Parameter | Type | Description |
|---|---|---|
f | Callable[[tuple[A_co, ...]], Effect[tuple[B, ...], E2, R2]] |
Returns Stream[B, E_co | E2, R_co | R2]
def map_array_effect[B, E2 = Never, R2 = Never](
self, f: Callable[[tuple[A_co, ...]], Effect[tuple[B, ...], E2, R2]]
) -> Stream[B, E_co | E2, R_co | R2]:
"""Write the chunk ``f(chunk)`` succeeds with, for each chunk."""
from pyfect.stream import _mapping
return _mapping.map_array_effect(self, f)flatten_effect#
flatten_effect(*, concurrency=None, unordered=False) -> Stream[B, E_co | E2, R_co | R2]Run each effect the stream writes and write its value.
| Parameter | Type | Description |
|---|---|---|
concurrency | Concurrency = None | |
unordered | bool = False |
Returns Stream[B, E_co | E2, R_co | R2]
def flatten_effect[B, E2 = Never, R2 = Never](
self: MapMethods[Effect[B, E2, R2], E_co, R_co],
*,
concurrency: Concurrency = None,
unordered: bool = False,
) -> Stream[B, E_co | E2, R_co | R2]:
"""Run each effect the stream writes and write its value."""
from pyfect.stream import _mapping
return _mapping.map_effect(self, _identity, concurrency, unordered)result#
result() -> Stream[Result[A_co, E_co], Never, R_co]Write each item as Ok, then a typed failure as one last
Err instead of failing.
Returns Stream[Result[A_co, E_co], Never, R_co]
def result(self) -> Stream[Result[A_co, E_co], Never, R_co]:
"""Write each item as ``Ok``, then a typed failure as one last
``Err`` instead of failing."""
from pyfect.stream import _mapping
return _mapping.result(self)tap#
tap(f, *, concurrency=None) -> Stream[A_co, E_co | E2, R_co | R2]Run f on each item, then write the item.
| Parameter | Type | Description |
|---|---|---|
f | Callable[[A_co], Effect[object, E2, R2]] | |
concurrency | Concurrency = None |
Returns Stream[A_co, E_co | E2, R_co | R2]
def tap[E2 = Never, R2 = Never](
self, f: Callable[[A_co], Effect[object, E2, R2]], *, concurrency: Concurrency = None
) -> Stream[A_co, E_co | E2, R_co | R2]:
"""Run ``f`` on each item, then write the item."""
from pyfect.stream import _mapping
return _mapping.map_effect(self, lambda a: f(a).as_(a), concurrency, False)tap_both#
tap_both(*, on_element, on_error, concurrency=None) -> Stream[A_co, E_co | EE, R_co | RR]tap with on_element and tap_error with on_error.
| Parameter | Type | Description |
|---|---|---|
on_element | Callable[[A_co], Effect[object, EE, RR]] | |
on_error | Callable[[E_co], Effect[object, EE, RR]] | |
concurrency | Concurrency = None |
Returns Stream[A_co, E_co | EE, R_co | RR]
def tap_both[EE = Never, RR = Never](
self,
*,
on_element: Callable[[A_co], Effect[object, EE, RR]],
on_error: Callable[[E_co], Effect[object, EE, RR]],
concurrency: Concurrency = None,
) -> Stream[A_co, E_co | EE, R_co | RR]:
"""``tap`` with ``on_element`` and ``tap_error`` with ``on_error``."""
return as_stream(self).tap_error(on_error).tap(on_element, concurrency=concurrency)flat_map#
flat_map(f, *, concurrency=None, buffer_size=16) -> Stream[B, E_co | E2, R_co | R2]Write the items of f(item) for each item, one stream after
another; with concurrency above 1, up to that many at once,
their items merged through a buffer of buffer_size.
| Parameter | Type | Description |
|---|---|---|
f | Callable[[A_co], StreamBase[B, E2, R2]] | |
concurrency | Concurrency = None | |
buffer_size | int = 16 |
Returns Stream[B, E_co | E2, R_co | R2]
def flat_map[B, E2 = Never, R2 = Never](
self,
f: Callable[[A_co], StreamBase[B, E2, R2]],
*,
concurrency: Concurrency = None,
buffer_size: int = 16,
) -> Stream[B, E_co | E2, R_co | R2]:
"""Write the items of ``f(item)`` for each item, one stream after
another; with ``concurrency`` above 1, up to that many at once,
their items merged through a buffer of ``buffer_size``."""
from pyfect.stream import _mapping
return _mapping.flat_map(self, f, concurrency, buffer_size, switch=False)switch_map#
switch_map(f, *, concurrency=1, buffer_size=16) -> Stream[B, E_co | E2, R_co | R2]flat_map where each new item interrupts the oldest running
stream once concurrency are running.
| Parameter | Type | Description |
|---|---|---|
f | Callable[[A_co], StreamBase[B, E2, R2]] | |
concurrency | Concurrency = 1 | |
buffer_size | int = 16 |
Returns Stream[B, E_co | E2, R_co | R2]
def switch_map[B, E2 = Never, R2 = Never](
self,
f: Callable[[A_co], StreamBase[B, E2, R2]],
*,
concurrency: Concurrency = 1,
buffer_size: int = 16,
) -> Stream[B, E_co | E2, R_co | R2]:
"""``flat_map`` where each new item interrupts the oldest running
stream once ``concurrency`` are running."""
from pyfect.stream import _mapping
return _mapping.flat_map(self, f, concurrency, buffer_size, switch=True)flatten#
flatten(*, concurrency=None, buffer_size=16) -> Stream[B, E_co | E2, R_co | R2]Write the items of each stream this stream writes.
| Parameter | Type | Description |
|---|---|---|
concurrency | Concurrency = None | |
buffer_size | int = 16 |
Returns Stream[B, E_co | E2, R_co | R2]
def flatten[B, E2 = Never, R2 = Never](
self: MapMethods[StreamBase[B, E2, R2], E_co, R_co],
*,
concurrency: Concurrency = None,
buffer_size: int = 16,
) -> Stream[B, E_co | E2, R_co | R2]:
"""Write the items of each stream this stream writes."""
from pyfect.stream import _mapping
return _mapping.flat_map(self, _identity, concurrency, buffer_size, switch=False)flatten_array#
flatten_array() -> Stream[B, E, R]Write the items of each tuple this stream writes, a tuple per chunk.
Returns Stream[B, E, R]
def flatten_array[B, E, R](self: MapMethods[tuple[B, ...], E, R]) -> Stream[B, E, R]:
"""Write the items of each tuple this stream writes, a tuple per
chunk."""
from pyfect.stream import _mapping
return _mapping.flatten_array(self)flatten_iterable#
flatten_iterable() -> Stream[B, E, R]Write the items of each iterable this stream writes.
Returns Stream[B, E, R]
def flatten_iterable[B, E, R](self: MapMethods[Iterable[B], E, R]) -> Stream[B, E, R]:
"""Write the items of each iterable this stream writes."""
from pyfect.stream import _constructors
return self.flat_map(_constructors.from_iterable)flatten_take#
flatten_take() -> Stream[B, E_co | E2, R_co]Replay each Take: write its items, or fail or end as it says.
Returns Stream[B, E_co | E2, R_co]
def flatten_take[B, E2](
self: MapMethods[Take[B, E2], E_co, R_co],
) -> Stream[B, E_co | E2, R_co]:
"""Replay each ``Take``: write its items, or fail or end as it says."""
from pyfect.stream import _mapping
return _mapping.flatten_take(self)drain#
drain() -> Stream[Never, E_co, R_co]Run the stream for its effects, writing nothing.
Returns Stream[Never, E_co, R_co]
def drain(self) -> Stream[Never, E_co, R_co]:
"""Run the stream for its effects, writing nothing."""
from pyfect.stream._stream import Stream
return Stream(self._channel.drain())drain_fork#
drain_fork(that) -> Stream[A_co, E_co | E2, R_co | R2]Run that for its effects alongside this stream; its failure
fails this stream, and it stops when this stream ends.
| Parameter | Type | Description |
|---|---|---|
that | Stream[object, E2, R2] |
Returns Stream[A_co, E_co | E2, R_co | R2]
def drain_fork[E2 = Never, R2 = Never](
self, that: Stream[object, E2, R2]
) -> Stream[A_co, E_co | E2, R_co | R2]:
"""Run ``that`` for its effects alongside this stream; its failure
fails this stream, and it stops when this stream ends."""
require_stream(that, "that")
return as_stream(self).merge_effect(that.run_drain())concat#
concat(that) -> Stream[A_co | B, E_co | E2, R_co | R2]Write this stream's items, then that's. Each part's resources
are released before the next starts; a chain of thousands of
concat calls runs as one flat sequence.
| Parameter | Type | Description |
|---|---|---|
that | Stream[B, E2, R2] |
Returns Stream[A_co | B, E_co | E2, R_co | R2]
def concat[B, E2 = Never, R2 = Never](
self, that: Stream[B, E2, R2]
) -> Stream[A_co | B, E_co | E2, R_co | R2]:
"""Write this stream's items, then ``that``'s. Each part's resources
are released before the next starts; a chain of thousands of
``concat`` calls runs as one flat sequence."""
from pyfect.stream import _concat
require_stream(that, "that")
return _concat.concat(self, that)prepend#
prepend(values) -> Stream[A_co | B, E_co, R_co]Write values first, then this stream's items.
| Parameter | Type | Description |
|---|---|---|
values | Iterable[B] |
Returns Stream[A_co | B, E_co, R_co]
def prepend[B](self, values: Iterable[B]) -> Stream[A_co | B, E_co, R_co]:
"""Write ``values`` first, then this stream's items."""
from pyfect.stream import _concat, _constructors
return _concat.concat(_constructors.from_iterable(values), self)intersperse#
intersperse(element) -> Stream[A_co | B, E_co, R_co]Write element between each two items.
| Parameter | Type | Description |
|---|---|---|
element | B |
Returns Stream[A_co | B, E_co, R_co]
def intersperse[B](self, element: B) -> Stream[A_co | B, E_co, R_co]:
"""Write ``element`` between each two items."""
from pyfect.stream import _mapping
return _mapping.intersperse(self, element)intersperse_affixes#
intersperse_affixes(*, start, middle, end) -> Stream[A_co | B | C | D, E_co, R_co]Write start, the items with middle between each two, then
end.
| Parameter | Type | Description |
|---|---|---|
start | B | |
middle | C | |
end | D |
Returns Stream[A_co | B | C | D, E_co, R_co]
def intersperse_affixes[B, C, D](
self, *, start: B, middle: C, end: D
) -> Stream[A_co | B | C | D, E_co, R_co]:
"""Write ``start``, the items with ``middle`` between each two, then
``end``."""
from pyfect.stream import _concat, _constructors
body = _concat.concat(_constructors.succeed(start), self.intersperse(middle))
return _concat.concat(body, _constructors.succeed(end))when#
when(test) -> Stream[A_co, E_co | E2, R_co | R2]This stream if test succeeds with True, else an empty one;
test runs each time the stream runs.
| Parameter | Type | Description |
|---|---|---|
test | Effect[bool, E2, R2] |
Returns Stream[A_co, E_co | E2, R_co | R2]
def when[E2 = Never, R2 = Never](
self, test: Effect[bool, E2, R2]
) -> Stream[A_co, E_co | E2, R_co | R2]:
"""This stream if ``test`` succeeds with ``True``, else an empty one;
``test`` runs each time the stream runs."""
from pyfect._effect_args import require_effect
from pyfect.stream import _constructors
require_effect(test, "test")
me = as_stream(self)
return _constructors.unwrap(test.map(lambda ok: me if ok else _constructors.empty))On This Page
Functions__init__pipepipe_through_channelpipe_through_channel_or_faildecode_textencode_textsplit_linesrun_into_queueto_queuerun_into_pubsubto_pubsubto_pubsub_taketo_async_iterableto_async_iterable_withto_async_iterable_effectgroup_bygroup_by_keygroup_adjacent_bypartitionpartition_effectpartition_queuebroadcastbroadcast_nshareruntransduceaggregateaggregate_withingrouped_withinpipe_throughtap_sinkpeelrun_collectrun_drainrun_countrun_sumrun_foldrun_fold_effectrun_headrun_lastrun_for_eachrun_for_each_whilerun_for_each_arrayrun_stringrun_bytesto_pullupdate_servicewith_spanschedulerepeatrepeat_elementsforevertimeouttimeout_or_elsedebouncethrottlethrottle_effectbufferbuffer_arrayhalt_wheninterrupt_whenon_starton_firston_endon_exiton_errorensuringcatch_causecatch_cause_ifcatch_allcatch_ifcatch_filtercatch_cause_filtercatch_defector_else_if_emptyor_else_succeedtap_causetap_errortap_error_tagtap_defectmap_erroror_dieignoreignore_causeretryzipzip_withzip_with_arrayzip_leftzip_rightzip_flattenzip_latestzip_latest_withcrosscross_withmergemerge_effectmerge_resultmerge_leftmerge_rightcombinecombine_arrayinterleaveinterleave_withracechunkscollectaccumulaterechunkgroupedslidingsliding_sizesplitscanscan_effectmap_accummap_accum_arraymap_accum_effectmap_accum_array_effectzip_with_indexzip_with_nextzip_with_previouszip_with_previous_and_nextfilterfilter_mapfilter_effectfilter_map_effecttaketake_righttake_untiltake_until_effecttake_whiletake_while_filtertake_while_effectlimit_bytesdropdrop_rightdrop_whiledrop_while_filterdrop_while_effectdrop_untildrop_until_effectchangeschanges_withchanges_with_effectmapas_map_bothmap_arraymap_effectmap_array_effectflatten_effectresulttaptap_bothflat_mapswitch_mapflattenflatten_arrayflatten_iterableflatten_takedraindrain_forkconcatprependintersperseintersperse_affixeswhen