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 Callable f with its argument at a later time.

  • first argument f taking values of type D to values of type R

  • second argument arg: D is the argument to be passed to f

    • where the type D is the tuple type of the argument types to f

  • function is evaluated when the eval method is called

  • result is cached unless pure is set to False

  • 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 function

  • next positional arguments are the arguments to be applied later to f

    • f is reevaluated whenever eval method of the returned Lazy is called

  • any kwargs passed are ignored

    • if f needs them, then wrap f 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 function

  • next positional arguments are the arguments to be applied later to f

    • f is evaluated when eval method of the returned Lazy is called

    • f is evaluated only once with results cached

  • any kwargs passed are ignored

    • if f needs them then wrap f in another function

Parameters:
  • f (Callable[[ParamSpec(P)], TypeVar(R)])

  • args (ParamSpecArgs)

  • kwargs (ParamSpecKwargs)

Return type:

Lazy[tuple[Any, ...], TypeVar(R)]