circularary.fixed¶
Circular array with fixed storage capacity.
O(1) pops and pushes either end
O(1) indexing, does not support slicing
fixed total storage capacity
iterable, can safely mutate while iterators continue iterating over previous state
comparisons compare identity before equality, like builtins
in boolean context, falsy when either empty or full, otherwise truthy
factory function caf produces a fixed storage capacity circular array from its arguments
- class pythonic_fp.circulararray.fixed.CAF(items: Iterable | None = None, capacity: int = 2)¶
Basically a list that can be grown from both ends in O(1) time and space complexity.
- Parameters:
items – optional iterable to initial populate circular array
capacity – fixed storage capacity of circular array
- Raises:
TypeError – if items is not Iterable
- capacity() int ¶
Return fixed storage capacity of the circular array.
- Returns:
fixed storage capacity
- empty() None ¶
Empty the circular array.
- foldl(f: Callable[[L, I], L], start: L | None = None) L | Never ¶
Fold left with a function and optional stating item.
- Parameters:
f – first argument to f is for the accumulator
start – optional starting item
- Returns:
reduced value produced by the left fold
- Raises:
ValueError – when circular array empty and no starting item given
- foldr(f: Callable[[I, R], R], start: R | None = None) R | Never ¶
Fold right with a function and an optional starting item.
- Parameters:
f – second argument to f is for the accumulator
start – optional starting item
- Returns:
reduced value produced by the right fold
- Raises:
ValueError – when circular array empty and no starting item given
- fraction_filled() float ¶
Find fraction of the storage capacity which is filled.
- Returns:
the ratio count/capacity
- map(f: Callable[[I], U]) CAF ¶
Apply function f over the circular array’s contents,
- Parameters:
f – callable from type I to type U
- Returns:
new fixed circular array instance
- popl() I | Never ¶
Pop item off left side (front) of circular array.
- Returns:
item popped from left side of circular array
- Raises:
ValueError – when called on an empty circular array
- popld(default: I) I ¶
Pop one item from left side of the circular array, provide a mandatory default value. “Safe” version of popl.
Pop one item from left side of the circular array, provide a mandatory default value. “Safe” version of popl.
- Parameters:
default – item returned if circular array is empty
- Returns:
item popped from left side or default item if empty
- poplt(maximum: int) tuple[I, ...] ¶
Pop multiple items from left side of circular array.
- Parameters:
maximum – maximum number of items to pop, may pop less if not enough items
- Returns:
items in the order popped, left to right
- popr() I | Never ¶
Pop item off right side (rear) of circular array.
- Returns:
item popped from right side of circular array
- Raises:
ValueError – when called on an empty circular array
- poprd(default: I) I ¶
Pop one item from right side of the circular array, provide a mandatory default value. “Safe” version of popr.
- Parameters:
default – item returned if circular array is empty
- Returns:
item popped from right side or default item if empty
- poprt(maximum: int) tuple[I, ...] ¶
Pop multiple items from right side of circular array.
- Parameters:
maximum – maximum number of items to pop, may pop less if not enough items
- Returns:
items in the order popped, right to left
- pushl(item: I) None ¶
Push single item onto left side (front) of circular array.
- Parameters:
item – single item pushed onto circular array from left
- Raises:
ValueError – when called on a full CAF
- pushr(item: I) None ¶
Push a single item onto right side (rear) of circular array.
- Parameters:
item – single item pushed onto circular array from right
- Raises:
ValueError – when called on a full fixed storage capacity circular array
- rotl(n: int = 1) None ¶
Rotate items left.
- Parameters:
n – number of times to shift elements to the left
- rotr(n: int = 1) None ¶
Rotate items right.
- Parameters:
n – number of times to shift elements to the right