Skip to content

Into

Bases: Protocol


              flowchart TD
              pyochain.rs.Into[Into]

              

              click pyochain.rs.Into href "" "pyochain.rs.Into"
            

Mixin class providing the into method for fluent chaining.

into(func, *args, **kwargs)

Convert Self to R.

This method allows to pipe the instance into an object or function that can convert Self into another type.

Conceptually, this allow to do x.into(f) instead of f(x), hence keeping a fluent chaining style.

Parameters:

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

Function for conversion.

required
*args P.args

Positional arguments to pass to func.

()
**kwargs P.kwargs

Keyword arguments to pass to func.

{}

Returns:

Name Type Description
R R

The converted value.

Example
>>> from pyochain import Seq, Result, Ok, Err
>>> from collections.abc import Sequence
>>> def check_data(data: Sequence[int]) -> Result[Sequence[int], str]:
...     if len(data) == 0:
...         return Err("Empty data")
...     return Ok(data)
>>>
>>> def handle_result(res: Result[Sequence[int], str]) -> str:
...     match res:
...         case Ok(data):
...             return f"Data is valid: {data}"
...         case Err(err):
...             return f"Data is invalid: {err}"
>>>
>>> Seq((1, 2, 3)).into(check_data).into(handle_result)
'Data is valid: Seq(1, 2, 3)'