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(f) #

Apply a function wrapped inside a Value to the inner value.

Parameters:

Name Type Description Default
f 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(f) #

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

Parameters:

Name Type Description Default
f 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(f) #

Map a function over the Value container.

pipe(func) #

pipe(func: Callable[[T], T]) -> Self
pipe(func: Callable[[T], None]) -> Self

Run a function to process the inner value. If a result is produced, the result will be put back to the container. Otherwise, the original reference within the container will be kept.

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, or return None.

Parameters:

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

A function to process the value.

required

Returns:

Type Description
Value[T]

The mutated container itself.

pure(x) classmethod #

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

Wrap a value into the Value container.

Parameters:

Name Type Description Default
x 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.pipe method instead.