class TxChunk[A = Unsubscripted]
TxChunk(ref) is Effect's makeUnsafe; make, empty and
from_iterable are the effects.
Attributes#
| Attribute | Type | Description |
|---|---|---|
ref | TxRef[tuple[A, ...]] |
Functions#
__init__#
__init__(ref) -> None| Parameter | Type | Description |
|---|---|---|
ref | TxRef[tuple[A, ...]] |
def __init__(self, ref: TxRef[tuple[A, ...]]) -> None:
self._ref = refmake#
make[T](items: Iterable[T]) -> Effect[TxChunk[T]]
make(items: Iterable[A]) -> Effect[TxChunk[A]]A new TxChunk each time the effect runs. items is read
once, here, so a one-shot iterator survives transaction retries.
| Parameter | Type | Description |
|---|---|---|
items |
@overload
@classmethod
def make[T](cls: type[TxChunk[Unsubscripted]], items: Iterable[T]) -> Effect[TxChunk[T]]: ...
@overload
@classmethod
def make(cls, items: Iterable[A]) -> Effect[TxChunk[A]]: ...
@classmethod
def make[T](cls, items: Iterable[T]) -> Effect[TxChunk[T]]:
"""A new ``TxChunk`` each time the effect runs. ``items`` is read
once, here, so a one-shot iterator survives transaction retries."""
initial = tuple(items)
return TxRef.make(initial).map(TxChunk)empty#
empty() -> Effect[TxChunk[A]]Spelled TxChunk[int].empty().
Returns Effect[TxChunk[A]]
@classmethod
def empty(cls) -> Effect[TxChunk[A]]:
"""Spelled ``TxChunk[int].empty()``."""
empty: tuple[A, ...] = ()
return TxRef.make(empty).map(cls)from_iterable#
from_iterable[T](items: Iterable[T]) -> Effect[TxChunk[T]]
from_iterable(items: Iterable[A]) -> Effect[TxChunk[A]]| Parameter | Type | Description |
|---|---|---|
items |
@overload
@classmethod
def from_iterable[T](
cls: type[TxChunk[Unsubscripted]], items: Iterable[T]
) -> Effect[TxChunk[T]]: ...
@overload
@classmethod
def from_iterable(cls, items: Iterable[A]) -> Effect[TxChunk[A]]: ...
@classmethod
def from_iterable[T](cls, items: Iterable[T]) -> Effect[TxChunk[T]]:
return TxChunk.make(items)get#
get() -> Effect[tuple[A, ...]]Returns Effect[tuple[A, ...]]
def get(self) -> Effect[tuple[A, ...]]:
return self._ref.get()set#
set(items) -> Effect[None]| Parameter | Type | Description |
|---|---|---|
items | tuple[A, ...] |
Returns Effect[None]
def set(self, items: tuple[A, ...]) -> Effect[None]:
return self._ref.set(items)modify#
modify[B](f) -> Effect[B]Replace the items with the second item of f(items); succeed
with the first.
| Parameter | Type | Description |
|---|---|---|
f | Callable[[tuple[A, ...]], tuple[B, tuple[A, ...]]] |
Returns Effect[B]
def modify[B](self, f: Callable[[tuple[A, ...]], tuple[B, tuple[A, ...]]]) -> Effect[B]:
"""Replace the items with the second item of ``f(items)``; succeed
with the first."""
return self._ref.modify(f)update#
update(f) -> Effect[None]| Parameter | Type | Description |
|---|---|---|
f | Callable[[tuple[A, ...]], tuple[A, ...]] |
Returns Effect[None]
def update(self, f: Callable[[tuple[A, ...]], tuple[A, ...]]) -> Effect[None]:
return self._ref.update(f)append#
append(item) -> Effect[None]| Parameter | Type | Description |
|---|---|---|
item | A |
Returns Effect[None]
def append(self, item: A) -> Effect[None]:
return self.update(lambda items: (*items, item))prepend#
prepend(item) -> Effect[None]| Parameter | Type | Description |
|---|---|---|
item | A |
Returns Effect[None]
def prepend(self, item: A) -> Effect[None]:
return self.update(lambda items: (item, *items))append_all#
append_all(items) -> Effect[None]| Parameter | Type | Description |
|---|---|---|
items | Iterable[A] |
Returns Effect[None]
def append_all(self, items: Iterable[A]) -> Effect[None]:
added = tuple(items)
return self.update(lambda current: current + added)prepend_all#
prepend_all(items) -> Effect[None]| Parameter | Type | Description |
|---|---|---|
items | Iterable[A] |
Returns Effect[None]
def prepend_all(self, items: Iterable[A]) -> Effect[None]:
added = tuple(items)
return self.update(lambda current: added + current)concat#
concat(other) -> Effect[None]Append other's items, read in the same transaction.
| Parameter | Type | Description |
|---|---|---|
other | TxChunk[A] |
Returns Effect[None]
def concat(self, other: TxChunk[A]) -> Effect[None]:
"""Append ``other``'s items, read in the same transaction."""
return _tx_ops.update_reading(self._ref, other._ref, lambda items, added: items + added)size#
size() -> Effect[int]Returns Effect[int]
def size(self) -> Effect[int]:
return _tx_ops.read(self._ref, len)is_empty#
is_empty() -> Effect[bool]Returns Effect[bool]
def is_empty(self) -> Effect[bool]:
return _tx_ops.read(self._ref, lambda items: not items)is_non_empty#
is_non_empty() -> Effect[bool]Returns Effect[bool]
def is_non_empty(self) -> Effect[bool]:
return _tx_ops.read(self._ref, bool)take#
take(n) -> Effect[None]Keep the first n items; none when n is 0 or less.
| Parameter | Type | Description |
|---|---|---|
n | int |
Returns Effect[None]
def take(self, n: int) -> Effect[None]:
"""Keep the first ``n`` items; none when ``n`` is 0 or less."""
return self.update(lambda items: items[: max(n, 0)])drop#
drop(n) -> Effect[None]Remove the first n items; none when n is 0 or less.
| Parameter | Type | Description |
|---|---|---|
n | int |
Returns Effect[None]
def drop(self, n: int) -> Effect[None]:
"""Remove the first ``n`` items; none when ``n`` is 0 or less."""
return self.update(lambda items: items[max(n, 0) :])slice#
slice(start, end) -> Effect[None]Keep the items from start up to end: drop(start) then
take(end - start), so negative bounds do not count from the
end.
| Parameter | Type | Description |
|---|---|---|
start | int | |
end | int |
Returns Effect[None]
def slice(self, start: int, end: int) -> Effect[None]:
"""Keep the items from ``start`` up to ``end``: ``drop(start)`` then
``take(end - start)``, so negative bounds do not count from the
end."""
return self.update(lambda items: items[max(start, 0) :][: max(end - start, 0)])map#
map(f) -> Effect[None]| Parameter | Type | Description |
|---|---|---|
f | Callable[[A], A] |
Returns Effect[None]
def map(self, f: Callable[[A], A]) -> Effect[None]:
return self.update(lambda items: tuple(f(item) for item in items))filter#
filter(predicate) -> Effect[None]| Parameter | Type | Description |
|---|---|---|
predicate | Callable[[A], bool] |
Returns Effect[None]
def filter(self, predicate: Callable[[A], bool]) -> Effect[None]:
return self.update(lambda items: tuple(item for item in items if predicate(item)))