module splitend

Mutable LIFO stacks safely share immutable data between themselves.

Class SplitEnds

LIFO stacks safely sharing immutable data.

  • each SplitEnd is a very simple stateful (mutable) LIFO stack

  • data can be either “extended” to or “snipped” off the “end”

  • the “root” of a SplitEnd

    • it is fixed and cannot be removed from the SplitEnd

  • different mutable split ends can safely share the same “tail”

  • each SplitEnd sees itself as a singularly linked list

  • bush-like datastructures can be formed using multiple SplitEnds

  • the SplitEnd.split and len methods are O(1)

  • in boolean context returns true if the SplitEnd is not just its “root”

class pythonic_fp.splitends.splitend.SplitEnd(*ds: D, root: ~pythonic_fp.splitends.splitend_node.SENode | _Sentinel = Sentinel('('split', 'end', '_private')'))
Parameters:
  • root_data – Irremovable initial data at bottom of stack.

  • data – Removable data to be pushed onto splitend stack.

__init__(*ds: D, root: ~pythonic_fp.splitends.splitend_node.SENode | _Sentinel = Sentinel('('split', 'end', '_private')')) None
Parameters:
  • root_data – Irremovable initial data at bottom of stack.

  • data – Removable data to be pushed onto splitend stack.

__bool__() bool
Returns:

True is SplitEnd is not just its root node.

__repr__() str

Return repr(self).

__str__() str

Return str(self).

__eq__(other: object, /) bool

Return self==value.

cut(num: int | None = None) tuple[D, ...]

Cut data off end of SplitEnd.

Parameters:

num – Optional number of nodes to cut, default is entire stack.

Returns:

Tuple of data cut off from end.

extend(*ds: D) None

Add data onto the tip of the SplitEnd. Like adding a hair extension.

Parameters:

ds – data to extend the splitend

peak() D

Return the data at end (top) of SplitEnd without consuming it.

Returns:

The data at the end of the SplitEnd.

root() SENode
Returns:

The root SENode node of the SplitEnd.

reroot(root: SENode) SplitEnd[D]

Create a brand new SplitEnd with the same data but different root.

Note

Two nodes are compatible root nodes if and only if

  • they are both actually root nodes

    • which implies that their previous nodes are themselves

  • their data compare as equal

    • comparing by identity is too strong for some use cases

Returns:

New SplitEnd with the same data and the new root.

Raises:

ValueError – If new and original root nodes are not compatible.

snip() D

Snip data off tip of SplitEnd. Just return data if tip is root.

Returns:

Data snipped off tip, just return root data if at root.

split(*ds: D) SplitEnd[D]

Split the end and add more data.

Returns:

New instance, same data nodes plus additional ones on end.

fold(f: Callable[[D, D], D]) D
fold(f: Callable[[T, D], T], init: T) T

Reduce with a function, folding from tip to root.

Parameters:
  • f – Folding function, first argument is for the accumulator.

  • init – Optional initial starting value for the fold.

Returns:

Reduced value folding from tip to root in natural LIFO order.

rev_fold(f: Callable[[D, D], D]) D
rev_fold(f: Callable[[T, D], T], init: T) T

Reduce with a function, fold from root to tip.

Parameters:
  • f – Folding function, first argument is for the accumulator.

  • init – Optional initial starting value for the fold.

Returns:

Reduced value folding from root to tip.