Module pythonic_fp.circulararray

Pythonic FP - Circular Array data structure

class pythonic_fp.circulararray.CA(ds=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

Parameters:

ds (Iterable[TypeVar(D)] | None)

Initialize circular array with optional initial values.

Parameters:

ds (Iterable[TypeVar(D)] | None) – optional iterable to initial populate the circular array.

Raises:

TypeError – if ds is not Iterable.

capacity()

Find current storage capacity of the circular array.

Return type:

int

Returns:

current capacity of the circular array

empty()

Empty the circular array, keep current capacity.

Return type:

None

foldl(f, initial=None)

Fold left with a function and optional initial value.

Parameters:
  • f (Callable[[TypeVar(L), TypeVar(D)], TypeVar(L)]) – first argument to f is for the accumulated value

  • initial (TypeVar(L) | None) – optional initial value

Raises:

ValueError – when circular array empty and no initial value given

Return type:

TypeVar(L)

foldr(f, initial=None)

Fold right with a function and an optional initial value.

Parameters:
  • f (Callable[[TypeVar(D), TypeVar(R)], TypeVar(R)]) – second argument to f is for the accumulated value

  • initial (TypeVar(R) | None) – optional initial value

Raises:

ValueError – when circular array empty and no initial value given

Return type:

TypeVar(R)

fraction_filled()

Find fraction of capacity filled.

Return type:

float

Returns:

the ratio cnt/capacity

map(f)

Apply function f over the circular array’s contents,

Parameters:

f (Callable[[TypeVar(D)], TypeVar(U)]) – function from type D to type U

Return type:

CA[TypeVar(U)]

Returns:

new circular array instance

popl()

Pop left.

Return type:

TypeVar(D) | Never

Returns:

value popped from left side of circular array

Raises:

ValueError – when called on an empty circular array

popld(default)

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

Parameters:

default (TypeVar(D)) – value returned if circular array is empty

Return type:

TypeVar(D)

Returns:

value popped from left side

poplt(maximum)

Pop multiple values from left side of circular array.

Parameters:

maximum (int) – pop no more than maximum values

Return type:

tuple[TypeVar(D), ...]

popr()

Pop right.

Return type:

TypeVar(D) | Never

Returns:

value popped from right side of circular array

Raises:

ValueError – when called on an empty circular array

poprd(default)

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

Parameters:

default (TypeVar(D)) – value returned if circular array is empty

Return type:

TypeVar(D)

Returns:

value popped from right side

poprt(maximum)

Pop multiple values from right side of circular array.

Parameters:

maximum (int) – pop no more than maximum values

Return type:

tuple[TypeVar(D), ...]

pushl(*ds)

Push left.

Parameters:

ds (TypeVar(D)) – data pushed onto circular array from left

Return type:

None

pushr(*ds)

Push right.

Parameters:

ds (TypeVar(D)) – data pushed onto circular array from right

Return type:

None

resize(minimum_capacity=2)

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 (int) – minimum value to compact the circular array

Return type:

None

rotl(n=1)

Rotate circular array elements left.

Parameters:

n (int) – number of times to shift elements to the left

Return type:

None

rotr(n=1)

Rotate circular array elements right.

Parameters:

n (int) – number of times to shift elements to the right

Return type:

None

pythonic_fp.circulararray.ca(*ts)

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

  • ts (TypeVar(T))

Return type:

CA[TypeVar(T)]