lazy

Lazy function evaluation

Non-strict delayed function evaluation.

  • class Lazy - Delay evaluation of single argument functions

  • function lazy - Delay evaluation of functions taking any number of arguments

  • function real_lazy - Caching version of lazy.

class pythonic_fp.fptools.lazy.Lazy

Bases: Generic

Lazy (delayed) function evaluation

Delayed evaluation of a singled valued function.

Tip

Make a function “non-strict” by passing some of its arguments wrapped in Lazy instances.

__init__(f: Callable[[D], R], d: D, pure: bool = True) None

initialize

Delayed evaluation of a single argument function.

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:

A Lazy instance which can evaluate f(d) at a later time.

__bool__() bool

bool

A Lazy becomes truthy when evaluated.

returns:

True when evaluated.

returns:

False when not evaluated.

eval() None

evaluate

Evaluate function with its argument.

  • cache result or exception if pure is True

  • reevaluate if pure is False

got_result() MayBe[bool]

Got valid result

Check if a valid result was obtained.

returns:

MayBe() if not yet evaluated.

returns:

MayBe(True) if a result was gotten.

returns:

MayBe(False) if an exception was thrown.

got_exception() MayBe[bool]

Exception raised

Check if exception thrown.

returns:

MayBe() if not yet evaluated.

returns:

MayBe(True) if an exception was thrown.

returns:

MayBe(False) if exception not thrown.

get(alt: R | None = None) R

get

Get result only if evaluated and no exceptions occurred, otherwise return an alternate value.

param alt:

Optional alternate value to return if Lazy is not evaluated or exceptional.

returns:

The successfully evaluated result or alt if given.

raises ValueError:

if Lazy not evaluated, or exceptional and alt not given.

get_result() MayBe

get result

Get result only if evaluated and not exceptional.

returns:

The result wrapped in a maybe monad.

get_exception() MayBe[Exception]

get exception

Get result only if evaluate and exceptional.

returns:

The exception thrown 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 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