Skip to content

Pipeable

Bases: Protocol

Mixin class providing pipeable methods for fluent chaining.

inspect(func, *args, **kwargs)

Pass Self to func to perform side effects without altering the data.

This method is very useful for debugging or passing the instance to other functions for side effects, without breaking the fluent method chaining.

Parameters:

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

Function to apply to the instance for side effects.

required
*args P.args

Positional arguments to pass to func.

()
**kwargs P.kwargs

Keyword arguments to pass to func.

{}

Returns:

Name Type Description
Self Self

The instance itself, unchanged.

Example:

>>> import pyochain as pc
>>> pc.Seq([1, 2, 3, 4]).inspect(print).last()
Seq(1, 2, 3, 4)
4

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:

>>> import pyochain as pc
>>> from collections.abc import Sequence
>>> import hashlib
>>> def sha256_hex(data: Sequence[int]) -> str:
...     return hashlib.sha256(bytes(data)).hexdigest()
>>>
>>> pc.Seq([1, 2, 3]).into(sha256_hex)
'039058c6f2c0cb492c533b0a4d14ef77cc0f78abccced5287d84a1a2011cfb81'