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.
- Parameters:
f – single argument function
d – argument to be passed to
fpure – if true, cache the result for future
evalmethod calls
- Returns:
an object that can call the function
fat a later time
- __init__(f: Callable[[D], R], d: D, pure: bool = True) None¶
- Parameters:
f – single argument function
d – argument to be passed to
fpure – 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
- got_exception() MayBe[bool]¶
Return true if Lazy raised exception. :returns:
Trueonly ifLazyraised an exception.
- 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
- 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.
- Parameters:
f – Function whose evaluation is to be delayed.
args – Positional arguments to be passed to
f.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.
- Parameters:
f – Function whose evaluation is to be delayed.
args – Positional arguments to be passed to
f.kwargs – Any kwargs given are ignored.
- Returns:
a
Lazyobject wrapping the evaluation off