Lazy

example_try.py
from pymonet.lazy import Lazy

def fn():
    print('fn call')
    return 42

def mapper(value):
    print('mapper side effect of ' + value)
    return value + 1

def side_effect(value):
    print('side effect of ' + value)

# Lazy instances memoize output of constructor function
lazy = Lazy(fn)
mapped_lazy = lazy.map(mapper)
mapped_lazy.fold(side_effect)
# fn call
# mapper side effect of 42
# side effect of 42
lazy = Lazy(fn)
value1 = lazy.get()
# fn call
value2 = lazy.get()
print(value1, value2)
# 42, 42
class pymonet.lazy.Lazy(constructor_fn: Callable[[T], U])

Data type for storage any type of function. This function (and all his mappers) will be called only during calling fold method

__eq__(other: object) → bool

Two Lazy are equals where both are evaluated both have the same value and constructor functions.

__init__(constructor_fn: Callable[[T], U]) → None
Parameters

constructor_fn (Function() -> A) – function to call during fold method call

bind(fn: Callable[[U], pymonet.lazy.Lazy[~ U, ~ W][U, W]]) → pymonet.lazy.Lazy[~T, ~W][T, W]

Take function and call constructor function passing returned value to fn function.

It’s only way to call function store in Lazy :param fn: Function(constructor_fn) -> B :returns: result od folder function :rtype: B

get(*args)

Evaluate function and memoize her output or return memoized value when function was evaluated.

Returns

result of function in Lazy

Return type

A

map(mapper: Callable[[U], W]) → pymonet.lazy.Lazy[~T, ~W][T, W]

Take function Function(A) -> B and returns new Lazy with mapped result of Lazy constructor function. Both mapper end constructor will be called only during calling fold method.

Parameters

mapper (Function(A) -> B) – mapper function

Returns

Lazy with mapped value

Return type

Lazy[Function() -> B)]

classmethod of(value: U) → pymonet.lazy.Lazy[~T, ~U][T, U]

Returns Lazy with function returning argument.

Parameters

value (Any) – value to return by Lazy constructor_fn

Returns

Lazy with function returning argument

Return type

Lazy[Function() -> A]

to_validation(*args)

Transform Lazy into successful Validation with constructor_fn result.

Returns

successfull Validation monad with previous value

Return type

Validation[A, []]