Skip to content

Checkable

Bases: Protocol

Mixin class providing conditional chaining methods based on truthiness.

This class provides methods inspired by Rust's bool type for conditional execution and wrapping in Option or Result types.

All methods evaluate the instance's truthiness to determine their behavior.

ok_or(err)

Wrap Self in a Result[Self, E] based on its truthiness.

Truthiness is determined by __bool__() if defined, otherwise by __len__() if defined (returning False if length is 0), otherwise all instances are truthy (Python's default behavior).

Parameters:

Name Type Description Default
err E

The error value to wrap in Err if self is falsy.

required

Returns:

Type Description
Result[Self, E]

Result[Self, E]: Ok(self) if self is truthy, Err(err) otherwise.

Example:

>>> import pyochain as pc
>>> pc.Seq([1, 2, 3]).ok_or("empty")
Ok(Seq(1, 2, 3))
>>> pc.Seq([]).ok_or("empty")
Err('empty')

ok_or_else(func, *args, **kwargs)

Wrap Self in a Result[Self, E] based on its truthiness.

E being the return type of func.

The function is only called if self evaluates to False.

Truthiness is determined by __bool__() if defined, otherwise by __len__() if defined (returning False if length is 0), otherwise all instances are truthy (Python's default behavior).

Parameters:

Name Type Description Default
func Callable[Concatenate[Self, P], E]

A callable that returns the error value to wrap in Err.

required
*args P.args

Positional arguments to pass to the function.

()
**kwargs P.kwargs

Keyword arguments to pass to the function.

{}

Returns:

Type Description
Result[Self, E]

Result[Self, E]: Ok(self) if self is truthy, Err(f(...)) otherwise.

Example:

>>> import pyochain as pc
>>> pc.Seq([1, 2, 3]).ok_or_else(lambda s: f"empty seq")
Ok(Seq(1, 2, 3))
>>> pc.Seq([]).ok_or_else(lambda s: f"empty seq")
Err('empty seq')

then(func, *args, **kwargs)

Wrap Self in an Option[R] based on its truthiness.

R being the return type of func.

The function is only called if Self evaluates to True (lazy evaluation).

Truthiness is determined by __bool__() if defined, otherwise by __len__() if defined (returning False if length is 0), otherwise all instances are truthy (Python's default behavior).

Parameters:

Name Type Description Default
func Callable[Concatenate[Self, P], R]

A callable that returns the value to wrap in Some.

required
*args P.args

Positional arguments to pass to func.

()
**kwargs P.kwargs

Keyword arguments to pass to func.

{}

Returns:

Type Description
Option[R]

Option[R]: Some(R) if self is truthy, NONE otherwise.

Example:

>>> import pyochain as pc
>>> pc.Seq([1, 2, 3]).then(lambda s: s.sum())
Some(6)
>>> pc.Seq([]).then(lambda s: s.sum())
NONE

then_some()

Wraps Self in an Option[Self] based on its truthiness.

Truthiness is determined by __bool__() if defined, otherwise by __len__() if defined (returning False if length is 0), otherwise all instances are truthy (Python's default behavior).

Returns:

Type Description
Option[Self]

Option[Self]: Some(self) if self is truthy, NONE otherwise.

Example:

>>> import pyochain as pc
>>> pc.Seq([1, 2, 3]).then_some()
Some(Seq(1, 2, 3))
>>> pc.Seq([]).then_some()
NONE