Monadic functions for trcks.AwaitableResult
.
Provides utilities for functional composition of
trcks.AwaitableResult
-returning functions.
Example
>>> import asyncio >>> import math >>> from trcks import Result >>> from trcks.fp.composition import pipe >>> from trcks.fp.monads import awaitable_result as ar >>> async def read_from_disk() -> Result[str, float]: ... await asyncio.sleep(0.001) ... return "failure", "not found" ... >>> def get_square_root(x: float) -> Result[str, float]: ... if x < 0: ... return "failure", "negative value" ... return "success", math.sqrt(x) ... >>> async def write_to_disk(output: float) -> None: ... await asyncio.sleep(0.001) ... print(f"Wrote '{output}' to disk.") ... >>> async def main() -> Result[str, None]: ... awaitable_result = read_from_disk() ... return await pipe( ... ( ... awaitable_result, ... ar.map_success_to_result(get_square_root), ... ar.map_success_to_awaitable(write_to_disk), ... ) ... ) ... >>> asyncio.run(main()) ('failure', 'not found')
Function | construct |
Create an AwaitableFailure object from a value. |
Function | construct |
Create an AwaitableFailure object from an Awaitable object. |
Function | construct |
Create an AwaitableResult object from a Result object. |
Function | construct |
Create an AwaitableSuccess object from a value. |
Function | construct |
Create an AwaitableSuccess object from an Awaitable object. |
Function | map |
Create function that maps AwaitableFailure to AwaitableFailure values. |
Function | map |
Create function that maps AwaitableFailure to AwaitableFailure values. |
Function | map |
Create function that maps AwaitableFailure values to AwaitableResult values. |
Function | map |
Create function that maps AwaitableFailure values to AwaitableResult values. |
Function | map |
Creates function that maps AwaitableSuccess to AwaitableSuccess values. |
Function | map |
Creates function that maps AwaitableSuccess to AwaitableSuccess values. |
Function | map |
Create function that maps AwaitableSuccess values to AwaitableResult values. |
Function | map |
Creates function that maps AwaitableSuccess values to AwaitableResult values. |
Async Function | to |
Turn an AwaitableResult into a collections.abc.Coroutine . |
Constant | _F |
Undocumented |
Constant | _F1 |
Undocumented |
Constant | _F2 |
Undocumented |
Constant | _S |
Undocumented |
Constant | _S1 |
Undocumented |
Constant | _S2 |
Undocumented |
Create an AwaitableFailure
object from a value.
Example
>>> import asyncio >>> from collections.abc import Awaitable >>> from trcks.fp.monads import awaitable_result as ar >>> a_rslt = ar.construct_failure("not found") >>> isinstance(a_rslt, Awaitable) True >>> asyncio.run(ar.to_coroutine_result(a_rslt)) ('failure', 'not found')
Parameters | |
value:_F | Value to be wrapped in an AwaitableFailure object. |
Returns | |
AwaitableFailure[ | A new AwaitableFailure instance containing the given value. |
Create an AwaitableFailure
object from an Awaitable
object.
Example
>>> import asyncio >>> from collections.abc import Awaitable >>> from http import HTTPStatus >>> from trcks.fp.monads import awaitable_result as ar >>> async def get_status() -> HTTPStatus: ... await asyncio.sleep(0.001) ... return HTTPStatus.NOT_FOUND ... >>> awaitable_status = get_status() >>> isinstance(awaitable_status, Awaitable) True >>> a_rslt = ar.construct_failure_from_awaitable(awaitable_status) >>> asyncio.run(ar.to_coroutine_result(a_rslt)) ('failure', <HTTPStatus.NOT_FOUND: 404>)
Parameters | |
awtbl:Awaitable[ | Awaitable object to be wrapped in an AwaitableFailure object. |
Returns | |
AwaitableFailure[ | A new AwaitableFailure instance containing
the value of the given Awaitable object. |
Create an AwaitableResult
object from a Result
object.
Example
>>> import asyncio >>> from collections.abc import Awaitable >>> from trcks.fp.monads import awaitable_result as ar >>> a_rslt = ar.construct_from_result(("failure", "not found")) >>> isinstance(a_rslt, Awaitable) True >>> asyncio.run(ar.to_coroutine_result(a_rslt)) ('failure', 'not found')
Parameters | |
rslt:Result[ | Result object to be wrapped in an AwaitableResult object. |
Returns | |
AwaitableResult[ | A new AwaitableResult instance containing
the value of the given Result object. |
Create an AwaitableSuccess
object from a value.
Example
>>> import asyncio >>> from collections.abc import Awaitable >>> from trcks.fp.monads import awaitable_result as ar >>> a_rslt = ar.construct_success(42) >>> isinstance(a_rslt, Awaitable) True >>> asyncio.run(ar.to_coroutine_result(a_rslt)) ('success', 42)
Parameters | |
value:_S | Value to be wrapped in an AwaitableSuccess object. |
Returns | |
AwaitableSuccess[ | A new AwaitableSuccess instance containing the given value. |
Create an AwaitableSuccess
object from an Awaitable
object.
Example
>>> import asyncio >>> from collections.abc import Awaitable >>> from trcks.fp.monads import awaitable_result as ar >>> async def read_from_disk() -> int: ... await asyncio.sleep(0.001) ... return "Hello, world!" ... >>> awaitable_str = read_from_disk() >>> isinstance(awaitable_str, Awaitable) True >>> a_rslt = ar.construct_success_from_awaitable(awaitable_str) >>> asyncio.run(ar.to_coroutine_result(a_rslt)) ('success', 'Hello, world!')
Parameters | |
awtbl:Awaitable[ | Awaitable object to be wrapped in an AwaitableSuccess object. |
Returns | |
AwaitableSuccess[ | A new AwaitableSuccess instance containing
the value of the given Awaitable object. |
Callable[ [ _F1], _F2]
) -> Callable[ [ AwaitableResult[ _F1, _S1]], AwaitableResult[ _F2, _S1]]
:
(source)
¶
Create function that maps AwaitableFailure
to AwaitableFailure
values.
AwaitableSuccess
values are left unchanged.
Example
>>> import asyncio >>> from trcks import AwaitableResult >>> from trcks.fp.monads import awaitable_result as ar >>> add_prefix_to_failure = ar.map_failure(lambda s: f"Prefix: {s}") >>> a_rslt_1: AwaitableResult[str, float] = add_prefix_to_failure( ... ar.construct_failure("negative value") ... ) >>> asyncio.run(ar.to_coroutine_result(a_rslt_1)) ('failure', 'Prefix: negative value') >>> a_rslt_2: AwaitableResult[str, float] = add_prefix_to_failure( ... ar.construct_success(25.0) ... ) >>> asyncio.run(ar.to_coroutine_result(a_rslt_2)) ('success', 25.0)
Parameters | |
f:Callable[ | Function to apply to the AwaitableFailure values. |
Returns | |
Callable[ | Maps AwaitableFailure values to AwaitableFailure values
according to the given function and
leaves AwaitableSuccess values unchanged. |
Callable[ [ _F1], Awaitable[ _F2]]
) -> Callable[ [ AwaitableResult[ _F1, _S1]], AwaitableResult[ _F2, _S1]]
:
(source)
¶
Create function that maps AwaitableFailure
to AwaitableFailure
values.
AwaitableSuccess
values are left unchanged.
Example
>>> import asyncio >>> from trcks import AwaitableResult >>> from trcks.fp.monads import awaitable_result as ar >>> async def slowly_add_prefix(s: str) -> str: ... await asyncio.sleep(0.001) ... return f"Prefix: {s}" ... >>> slowly_add_prefix_to_failure = ar.map_failure_to_awaitable( ... slowly_add_prefix ... ) >>> a_rslt_1: AwaitableResult[str, float] = slowly_add_prefix_to_failure( ... ar.construct_failure("negative value") ... ) >>> asyncio.run(ar.to_coroutine_result(a_rslt_1)) ('failure', 'Prefix: negative value') >>> a_rslt_2: AwaitableResult[str, float] = slowly_add_prefix_to_failure( ... ar.construct_success(25.0) ... ) >>> asyncio.run(ar.to_coroutine_result(a_rslt_2)) ('success', 25.0)
Parameters | |
f:Callable[ | Asynchronous function to apply to the AwaitableFailure values. |
Returns | |
Callable[ | Maps AwaitableFailure values to AwaitableFailure values
according to the given asynchronous function and
leaves AwaitableSuccess values unchanged. |
Callable[ [ _F1], AwaitableResult[ _F2, _S2]]
) -> Callable[ [ AwaitableResult[ _F1, _S1]], AwaitableResult[ _F2, _S1 | _S2]]
:
(source)
¶
Create function that maps AwaitableFailure
values to AwaitableResult
values.
AwaitableSuccess
values are left unchanged.
Example
>>> import asyncio >>> from trcks import Result >>> from trcks.fp.monads import awaitable_result as ar >>> async def _slowly_replace_not_found(s: str) -> Result[str, float]: ... await asyncio.sleep(0.001) ... if s == "not found": ... return "success", 0.0 ... return "failure", s ... >>> slowly_replace_not_found = ar.map_failure_to_awaitable_result( ... _slowly_replace_not_found ... ) >>> >>> a_rslt_1 = slowly_replace_not_found(ar.construct_failure("not found")) >>> asyncio.run(ar.to_coroutine_result(a_rslt_1)) ('success', 0.0) >>> a_rslt_2 = slowly_replace_not_found(ar.construct_failure("other failure")) >>> asyncio.run(ar.to_coroutine_result(a_rslt_2)) ('failure', 'other failure') >>> a_rslt_3 = slowly_replace_not_found(ar.construct_success(25.0)) >>> asyncio.run(ar.to_coroutine_result(a_rslt_3)) ('success', 25.0)
Parameters | |
f:Callable[ | Asynchronous function to apply to the AwaitableFailure values. |
Returns | |
Callable[ | Maps AwaitableFailure values
to AwaitableFailure and AwaitableSuccess values
according to the given asynchronous function and
leaves AwaitableSuccess values unchanged. |
Callable[ [ _F1], Result[ _F2, _S2]]
) -> Callable[ [ AwaitableResult[ _F1, _S1]], AwaitableResult[ _F2, _S1 | _S2]]
:
(source)
¶
Create function that maps AwaitableFailure
values to AwaitableResult
values.
AwaitableSuccess
values are left unchanged.
Example
>>> import asyncio >>> from trcks import AwaitableResult >>> from trcks.fp.monads import awaitable_result as ar >>> replace_not_found_by_default_value = ar.map_failure_to_result( ... lambda s: ("success", 0.0) if s == "not found" else ("failure", s) ... ) >>> a_rslt_1: AwaitableResult[str, float] = replace_not_found_by_default_value( ... ar.construct_failure("not found") ... ) >>> asyncio.run(ar.to_coroutine_result(a_rslt_1)) ('success', 0.0) >>> a_rslt_2: AwaitableResult[str, float] = replace_not_found_by_default_value( ... ar.construct_failure("other failure") ... ) >>> asyncio.run(ar.to_coroutine_result(a_rslt_2)) ('failure', 'other failure') >>> a_rslt_3: AwaitableResult[str, float] = replace_not_found_by_default_value( ... ar.construct_success(25.0) ... ) >>> asyncio.run(ar.to_coroutine_result(a_rslt_3)) ('success', 25.0)
Parameters | |
f:Callable[ | Function to apply to the AwaitableFailure values. |
Returns | |
Callable[ | Maps AwaitableFailure values to AwaitableResult values
according to the given function and
leaves AwaitableSuccess values unchanged. |
Callable[ [ _S1], _S2]
) -> Callable[ [ AwaitableResult[ _F1, _S1]], AwaitableResult[ _F1, _S2]]
:
(source)
¶
Creates function that maps AwaitableSuccess
to AwaitableSuccess
values.
AwaitableFailure
values are left unchanged.
Example
>>> import asyncio >>> from trcks import AwaitableResult >>> from trcks.fp.monads import awaitable_result as ar >>> def increase(n: int) -> int: ... return n + 1 ... >>> increase_success = ar.map_success(increase) >>> a_rslt_1: AwaitableResult[str, int] = increase_success( ... ar.construct_failure("not found") ... ) >>> asyncio.run(ar.to_coroutine_result(a_rslt_1)) ('failure', 'not found') >>> a_rslt_2: AwaitableResult[str, int] = increase_success( ... ar.construct_success(42) ... ) >>> asyncio.run(ar.to_coroutine_result(a_rslt_2)) ('success', 43)
Parameters | |
f:Callable[ | Function to apply to the AwaitableSuccess values. |
Returns | |
Callable[ | Leaves AwaitableFailure values unchanged and
maps AwaitableSuccess values to new AwaitableSuccess values
according to the given function. |
Callable[ [ _S1], Awaitable[ _S2]]
) -> Callable[ [ AwaitableResult[ _F1, _S1]], AwaitableResult[ _F1, _S2]]
:
(source)
¶
Creates function that maps AwaitableSuccess
to AwaitableSuccess
values.
AwaitableFailure
values are left unchanged.
Example
>>> import asyncio >>> >>> from trcks import AwaitableResult >>> from trcks.fp.monads import awaitable_result as ar >>> >>> >>> async def increment_slowly(n: int) -> int: ... return n + 1 ... >>> increase_success = ar.map_success_to_awaitable(increment_slowly) >>> >>> a_rslt_1: AwaitableResult[str, int] = increase_success( ... ar.construct_failure("not found") ... ) >>> asyncio.run(ar.to_coroutine_result(a_rslt_1)) ('failure', 'not found') >>> >>> a_rslt_2: AwaitableResult[str, int] = increase_success( ... ar.construct_success(42) ... ) >>> asyncio.run(ar.to_coroutine_result(a_rslt_2)) ('success', 43)
Parameters | |
f:Callable[ | Asynchronous function to apply to the AwaitableSuccess values. |
Returns | |
Callable[ | Leaves AwaitableFailure values unchanged and
maps AwaitableSuccess values to new AwaitableSuccess values
according to the given function. |
Callable[ [ _S1], AwaitableResult[ _F2, _S2]]
) -> Callable[ [ AwaitableResult[ _F1, _S1]], AwaitableResult[ _F1 | _F2, _S2]]
:
(source)
¶
Create function that maps AwaitableSuccess
values to AwaitableResult
values.
AwaitableFailure
values are left unchanged.
Example
>>> import asyncio >>> import math >>> from trcks import AwaitableResult, Result >>> from trcks.fp.monads import awaitable_result as ar >>> async def _get_square_root_slowly(x: float) -> Result[str, float]: ... await asyncio.sleep(0.001) ... if x < 0: ... return "failure", "negative value" ... return "success", math.sqrt(x) ... >>> get_square_root_slowly = ar.map_success_to_awaitable_result( ... _get_square_root_slowly ... ) >>> a_rslt_1: AwaitableResult[str, float] = get_square_root_slowly( ... ar.construct_failure("not found") ... ) >>> asyncio.run(ar.to_coroutine_result(a_rslt_1)) ('failure', 'not found') >>> a_rslt_2: AwaitableResult[str, float] = get_square_root_slowly( ... ar.construct_success(25.0) ... ) >>> asyncio.run(ar.to_coroutine_result(a_rslt_2)) ('success', 5.0)
Parameters | |
f:Callable[ | Asynchronous function to apply to the AwaitableSuccess values. |
Returns | |
Callable[ | Leaves AwaitableFailure values unchanged and
maps AwaitableSuccess values
to AwaitableFailure and AwaitableSuccess values
according to the given asynchronous function. |
Callable[ [ _S1], Result[ _F2, _S2]]
) -> Callable[ [ AwaitableResult[ _F1, _S1]], AwaitableResult[ _F1 | _F2, _S2]]
:
(source)
¶
Creates function that maps AwaitableSuccess
values to AwaitableResult
values.
AwaitableFailure
values are left unchanged.
Example
>>> import asyncio >>> import math >>> from trcks import Result >>> from trcks.fp.monads import awaitable_result as ar >>> def _get_square_root_slowly(x: float) -> Result[str, float]: ... if x < 0: ... return "failure", "negative value" ... return "success", math.sqrt(x) ... >>> get_square_root_slowly = ar.map_success_to_result( ... _get_square_root_slowly ... ) >>> a_rslt_1 = get_square_root_slowly( ... ar.construct_failure("not found") ... ) >>> asyncio.run(ar.to_coroutine_result(a_rslt_1)) ('failure', 'not found') >>> a_rslt_2 = get_square_root_slowly( ... ar.construct_success(25.0) ... ) >>> asyncio.run(ar.to_coroutine_result(a_rslt_2)) ('success', 5.0)
Parameters | |
f:Callable[ | Function to apply to the AwaitableSuccess values. |
Returns | |
Callable[ | Leaves AwaitableFailure values unchanged and
maps AwaitableSuccess values
to AwaitableFailure and AwaitableSuccess values
according to the given function. |
Turn an AwaitableResult
into a collections.abc.Coroutine
.
This is useful for functions that expect a coroutine (e.g. asyncio.run
).
Example
>>> import asyncio >>> from trcks import Result >>> from trcks.fp.monads import awaitable_result as ar >>> asyncio.set_event_loop(asyncio.new_event_loop()) >>> future = asyncio.Future[Result[str, int]]() >>> future.set_result(("success", 42)) >>> future <Future finished result=('success', 42)> >>> coro = ar.to_coroutine_result(future) >>> coro <coroutine object to_coroutine_result at ...> >>> asyncio.run(coro) ('success', 42)
Parameters | |
aAwaitableResult[ | The AwaitableResult to be transformed into a collections.abc.Coroutine . |
Returns | |
Result[ | The given AwaitableResult transformed into a collections.abc.Coroutine . |