Skip to content

Value#

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

See also Identity.

Value #

apply(func) #

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

Parameters:

Name Type Description Default
func Value[Callable[[T], R]]

A Value containing a function to apply.

required

Returns:

Type Description
Value[R]

A new Value containing the result of the function application.

bind(func) #

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

Parameters:

Name Type Description Default
func Callable[[T], Value[R]]

A function that takes the inner value and returns a Value.

required

Returns:

Type Description
Value[R]

A new Value containing the result of the function.

done() #

Return the value wrapped inside the container.

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

map(func) #

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

Parameters:

Name Type Description Default
func Callable[[T], U]

A function to transform the inner value.

required

Returns:

Type Description
Value[U]

A new Value containing the transformed value.

mutate(func) #

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:

Name Type Description Default
func Callable[[T], Any]

A function that mutates the inner value.

required

Returns:

Type Description
Value[T]

The mutated container itself.

pure(value) classmethod #

pure(value: R) -> Value[R]
pure(value: T) -> Value[T]

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

Parameters:

Name Type Description Default
value T

The value to wrap.

required

Returns:

Type Description
Value[T]

A new instance of Value with the value wrapped.

run(func) #

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

Parameters:

Name Type Description Default
func Callable[[T], R]

A function to process the value.

required

Returns:

Type Description
R

The result of the function.

tap(func) #

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

See also also.

Warning

Pragmatically, the function shouldn't mutate the inner value. Use the Value.mutate method instead.

update(func) #

Update the inner value with the result of a function.

Warning

Compare to Value.map, this method mutates the container in place. No new container is created. Therefore, the function must return a result of the same type as the previous inner type of the container.

Parameters:

Name Type Description Default
func Callable[[T], T]

A function to transform the inner value.

required

Returns:

Type Description
Value[T]

The mutated container itself.