circulararray.auto

Circular array with variable storage capacity.

  • O(1) pops either end

  • O(1) amortized pushes either end

  • O(1) indexing, fully supports slicing

  • auto-resizing more storage capacity when necessary, manually compatible

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

  • comparisons compare identity before equality, like builtins

  • in boolean context, falsy when empty, otherwise truthy

  • factory function ca produces a variable storage capacity circular array from its arguments

class pythonic_fp.circulararray.auto.CA(items: Iterable | None = None)

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

Raises:

TypeError – if items is not Iterable

capacity() int

Return current storage capacity of the circular array.

Returns:

current storage capacity

empty() None

Empty the circular array, keep current storage capacity.

foldl(f: Callable[[L, I], L], start: L | None = None) L | Never

Fold left with a function and optional starting 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]) CA

Apply function f over the circular array’s contents,

Parameters:

f – callable from type I to type U

Returns:

new 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.

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(*items: I) None

Push items onto left side (front) of circular array.

Parameters:

items – items pushed onto circular array from left

pushr(*items: I) None

Push items onto right side (rear) of circular array.

Parameters:

items – items pushed onto circular array from right

resize(minimum_capacity: int = 2) None

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

Parameters:

minimum_capacity – minimum storage capacity to compact the 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

pythonic_fp.circulararray.auto.ca(*items: T) CA

Produce circular array from a variable number of arguments.

Parameters:

items – initial items for a new circular array

Returns:

new variable storage capacity circular array