fptools.lazy¶
Lazy function evaluation¶
Delayed function evaluations. FP tools for “non-strict” function evaluations. Useful to delay a function’s evaluation until some inner scope.
Non-strict delayed function evaluation.
class Lazy - Delay evaluation of functions taking & returning single values
function lazy - Delay evaluation of functions taking any number of values
function real_lazy - Version of
lazywhich caches its result
- class pythonic_fp.fptools.lazy.Lazy(f: Callable[[D], R], d: D, pure: bool = True)¶
Non-strict function evaluation¶
Delayed evaluation of a singled valued function.
Class instance delays the executable of a function where
Lazy(f, arg)constructs an object that can evaluate the Callablefwith its argument at a later time.Note
Usually use case is to make a function “non-strict” by passing some of its arguments wrapped in Lazy instances.
- param f:
single argument function
- param d:
argument to be passed to
f- param pure:
if true, cache the result for future
evalmethod calls- returns:
an object that can call the function
fat a later time
- eval() None¶
Evaluate function with its argument.
evaluate function
cache result or exception if
pure is Truereevaluate if
pure is False
- get(alt: R | None = None) R¶
Get result only if evaluated and no exceptions occurred, otherwise return an alternate value.
- Parameters:
alt – optional alternate value to return if
Lazyis exceptional- Returns:
the successfully evaluated result, otherwise
altif given- Raises:
ValueError – if method called on a
Lazywhich was not yet evaluated
- get_exception() MayBe[Exception]¶
Get result only if evaluate and exceptional.
- Returns:
The exception thrown wrapped in a maybe monad.
- get_result() MayBe¶
Get result only if evaluated and not exceptional.
- Returns:
The result wrapped in a maybe monad.
- pythonic_fp.fptools.lazy.lazy(f: Callable[[P], R], *args: P, **kwargs: P) Lazy[tuple[Any, ...], R]¶
Delayed evaluations¶
Function returning a delayed evaluation of a function of an arbitrary number of positional arguments.
- param f:
Function whose evaluation is to be delayed.
- param args:
Positional arguments to be passed to
f.- param kwargs:
Any kwargs given are ignored.
- returns:
a
Lazyobject wrapping the evaluation off
- pythonic_fp.fptools.lazy.real_lazy(f: Callable[[P], R], *args: P, **kwargs: P) Lazy[tuple[Any, ...], R]¶
Cached Delayed evaluations¶
Function returning a delayed evaluation of a function of an arbitrary number of positional arguments. Evaluation is cached.
- param f:
Function whose evaluation is to be delayed.
- param args:
Positional arguments to be passed to
f.- param kwargs:
Any kwargs given are ignored.
- returns:
a
Lazyobject wrapping the evaluation off