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 lazy which 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 Callable f with 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 eval method calls

returns:

an object that can call the function f at a later time

eval() None

Evaluate function with its argument.

  • evaluate function

  • cache result or exception if pure is True

  • reevaluate 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 Lazy is exceptional

Returns:

the successfully evaluated result, otherwise alt if given

Raises:

ValueError – if method called on a Lazy which 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.

got_exception() MayBe[bool]

Return true if Lazy raised exception. :returns: True only if Lazy raised an exception.

got_result() MayBe[bool]
Returns:

True only if an evaluated Lazy did not raise an exception.

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 Lazy object wrapping the evaluation of f

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 Lazy object wrapping the evaluation of f