fptools.lazy¶
Pythonic FP - 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)¶
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.first argument
ftaking values of typeDto values of typeRsecond argument
arg: Dis the argument to be passed tofwhere the type
Dis thetupletype of the argument types tof
function is evaluated when the
evalmethod is calledresult is cached unless
pureis set toFalsereturns True in Boolean context if evaluated
Usually use case is to make a function “non-strict” by passing some of its arguments wrapped in Lazy instances.
- eval() None¶
Evaluate function with its argument.
evaluate function
cache result or exception if
pure == Truereevaluate if
pure == False
- get(alt: R | None = None) R | Never¶
Get result only if evaluated and no exceptions occurred, otherwise return an alternate value.
A possible use case would be if the calculation is expensive, but if it has already been done, its result is better than the alternate value.
- pythonic_fp.fptools.lazy.lazy(f: Callable[[P], R], *args: P, **kwargs: P) Lazy[tuple[Any, ...], R]¶
Delayed evaluation of a function with arbitrary positional arguments.
Function returning a delayed evaluation of a function of an arbitrary number of positional arguments.
first positional argument
ftakes a functionnext positional arguments are the arguments to be applied later to
ffis reevaluated wheneverevalmethod of the returnedLazyis called
any kwargs passed are ignored
if
fneeds them, then wrapfin another function
- pythonic_fp.fptools.lazy.real_lazy(f: Callable[[P], R], *args: P, **kwargs: P) Lazy[tuple[Any, ...], R]¶
Cached delayed evaluation of a function with arbitrary positional arguments.
Function returning a delayed evaluation of a function of an arbitrary number of positional arguments.
first positional argument
ftakes a functionnext positional arguments are the arguments to be applied later to
ffis evaluated whenevalmethod of the returnedLazyis calledfis evaluated only once with results cached
any kwargs passed are ignored
if
fneeds them then wrapfin another function