module immutable_list

class pythonic_fp.containers.immutable_list.IList

Bases: Hashable, Generic

immutable list like data structure

  • hashability should be enforced by LSP tooling

  • hashability will be enforced at runtime

  • its method type parameters are also covariant

  • supports both indexing and slicing

  • addition and left & right int multiplication supported

    • addition is concatenation resulting in a union type

__init__(*ds: Iterable) None

init

param ds:

One optional iterable.

__hash__() int

hash

Hashability enforced at runtime by initializer.

returns:

Hash value.

__iter__() Iterator

iterate

Forward iterate, left to right.

yields:

The contained values.

__reversed__() Iterator

iterate

Reverse iterate, left to right.

yields:

The contained values in reverse order.

__bool__() bool

bool

returns:

True if not empty, False if empty.

__len__() int

len

returns:

The number of items in the Ilist.

__eq__(other: object, /) bool

Return self==value.

__getitem__(idx: int, /) D
__getitem__(idx: slice, /) IList[D]

getitem

Indexable and sliceable but otherwise immutable.

param idx:

Either a slice or and index.

returns:

A slice or the value.

__repr__() str

repr string

returns:

A string to reproduce the value.

__str__() str

user string

returns:

A string meaningful to an end user.

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

fold left

param f:

Folding function, first argument is for the accumulated value.

param start:

Optional starting value.

param default:

Optional default value if fold does not exist.

returns:

Left folded value.

raises ValueError:

When empty and neither a start value or default value not given.

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

fold right

param f:

Folding function, second argument is for the accumulated value.

param start:

Optional starting value.

param default:

Optional default value if fold does not exist.

returns:

Right folded value.

raises ValueError:

When empty and neither a start value or default value not given.

__add__(other: IList[D], /) IList[D]

add by concatenation

param other:

The other same typed IList.

returns:

New IList instance with concatenated values.

raises ValueError:

When other is not an IList.

TODO

Return NotImplemented if appropriate?

__mul__(num: int, /) IList[D]

add by concatenation

param other:

The other same typed IList.

returns:

New IList instance with concatenated values.

raises ValueError:

When other is not an IList.

TODO

Consider returning NotImplemented if appropriate? Maybe define a “column vector” version?

__rmul__(num: int, /) IList[D]

concatenation with itself

param num:

Number of times to concatenate IList with itself.

returns:

A new IList instance with concatenated values.

raises ValueError:

When other is not an IList.

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

accumulate partial folds

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

param f:

Folding function used to produce partial folds.

param s:

Optional starting value.

returns:

New IList of the partial folds.

map(f: Callable[[D], U], /) IList

map

parameter f:

Function to map over the IList.

returns:

A new IList instance with the mapped values.

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

bind

param f:

Function D -> IList[U] to bind with different merge strategies.

param merge_type:

MergeEnum to determine how to merge the result.

param yield_partials:

Yield unmatched values if MergeEnum given as merge type.

return:

New IList instance.

raises ValueError:

If given an unknown merge enumeration.