Task¶
from pymonet.task import Task
def resolvable_fn(reject, resolve):
print('resolve side effect')
resolve(42)
def rejectable_fn(reject, resolve):
print('reject side effect')
reject(0)
resolvable_task = Task.of(resolvable_fn)
rejectable_task = Task.of(rejectable_fn)
# map method will be applied only on resolvable tasks during calling bind method
resolvable_task.map(lambda value: value + 1) # Task<() -> 43>
rejectable_task.map(lambda value: value + 1) # Task<() -> 0>
# bind method will be applied only on resolvable tasks. bind also will call stored function
def mapper(value):
print('mapper side effect ' + value)
return value + 1
resolvable_task.bind(mapper)
# resolve side effect
# mapper side effect 42
rejectable_task.bind(mapper)
# reject side effect
-
class
pymonet.task.
Task
(fork)¶ Task are data-type for handle execution of functions (in lazy way) transform results of this function and handle errors.
-
__init__
(fork)¶ - Parameters
fork (Function(reject, resolve) -> Any) – function to call during fork
-
bind
(fn)¶ Take function, store it and call with Task value during calling fork function. Return result of called.
- Parameters
fn (Function(value) -> Task[reject, mapped_value]) – mapper function
- Returns
new Task with mapper resolve attribute
- Return type
Task[reject, mapped_value]
-
map
(fn)¶ Take function, store it and call with Task value during calling fork function. Return new Task with result of called.
- Parameters
fn (Function(value) -> B) – mapper function
- Returns
new Task with mapped resolve attribute
- Return type
Task[Function(resolve, reject -> A | B]
-