Development API docs

Circular Array

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

Initialize circular array with optional initial values.

Parameters:

ds (Optional[Iterable[~D]]) – 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[L, ~D, L]) – first argument to f is for the accumulated value

  • initial (Optional[L]) – optional initial value

Raises:

ValueError – when circular array empty and no initial value given

Return type:

L

foldr(f, initial=None, /)

Fold right with a function and an optional initial value.

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

  • initial (Optional[R]) – optional initial value

Raises:

ValueError – when circular array empty and no initial value given

Return type:

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[~D, U]) – function from type D to type U

Return type:

CA[U]

Returns:

new circular array instance

popl()

Pop left.

Return type:

Union[~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 (~D) – value returned if circular array is empty

Return type:

~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[~D, …]

popr()

Pop right.

Return type:

Union[~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 (~D) – value returned if circular array is empty

Return type:

~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[~D, …]

pushl(*ds)

Push left.

Parameters:

ds – data pushed onto circular array from left

Return type:

None

pushr(*ds)

Push right.

Parameters:

ds – 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

Return type:

CA[T]