Skip to content

apfel.container.value

apfel.container.value provides a container that simply wraps a value designed to aid chained method calls.

See also Identity.

API#

Classes

NameDescription
ValueNo description.

Valueclass#

class Value(value: T)

Bases: Monad

__doc__attributeinstance attribute#

__doc__ = value.__doc__

__slots__attributeclass attributeinstance attribute#

__slots__ = ('_value')

__class_getitem__method#

def __class_getitem__(item: type[U]) -> Value[U]

__init__method#

def __init__(value: T)

__repr__method#

def __repr__()

__str__method#

def __str__()

applymethod#

def apply(func: Value[Callable[[T], R]], /) -> Value[R]

Apply a function wrapped inside a Value to the inner value. Implementation of Applicative.apply.

Parameters

NameTypeDescription
funcValue[Callable[[T], R]]A Value containing a function to apply.

Returns

Value[R]
Value[R]: A new Value containing the result of the function application.

bindmethod#

def bind(func: Callable[[T], Value[R]], /) -> Value[R]

Monadically bind a function that maps the inner value to a new Value. Implementation of Monad.bind.

Parameters

NameTypeDescription
funcCallable[[T], Value[R]]A function that takes the inner value and returns a Value.

Returns

Value[R]
Value[R]: A new Value containing the result of the function.

donemethod#

def done() -> T

Return the value wrapped inside the container.

Unlike unwrap methods on other containers, this method will never raise exceptions.

mapmethod#

def map(func: Callable[[T], U], /) -> Value[U]

Map a function over the Value container. Implementation of Functor.map.

Parameters

NameTypeDescription
funcCallable[[T], U]A function to transform the inner value.

Returns

Value[U]
Value[U]: A new Value containing the transformed value.

mutatemethod#

def mutate(func: Callable[[T], Any], /) -> Self

Call a function to mutate the inner value in place.

The function’s return value is ignored, and the container keeps the original inner reference. This is similar to Value.tap, just hinting the mutating intention of the function.

Parameters

NameTypeDescription
funcCallable[[T], Any]A function that mutates the inner value.

Returns

Self
Value[T]: The mutated container itself.

puremethodclassmethod#

def pure(value: R | T, /) -> Value[R] | Value[T]

Wrap a value into the Value container. Implementation of Applicative.pure.

Parameters

NameTypeDescription
valueTThe value to wrap.

Returns

Value[R] | Value[T]
Value[T]: A new instance of Value with the value wrapped.

runmethod#

def run(func: Callable[[T], R], /) -> R

Run a function to process the inner value, and return the result.

Parameters

NameTypeDescription
funcCallable[[T], R]A function to process the value.

Returns

R
The result of the function.

tapmethod#

def tap(func: Callable[[T], Any], /) -> Value[T]

Call a function over the inner value, ignore the return value, and return the original container.

See also also.

updatemethod#

def update(func: Callable[[T], T], /) -> Self

Update the inner value with the result of a function.

Parameters

NameTypeDescription
funcCallable[[T], T]A function to transform the inner value.

Returns

Self
Value[T]: The mutated container itself.