Module better_functools.pipe

Classes

class Composition (fn: Callable[[T], R],
prev: Composition | None = None)
Expand source code
@dataclass
class Composition(Generic[T, R]):
    fn: Callable[[T], R]
    prev: Composition | None = None

    def __or__(self, fn: Callable[[R], S]) -> Composition[T, S]:
        def _new_fn(v: T) -> S:
            return fn(self(v))

        return Composition(_new_fn, prev=self)

    def __call__(self, arg: T) -> R:
        return self.fn(arg)

    def __iter__(self) -> Iterator[Callable]:
        if self.prev is not None:
            yield from self.prev

        yield self.fn

Composition(fn: 'Callable[[T], R]', prev: 'Composition | None' = None)

Ancestors

  • typing.Generic

Class variables

var fn : Callable[[~T], ~R]
var prevComposition | None
class Pipeline (value: T)
Expand source code
@dataclass
class Pipeline(Generic[T]):
    value: T

    class Unwrap: ...

    unwrap = Unwrap()

    @overload
    def __or__(self, val: Callable[[T], R]) -> Pipeline[R]: ...

    @overload
    def __or__(self, val: Unwrap) -> T: ...

    def __or__(self, val: Callable | Unwrap) -> Pipeline | T:
        match val:
            case Pipeline.Unwrap():
                return self.value
            case _:
                return Pipeline(val(self.value))

Pipeline(value: 'T')

Ancestors

  • typing.Generic

Class variables

var Unwrap
var unwrap
var value : ~T