pythonic-fp.circulararray API

Detailed Documentation

Pythonic FP namespace project - Circular Array data structure

class pythonic_fp.circulararray.CA(ds: Iterable[D] | None = None)

Indexable circular array data structure

  • O(1) pops either end

  • O(1) amortized pushes either end

  • O(1) indexing, fully supports slicing

  • Auto-resizing larger when necessary, manually compatible

  • Iterable, can safely mutate while iterators continue iterating over previous state

  • comparisons compare identity before equality, like builtins do

  • in boolean context returns

    • True when not empty

    • False when empty

capacity() int

Find current storage capacity of the circular array.

Returns:

current capacity of the circular array

empty() None

Empty the circular array, keep current capacity.

foldl(f: Callable[[L, D], L], initial: L | None = None, /) L

Left fold with a function and optional initial value.

Parameters:
  • f – first argument to f is for the accumulated value

  • initial – optional initial value

Raises:

ValueError – when circular array empty and no initial value given

foldr(f: Callable[[D, R], R], initial: R | None = None, /) R

Right fold with a function and an optional initial value.

Parameters:
  • f – second argument to f is for the accumulated value

  • initial – optional initial value

Raises:

ValueError – when circular array empty and no initial value given

fraction_filled() float

Find fraction of capacity filled.

Returns:

the ratio cnt/capacity

map(f: Callable[[D], U], /) CA[U]

Apply function f over the circular array’s contents,

Parameters:

f – function from type D to type U

Returns:

new circular array instance.

popl() D | Never

Pop left.

Returns:

value popped from left side of circular array

Raises:

ValueError – when called on an empty circular array

popld(default: D, /) D

Pop one value from left side of the circular array, provide a mandatory default value. “Safe” version of popl.

Parameters:

default – value returned if circular array is empty

Returns:

value popped from left side

poplt(maximum: int, /) tuple[D, ...]

Pop multiple values from left side of the circular array.

Parameters:

maximum – pop no more than maximum values

Returns:

tuple of values popped from left side

popr() D | Never

Pop right.

Returns:

value popped from right side of circular array

Raises:

ValueError – when called on an empty circular array

poprd(default: D, /) D

Pop one value from right side of the circular array, provide a mandatory default value. “Safe” version of popr.

Parameters:

default – value returned if circular array is empty

Returns:

value popped from right side

poprt(maximum: int, /) tuple[D, ...]

Pop multiple values from right side of the circular array.

Parameters:

maximum – pop no more than maximum values

Returns:

tuple of values popped from right side

pushl(*ds: D) None

Push left.

Parameters:

ds – data pushed from left onto circular array

pushr(*ds: D) None

Push right.

Parameters:

ds – data pushed from right onto circular array

resize(minimum_capacity: int = 2) None

Compact circular array and resize to a minimum capacity if necessary. To just compact the circular array, do not provide a minimum capacity.

Parameters:

minimum_capacity – minimum value to compact the circular array

rotl(n: int = 1, /) None

Rotate circular array elements left.

Parameters:

n – number of times to shift elements to the left

rotr(n: int = 1, /) None

Rotate circular array elements right.

Parameters:

n – number of times to shift elements to the right

pythonic_fp.circulararray.ca(*ds: D) CA

Function to produce a circular array from a variable number of arguments.

Parameters:

ds – initial values to push onto a new circular array from right to left