Modules 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
lazy
which caches its result
- class pythonic_fp.fptools.lazy.Lazy(f, d, pure=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 Callablef
with its argument at a later time.first argument
f
taking values of typeD
to values of typeR
second argument
arg: D
is the argument to be passed tof
where the type
D
is thetuple
type of the argument types tof
function is evaluated when the
eval
method is calledresult is cached unless
pure
is set toFalse
returns 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.
- Parameters:
f (
Callable
[[TypeVar
(D
)],TypeVar
(R
, contravariant=True)])d (
TypeVar
(D
))pure (
bool
)
- eval()¶
Evaluate function with its argument.
evaluate function
cache result or exception if
pure == True
reevaluate if
pure == False
- Return type:
None
- get(alt=None)¶
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.
- Parameters:
alt (
TypeVar
(R
, contravariant=True) |None
)- Return type:
TypeVar
(R
, contravariant=True) |Never
- get_exception()¶
Get result only if evaluate and exceptional.
- Return type:
MayBe
[Exception
]
- get_result()¶
Get result only if evaluated and not exceptional.
- Return type:
MayBe
[TypeVar
(R
, contravariant=True)]
- got_exception()¶
Return true if Lazy raised exception.
- Return type:
MayBe
[bool
]
- got_result()¶
Return true if an evaluated Lazy did not raise an exception.
- Return type:
MayBe
[bool
]
- pythonic_fp.fptools.lazy.lazy(f, *args, **kwargs)¶
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
f
takes a functionnext positional arguments are the arguments to be applied later to
f
f
is reevaluated whenevereval
method of the returnedLazy
is called
any kwargs passed are ignored
if
f
needs them, then wrapf
in another function
- Parameters:
f (
Callable
[[ParamSpec
(P
)],TypeVar
(R
)])args (
ParamSpecArgs
)kwargs (
ParamSpecKwargs
)
- Return type:
Lazy
[tuple
[Any
,...
],TypeVar
(R
)]
- pythonic_fp.fptools.lazy.real_lazy(f, *args, **kwargs)¶
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
f
takes a functionnext positional arguments are the arguments to be applied later to
f
f
is evaluated wheneval
method of the returnedLazy
is calledf
is evaluated only once with results cached
any kwargs passed are ignored
if
f
needs them then wrapf
in another function
- Parameters:
f (
Callable
[[ParamSpec
(P
)],TypeVar
(R
)])args (
ParamSpecArgs
)kwargs (
ParamSpecKwargs
)
- Return type:
Lazy
[tuple
[Any
,...
],TypeVar
(R
)]