wrap

Module wrap

Wrap objects in ways to make them “immutable.”

  • Class Wrap: wrap an object

  • Class HWrap: wrap a hashable object

class pythonic_fp.gadgets.wrap.Wrap

Bases: Generic

Wrap object

Immutablely wrap exactly one value of a given type. Wrap objects can be used in Python match statements.

__init__(item: T) None

Initializer

Initialize Wrap with 1 required item.

Parameters:

item – Item to be wrapped.

__bool__() bool

Bool

Truthiness same as wrapped object.

__iter__() Iterator

Iter

Iterable, iterates wrapped item.

__str__() str

User string

Construct string ‘Box(item_str)’ where item_str = str(item) for the currently contained item.

Returns:

A string to reproduce the current state of the Box.

__eq__(other: object) bool

Equality comparison

Efficiently compare Wrap to another object.

Parameters:

other – The object to be compared with,

Returns:

True if other is of type Wrap and wraps an object which compares as equal to the wrapped object, otherwise False.

map(f: Callable[[T], U]) Wrap

Map

Map function f over contents.

Map function f over contents.

Parameters:

f – Mapping function.

Returns:

New instance.

bind(f: Callable[[T], Wrap]) Wrap

Bind

Flatmap wrapped object with function f.

Parameters:

f – Binding function.

Returns:

New instance.

class pythonic_fp.gadgets.wrap.HWrap

Bases: Hashable, Generic

Wrap hashable object

Immutablely wrap exactly one value of a given hashable type. HWrap objects can be used in Python match statements.

Tip

HWrap objects are hashable.

__init__(item: T) None
__hash__() int

Return hash(self).

__bool__() bool

Bool

Truthiness same as wrapped object.

__iter__() Iterator

Iter

Iterable, iterates wrapped item.

__repr__() str

Return repr(self).

__eq__(other: object) bool

Equality comparison

Efficiently compare to another object.

Parameters:

other – Object to be compared

Returns:

True if other is of type HWrap and wraps an object which compares as equal to the wrapped object, otherwise False.

map(f: Callable[[T], U]) HWrap

Map

Map function f over wrapped the wrapped object returning a new HWrap instance.

Parameters:

f – Mapping function.

Returns:

New instance.

bind(f: Callable[[T], HWrap]) HWrap

Bind

Flatmap Box with function f.

Parameters:

f – Binding function.

Returns:

New instance.