Skip to content

Functional utilities

pipe, curry, and retry are small helpers for function composition, partial application, and retrying asynchronous work.

fpstreams.functional

Function composition, staged argument binding, and asynchronous retries.

pipe

pipe(value: T, *functions: Callable[[Any], Any]) -> Any

Pass a value through a left-to-right sequence of callables.

Each callable receives the preceding callable's return value. With no callables, the original value is returned unchanged.

Parameters:

Name Type Description Default
value T

The first callable's input.

required
*functions Callable[[Any], Any]

Unary callables to invoke in order.

()

Returns:

Type Description
Any

The final callable's return value, or value when functions is empty.

curry

curry(function: Callable[..., T]) -> Callable[..., Any]

Wrap a callable so its arguments can be supplied across multiple calls.

The wrapped callable executes as soon as every non-variadic parameter without a default has been bound. Arguments may still be supplied all at once, and invalid or duplicate arguments raise the same binding errors produced by inspect.signature.

Parameters:

Name Type Description Default
function Callable[..., T]

A callable whose signature can be inspected.

required

Returns:

Type Description
Callable[..., Any]

A wrapper that preserves function metadata. It calls function once all required arguments are bound; otherwise it returns a callable for the next arguments.

retry

retry(
    attempts: int = 3,
    backoff: float = 2.0,
    jitter: bool = True,
    exceptions: tuple[type[Exception], ...] = (Exception,),
    *,
    delay: float = 0.0,
) -> Callable[
    [Callable[P, Awaitable[T]]], Callable[P, Awaitable[T]]
]

Decorate an async callable with bounded retries and exponential delay.

attempts includes the initial call. Only exceptions are retried; all other exceptions propagate immediately. Before each retry, the current delay is optionally increased by a random value of up to ten percent, then multiplied by backoff for the next retry.

Parameters:

Name Type Description Default
attempts int

Maximum calls, including the initial call; must be at least one.

3
backoff float

Non-negative multiplier applied after each retry delay.

2.0
jitter bool

Whether to add up to ten percent random jitter to nonzero delays.

True
exceptions tuple[type[Exception], ...]

Exception classes that trigger another attempt.

(Exception,)
delay float

Non-negative seconds to wait before the first retry.

0.0

Returns:

Type Description
Callable[[Callable[P, Awaitable[T]]], Callable[P, Awaitable[T]]]

A decorator whose wrapper retries the asynchronous callable under this policy.

Raises:

Type Description
ValueError

If attempts is less than one or a delay parameter is negative.