module functional_tuple

Pythonic FP - Functional Tuple

class pythonic_fp.containers.functional_tuple.FTuple(iterable=(), /)

Functional Tuple suitable for inheritance.

  • supports both indexing and slicing

  • int multiplication and FTuple addition supported

  • addition concatenates results, resulting in a Union type

  • both left and right int multiplication supported

  • homogeneous in its data type

  • supports being further inherited from

  • unslotted

__repr__() str

Return repr(self).

__eq__(other: object, /) bool

Return self==value.

__getitem__(idx: SupportsIndex) D
__getitem__(idx: slice) tuple[D, ...]

Return self[key].

foldl(f: Callable[[L, D], L], /, start: L | None = None, default: L | None = None) L

Fold Left with optional starting and default values.

  • fold left with an optional starting value

  • first argument of function f is for the accumulated value

Parameters:
  • f – The folding function, first argument is for the accumulated value.

  • start – An optional starting value.

  • default – An optional default value if fold does not exist.

Raises:

ValueError – When FTuple empty and a start value not given.

Returns:

The folded value if it exists, otherwise the default value.

foldr(f: Callable[[D, R], R], /, start: R | None = None, default: R | None = None) R

Fold Right with optional starting and default values.

Parameters:
  • f – The folding function, second argument is for the accumulated value.

  • start – An optional starting value.

  • default – An optional default value if fold does not exist.

Raises:

ValueError – when FTuple empty and a start value not given

Returns:

The folded value if it exists, otherwise the default value.

copy() FTuple[D]

Return a shallow copy of FTuple in O(1) time & space complexity.

Returns:

New FTuple.

__add__(other: object, /) tuple[D, ...]

Return self+value.

__mul__(num: SupportsIndex) tuple[D, ...]

Return self*value.

__rmul__(num: SupportsIndex) tuple[D, ...]

Return value*self.

accummulate(f: Callable[[L, D], L], s: L | None = None, /) FTuple

Accumulate partial folds.

Accumulate partial fold with an optional starting value, results in an FTuple.

Parameters:
  • f – Folding function used to produce partial folds.

  • s – Optional starting value.

Returns:

New FTuple of the partial folds.

bind(f: Callable[[D], FTuple[U]], merge_type: MergeEnum = MergeEnum.Concat, yield_partials: bool = False) FTuple[U] | Never

Bind function f to the FTuple.

Parameters:
  • f – Function D -> FTuple[U]

  • merge_typeMergeEnum to determine how to merge the result.

  • yield_partials – Yield unmatched values if MergeEnum given as merge type.

Returns:

Resulting FTuple.

Raises:

ValueError – If given an unknown merge enumeration.