module auto

Variable storage capacity circular array.**

  • 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, safely mutates while iterators iterating over previous state

  • comparisons compare identity before equality, like builtins

  • in boolean context, falsy when empty, otherwise truthy

  • function ca produces auto-resizing circular array from arguments

class pythonic_fp.circulararray.auto.CA
__init__(*items: Iterable) None
Parameters:

items – Optionally takes a single iterable to initially populate the circular array.

Raises:
  • TypeError – When items[0] not iterable.

  • ValueError – If more than 1 iterable is given.

__repr__() str

Return repr(self).

__str__() str

Return str(self).

__eq__(other: object) bool
Parameters:

other – The object to be compared to.

Returns:

True if object is another CA whose items compare as equal to the corresponding items in the CA, otherwise False.

pushl(*items: I) None

Push items on from left.

Parameters:

items – Items pushed onto circular array from left (front).

pushr(*items: I) None

Push items on from right.

Parameters:

items – Items pushed onto circular array from right (rear).

popl() I

Pop single item off from left side.

Returns:

Item popped from left side (front) of circular array.

Raises:

ValueError – When called on an empty circular array.

popr() I

Pop single item off from right.

Returns:

Item popped from right side (rear) 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.

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.

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:

Tuple of items in the order popped, left to right.

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:

Tuple of items in the order popped, right to left.

rotl(n: int = 1) None

Rotate items to the left.

Parameters:

n – Number of times to shift elements to the left.

rotr(n: int = 1) None

Rotate items to the right.

Parameters:

n – Number of times to shift elements to the right.

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 auto-resizing circular array instance.

foldl(f: Callable[[I, I], I]) I
foldl(f: Callable[[L, I], L], start: L) L

Fold left with a function and optional starting item.

Parameters:
  • f – Folding function, 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, I], I]) I
foldr(f: Callable[[I, R], R], start: R) R

Fold right with a function and an optional starting item.

Parameters:
  • f – Folding function, 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.

capacity() int

Return current storage capacity of the circular array.

Returns:

Current storage capacity.

empty() None

Empty the circular array, keep current storage capacity.

fraction_filled() float

Find fraction of the storage capacity which is filled.

Returns:

The ratio count/capacity.

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.

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

Produce circular array from a variable number of arguments.

Parameters:

items – Initial items for a new auto-resizing circular array.

Returns:

New variable storage capacity circular array.