Skip to content

trcks.fp.monads.awaitable_result

Monadic functions for trcks.AwaitableResult.

Provides utilities for functional composition of asynchronous trcks.Result-returning functions.

Examples:

>>> 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, float]:
...     a_rslt = read_from_disk()
...     return await pipe(
...         a_rslt,
...         ar.map_success_to_result(get_square_root),
...         ar.tap_success_to_awaitable(write_to_disk),
...     )
...
>>> asyncio.run(main())
('failure', 'not found')

construct_failure(value)

Create a trcks.AwaitableFailure object from a value.

Parameters:

Returns:

Examples:

>>> 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')
Source code in src/trcks/fp/_monads/awaitable_result.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
def construct_failure(value: _F, /) -> AwaitableFailure[_F]:
    """Create a [trcks.AwaitableFailure][] object from a value.

    Args:
        value: Value to be wrapped in a [trcks.AwaitableFailure][] object.

    Returns:
        A new [trcks.AwaitableFailure][] instance containing the given value.

    Examples:
        >>> 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')
    """
    return a.construct(r.construct_failure(value))

construct_failure_from_awaitable(awtbl)

Create a trcks.AwaitableFailure object from a collections.abc.Awaitable object.

Parameters:

Returns:

Examples:

>>> 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>)
Source code in src/trcks/fp/_monads/awaitable_result.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def construct_failure_from_awaitable(awtbl: Awaitable[_F], /) -> AwaitableFailure[_F]:
    """Create a [trcks.AwaitableFailure][] object
    from a [collections.abc.Awaitable][] object.

    Args:
        awtbl: [collections.abc.Awaitable][] object to be wrapped
            in a [trcks.AwaitableFailure][] object.

    Returns:
        A new [trcks.AwaitableFailure][] instance containing
            the value of the given [collections.abc.Awaitable][] object.

    Examples:
        >>> 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>)
    """
    return a.map_(r.construct_failure)(awtbl)

construct_from_result(rslt)

Create a trcks.AwaitableResult object from a trcks.Result object.

Parameters:

Returns:

Examples:

>>> 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')
Source code in src/trcks/fp/_monads/awaitable_result.py
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
def construct_from_result(rslt: Result[_F, _S], /) -> AwaitableResult[_F, _S]:
    """Create a [trcks.AwaitableResult][] object from a [trcks.Result][] object.

    Args:
        rslt: [trcks.Result][] object to be wrapped
            in a [trcks.AwaitableResult][] object.

    Returns:
        A new [trcks.AwaitableResult][] instance containing
            the value of the given [trcks.Result][] object.

    Examples:
        >>> 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')
    """
    return a.construct(rslt)

construct_success(value)

Create a trcks.AwaitableSuccess object from a value.

Parameters:

Returns:

Examples:

>>> 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)
Source code in src/trcks/fp/_monads/awaitable_result.py
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
def construct_success(value: _S, /) -> AwaitableSuccess[_S]:
    """Create a [trcks.AwaitableSuccess][] object from a value.

    Args:
        value: Value to be wrapped in a [trcks.AwaitableSuccess][] object.

    Returns:
        A new [trcks.AwaitableSuccess][] instance containing the given value.

    Examples:
        >>> 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)
    """
    return a.construct(r.construct_success(value))

construct_success_from_awaitable(awtbl)

Create a trcks.AwaitableSuccess object from a collections.abc.Awaitable object.

Parameters:

Returns:

Examples:

>>> import asyncio
>>> from collections.abc import Awaitable
>>> from trcks.fp.monads import awaitable_result as ar
>>> async def read_from_disk() -> str:
...     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!')
Source code in src/trcks/fp/_monads/awaitable_result.py
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
def construct_success_from_awaitable(awtbl: Awaitable[_S], /) -> AwaitableSuccess[_S]:
    """Create a [trcks.AwaitableSuccess][] object
    from a [collections.abc.Awaitable][] object.

    Args:
        awtbl: [collections.abc.Awaitable][] object to be wrapped
            in a [trcks.AwaitableSuccess][] object.

    Returns:
        A new [trcks.AwaitableSuccess][] instance containing
            the value of the given [collections.abc.Awaitable][] object.

    Examples:
        >>> import asyncio
        >>> from collections.abc import Awaitable
        >>> from trcks.fp.monads import awaitable_result as ar
        >>> async def read_from_disk() -> str:
        ...     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!')
    """
    return a.map_(r.construct_success)(awtbl)

map_failure(callable_, /, *args, **kwargs)

Create function that maps trcks.AwaitableFailure to trcks.AwaitableFailure values.

trcks.AwaitableSuccess values are left unchanged.

Parameters:

  • callable_ (Callable[Concatenate[_F1, _P], _F2]) –

    Synchronous function to apply to trcks.AwaitableFailure values.

  • *args (args, default: () ) –

    Positional arguments to be passed to callable_.

  • **kwargs (kwargs, default: {} ) –

    Keyword arguments to be passed to callable_.

Returns:

Examples:

>>> 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)
Source code in src/trcks/fp/_monads/awaitable_result.py
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
def map_failure(
    callable_: Callable[Concatenate[_F1, _P], _F2],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[AwaitableResult[_F1, _S1]], AwaitableResult[_F2, _S1]]:
    """Create function that maps [trcks.AwaitableFailure][]
    to [trcks.AwaitableFailure][] values.

    [trcks.AwaitableSuccess][] values are left unchanged.

    Args:
        callable_: Synchronous function to apply to [trcks.AwaitableFailure][] values.
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Maps [trcks.AwaitableFailure][] values to [trcks.AwaitableFailure][] values
            according to the given function and
            leaves [trcks.AwaitableSuccess][] values unchanged.

    Examples:
        >>> 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)
    """
    return a.map_(r.map_failure(callable_, *args, **kwargs))

map_failure_to_awaitable(callable_, /, *args, **kwargs)

Create function that maps trcks.AwaitableFailure to trcks.AwaitableFailure values.

trcks.AwaitableSuccess values are left unchanged.

Parameters:

  • callable_ (Callable[Concatenate[_F1, _P], Awaitable[_F2]]) –

    Asynchronous function to apply to trcks.AwaitableFailure values.

  • *args (args, default: () ) –

    Positional arguments to be passed to callable_.

  • **kwargs (kwargs, default: {} ) –

    Keyword arguments to be passed to callable_.

Returns:

Examples:

>>> 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)
Source code in src/trcks/fp/_monads/awaitable_result.py
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
def map_failure_to_awaitable(
    callable_: Callable[Concatenate[_F1, _P], Awaitable[_F2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[AwaitableResult[_F1, _S1]], AwaitableResult[_F2, _S1]]:
    """Create function that maps [trcks.AwaitableFailure][]
    to [trcks.AwaitableFailure][] values.

    [trcks.AwaitableSuccess][] values are left unchanged.

    Args:
        callable_: Asynchronous function to apply to [trcks.AwaitableFailure][] values.
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Maps [trcks.AwaitableFailure][] values to [trcks.AwaitableFailure][] values
            according to the given asynchronous function and
            leaves [trcks.AwaitableSuccess][] values unchanged.

    Examples:
        >>> 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)
    """
    return map_failure_to_awaitable_result(
        compose(callable_, construct_failure_from_awaitable), *args, **kwargs
    )

map_failure_to_awaitable_result(callable_, /, *args, **kwargs)

Create function that maps trcks.AwaitableFailure values to trcks.AwaitableResult values.

trcks.AwaitableSuccess values are left unchanged.

Parameters:

  • callable_ (Callable[Concatenate[_F1, _P], AwaitableResult[_F2, _S2]]) –

    Asynchronous function to apply to trcks.AwaitableFailure values.

  • *args (args, default: () ) –

    Positional arguments to be passed to callable_.

  • **kwargs (kwargs, default: {} ) –

    Keyword arguments to be passed to callable_.

Returns:

Examples:

>>> 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)
Source code in src/trcks/fp/_monads/awaitable_result.py
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
def map_failure_to_awaitable_result(
    callable_: Callable[Concatenate[_F1, _P], AwaitableResult[_F2, _S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[AwaitableResult[_F1, _S1]], AwaitableResult[_F2, _S1 | _S2]]:
    """Create function that maps [trcks.AwaitableFailure][] values
    to [trcks.AwaitableResult][] values.

    [trcks.AwaitableSuccess][] values are left unchanged.

    Args:
        callable_: Asynchronous function to apply to [trcks.AwaitableFailure][] values.
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Maps [trcks.AwaitableFailure][] values
            to [trcks.AwaitableFailure][] and [trcks.AwaitableSuccess][] values
            according to the given asynchronous function and
            leaves [trcks.AwaitableSuccess][] values unchanged.

    Examples:
        >>> 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)
    """

    async def partially_mapped_callable(
        rslt: Result[_F1, _S1],
    ) -> Result[_F2, _S1 | _S2]:
        match rslt:
            case ("failure", value):
                return await callable_(value, *args, **kwargs)
            case ("success", _):
                return rslt
            case _:  # pragma: no cover
                assert_type(rslt, Never)  # type: ignore[unreachable]  # pyright: ignore[reportUnreachable]
                msg = f"{type(rslt).__name__!r} is not a valid Result"
                raise TypeError(msg)

    return a.map_to_awaitable(partially_mapped_callable)

map_failure_to_awaitable_result_iterable(callable_, /, *args, **kwargs)

Create function that maps trcks.Failure values to trcks.AwaitableResultTuple values.

trcks.Success values are left unchanged.

Parameters:

  • callable_ (Callable[Concatenate[_F1, _P], AwaitableResultIterable[_F2, _S2]]) –

    Asynchronous function to apply to the trcks.Failure values.

  • *args (args, default: () ) –

    Positional arguments to be passed to callable_.

  • **kwargs (kwargs, default: {} ) –

    Keyword arguments to be passed to callable_.

Returns:

Examples:

>>> import asyncio
>>> from trcks import AwaitableResultTuple
>>> from trcks.fp.monads import awaitable_result as ar
>>> from trcks.fp.monads import awaitable_result_tuple as art
>>> async def recover(e: str) -> AwaitableResultTuple[str, float]:
...     await asyncio.sleep(0.001)
...     if e == "not found":
...         return "success", (0.0, 1.0)
...     return "failure", e
...
>>> recover_from_failure = ar.map_failure_to_awaitable_result_iterable(
...     recover
... )
>>> a_r_tpl_1 = recover_from_failure(ar.construct_failure("not found"))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
('success', (0.0, 1.0))
>>> a_r_tpl_2 = recover_from_failure(ar.construct_success(25.0))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
('success', (25.0,))
Source code in src/trcks/fp/monads/awaitable_result.py
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
def map_failure_to_awaitable_result_iterable(
    callable_: Callable[Concatenate[_F1, _P], AwaitableResultIterable[_F2, _S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[AwaitableResult[_F1, _S1]], AwaitableResultTuple[_F2, _S1 | _S2]]:
    """Create function that maps [trcks.Failure][] values
    to [trcks.AwaitableResultTuple][] values.

    [trcks.Success][] values are left unchanged.

    Args:
        callable_: Asynchronous function to apply to the [trcks.Failure][] values.
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Maps [trcks.Failure][] values to new [trcks.AwaitableResultTuple][] values
            according to the given asynchronous function and
            leaves [trcks.Success][] values unchanged.

    Examples:
        >>> import asyncio
        >>> from trcks import AwaitableResultTuple
        >>> from trcks.fp.monads import awaitable_result as ar
        >>> from trcks.fp.monads import awaitable_result_tuple as art
        >>> async def recover(e: str) -> AwaitableResultTuple[str, float]:
        ...     await asyncio.sleep(0.001)
        ...     if e == "not found":
        ...         return "success", (0.0, 1.0)
        ...     return "failure", e
        ...
        >>> recover_from_failure = ar.map_failure_to_awaitable_result_iterable(
        ...     recover
        ... )
        >>> a_r_tpl_1 = recover_from_failure(ar.construct_failure("not found"))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
        ('success', (0.0, 1.0))
        >>> a_r_tpl_2 = recover_from_failure(ar.construct_success(25.0))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
        ('success', (25.0,))
    """
    return compose(
        art.construct_from_awaitable_result,
        art.map_failure_to_awaitable_result_iterable(callable_, *args, **kwargs),
    )

map_failure_to_awaitable_result_tuple(callable_, /, *args, **kwargs)

Deprecated alias for trcks.fp.monads.awaitable_result.map_failure_to_awaitable_result_iterable.

Source code in src/trcks/fp/monads/awaitable_result.py
188
189
190
191
192
193
194
195
196
197
198
199
200
@deprecated("Use map_failure_to_awaitable_result_iterable instead")
def map_failure_to_awaitable_result_tuple(
    callable_: Callable[Concatenate[_F1, _P], AwaitableResultTuple[_F2, _S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[AwaitableResult[_F1, _S1]], AwaitableResultTuple[_F2, _S1 | _S2]]:
    """Deprecated alias for
    [trcks.fp.monads.awaitable_result.map_failure_to_awaitable_result_iterable][].
    """
    return map_failure_to_awaitable_result_iterable(
        callable_, *args, **kwargs
    )  # pragma: no cover

map_failure_to_iterable(callable_, /, *args, **kwargs)

Create function that maps trcks.Failure values to homogeneous tuples.

trcks.Success values are left unchanged.

Parameters:

  • callable_ (Callable[Concatenate[_F1, _P], Iterable[_S2]]) –

    Synchronous function to apply to the trcks.Failure values, returning an collections.abc.Iterable.

  • *args (args, default: () ) –

    Positional arguments to be passed to callable_.

  • **kwargs (kwargs, default: {} ) –

    Keyword arguments to be passed to callable_.

Returns:

Examples:

>>> import asyncio
>>> from trcks.fp.monads import awaitable_result as ar
>>> from trcks.fp.monads import awaitable_result_tuple as art
>>> def recover(e: str) -> tuple[float, ...]:
...     if e == "not found":
...         return (0.0, 1.0)
...     return ()
...
>>> recover_from_failure = ar.map_failure_to_iterable(recover)
>>> a_r_tpl_1 = recover_from_failure(ar.construct_failure("not found"))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
('success', (0.0, 1.0))
>>> a_r_tpl_2 = recover_from_failure(ar.construct_success(25.0))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
('success', (25.0,))
Source code in src/trcks/fp/monads/awaitable_result.py
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
def map_failure_to_iterable(
    callable_: Callable[Concatenate[_F1, _P], Iterable[_S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[
    [AwaitableResult[_F1, _S1]],
    Awaitable[SuccessTuple[_S1] | SuccessTuple[_S2]],
]:
    """Create function that maps [trcks.Failure][] values to homogeneous [tuple][]s.

    [trcks.Success][] values are left unchanged.

    Args:
        callable_: Synchronous function to apply to the [trcks.Failure][] values,
            returning an [collections.abc.Iterable][].
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Maps [trcks.Failure][] values to homogeneous [tuple][]s wrapped
            in [trcks.AwaitableSuccessTuple][] values
            according to the given function and
            leaves [trcks.Success][] values unchanged (wrapped as a tuple).

    Examples:
        >>> import asyncio
        >>> from trcks.fp.monads import awaitable_result as ar
        >>> from trcks.fp.monads import awaitable_result_tuple as art
        >>> def recover(e: str) -> tuple[float, ...]:
        ...     if e == "not found":
        ...         return (0.0, 1.0)
        ...     return ()
        ...
        >>> recover_from_failure = ar.map_failure_to_iterable(recover)
        >>> a_r_tpl_1 = recover_from_failure(ar.construct_failure("not found"))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
        ('success', (0.0, 1.0))
        >>> a_r_tpl_2 = recover_from_failure(ar.construct_success(25.0))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
        ('success', (25.0,))
    """
    c: tuple[
        Callable[[AwaitableResult[_F1, _S1]], AwaitableResultTuple[_F1, _S1]],
        Callable[
            [AwaitableResultTuple[_F1, _S1]],
            Awaitable[SuccessTuple[_S1] | SuccessTuple[_S2]],
        ],
    ] = (
        art.construct_from_awaitable_result,
        art.map_failure_to_iterable(callable_, *args, **kwargs),
    )
    return compose(*c)

map_failure_to_result(callable_, /, *args, **kwargs)

Create function that maps trcks.AwaitableFailure values to trcks.AwaitableResult values.

trcks.AwaitableSuccess values are left unchanged.

Parameters:

  • callable_ (Callable[Concatenate[_F1, _P], Result[_F2, _S2]]) –

    Synchronous function to apply to trcks.AwaitableFailure values.

  • *args (args, default: () ) –

    Positional arguments to be passed to callable_.

  • **kwargs (kwargs, default: {} ) –

    Keyword arguments to be passed to callable_.

Returns:

Examples:

>>> 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)
Source code in src/trcks/fp/_monads/awaitable_result.py
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
def map_failure_to_result(
    callable_: Callable[Concatenate[_F1, _P], Result[_F2, _S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[AwaitableResult[_F1, _S1]], AwaitableResult[_F2, _S1 | _S2]]:
    """Create function that maps [trcks.AwaitableFailure][] values
    to [trcks.AwaitableResult][] values.

    [trcks.AwaitableSuccess][] values are left unchanged.

    Args:
        callable_: Synchronous function to apply to [trcks.AwaitableFailure][] values.
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Maps [trcks.AwaitableFailure][] values to [trcks.AwaitableResult][] values
            according to the given function and
            leaves [trcks.AwaitableSuccess][] values unchanged.

    Examples:
        >>> 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)
    """
    return a.map_(r.map_failure_to_result(callable_, *args, **kwargs))

map_failure_to_result_iterable(callable_, /, *args, **kwargs)

Create function that maps trcks.Failure values to new trcks.ResultTuple values.

trcks.Success values are left unchanged.

Parameters:

  • callable_ (Callable[Concatenate[_F1, _P], ResultIterable[_F2, _S2]]) –

    Synchronous function to apply to the trcks.Failure values.

  • *args (args, default: () ) –

    Positional arguments to be passed to callable_.

  • **kwargs (kwargs, default: {} ) –

    Keyword arguments to be passed to callable_.

Returns:

Examples:

>>> import asyncio
>>> from trcks import ResultTuple
>>> from trcks.fp.monads import awaitable_result as ar
>>> from trcks.fp.monads import awaitable_result_tuple as art
>>> def recover(e: str) -> ResultTuple[str, float]:
...     if e == "not found":
...         return "success", (0.0, 1.0)
...     return "failure", e
...
>>> recover_from_failure = ar.map_failure_to_result_iterable(recover)
>>> a_r_tpl_1 = recover_from_failure(ar.construct_failure("not found"))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
('success', (0.0, 1.0))
>>> a_r_tpl_2 = recover_from_failure(ar.construct_success(25.0))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
('success', (25.0,))
Source code in src/trcks/fp/monads/awaitable_result.py
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
def map_failure_to_result_iterable(
    callable_: Callable[Concatenate[_F1, _P], ResultIterable[_F2, _S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[AwaitableResult[_F1, _S1]], AwaitableResultTuple[_F2, _S1 | _S2]]:
    """Create function that maps [trcks.Failure][] values
    to new [trcks.ResultTuple][] values.

    [trcks.Success][] values are left unchanged.

    Args:
        callable_: Synchronous function to apply to the [trcks.Failure][] values.
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Maps [trcks.Failure][] values to new [trcks.ResultTuple][] values
            according to the given function and
            leaves [trcks.Success][] values unchanged (wrapped as a tuple).

    Examples:
        >>> import asyncio
        >>> from trcks import ResultTuple
        >>> from trcks.fp.monads import awaitable_result as ar
        >>> from trcks.fp.monads import awaitable_result_tuple as art
        >>> def recover(e: str) -> ResultTuple[str, float]:
        ...     if e == "not found":
        ...         return "success", (0.0, 1.0)
        ...     return "failure", e
        ...
        >>> recover_from_failure = ar.map_failure_to_result_iterable(recover)
        >>> a_r_tpl_1 = recover_from_failure(ar.construct_failure("not found"))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
        ('success', (0.0, 1.0))
        >>> a_r_tpl_2 = recover_from_failure(ar.construct_success(25.0))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
        ('success', (25.0,))
    """
    return compose(
        art.construct_from_awaitable_result,
        art.map_failure_to_result_iterable(callable_, *args, **kwargs),
    )

map_failure_to_result_tuple(callable_, /, *args, **kwargs)

Deprecated alias for trcks.fp.monads.awaitable_result.map_failure_to_result_iterable.

Source code in src/trcks/fp/monads/awaitable_result.py
307
308
309
310
311
312
313
314
315
316
317
318
319
@deprecated("Use map_failure_to_result_iterable instead")
def map_failure_to_result_tuple(
    callable_: Callable[Concatenate[_F1, _P], ResultTuple[_F2, _S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[AwaitableResult[_F1, _S1]], AwaitableResultTuple[_F2, _S1 | _S2]]:
    """Deprecated alias for
    [trcks.fp.monads.awaitable_result.map_failure_to_result_iterable][].
    """
    return map_failure_to_result_iterable(
        callable_, *args, **kwargs
    )  # pragma: no cover

map_failure_to_tuple(callable_, /, *args, **kwargs)

Deprecated alias for trcks.fp.monads.awaitable_result.map_failure_to_iterable.

Source code in src/trcks/fp/monads/awaitable_result.py
322
323
324
325
326
327
328
329
330
331
332
333
334
335
@deprecated("Use map_failure_to_iterable instead")
def map_failure_to_tuple(
    callable_: Callable[Concatenate[_F1, _P], tuple[_S2, ...]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[
    [AwaitableResult[_F1, _S1]],
    Awaitable[SuccessTuple[_S1] | SuccessTuple[_S2]],
]:
    """Deprecated alias for
    [trcks.fp.monads.awaitable_result.map_failure_to_iterable][].
    """
    return map_failure_to_iterable(callable_, *args, **kwargs)  # pragma: no cover

map_success(callable_, /, *args, **kwargs)

Create function that maps trcks.AwaitableSuccess to trcks.AwaitableSuccess values.

trcks.AwaitableFailure values are left unchanged.

Parameters:

  • callable_ (Callable[Concatenate[_S1, _P], _S2]) –

    Synchronous function to apply to trcks.AwaitableSuccess values.

  • *args (args, default: () ) –

    Positional arguments to be passed to callable_.

  • **kwargs (kwargs, default: {} ) –

    Keyword arguments to be passed to callable_.

Returns:

Examples:

>>> 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)
Source code in src/trcks/fp/_monads/awaitable_result.py
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
def map_success(
    callable_: Callable[Concatenate[_S1, _P], _S2],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[AwaitableResult[_F1, _S1]], AwaitableResult[_F1, _S2]]:
    """Create function that maps [trcks.AwaitableSuccess][]
    to [trcks.AwaitableSuccess][] values.

    [trcks.AwaitableFailure][] values are left unchanged.

    Args:
        callable_: Synchronous function to apply to [trcks.AwaitableSuccess][] values.
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Leaves [trcks.AwaitableFailure][] values unchanged and
            maps [trcks.AwaitableSuccess][] values
            to new [trcks.AwaitableSuccess][] values
            according to the given function.

    Examples:
        >>> 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)
    """
    return a.map_(r.map_success(callable_, *args, **kwargs))

map_success_to_awaitable(callable_, /, *args, **kwargs)

Create function that maps trcks.AwaitableSuccess to trcks.AwaitableSuccess values.

trcks.AwaitableFailure values are left unchanged.

Parameters:

  • callable_ (Callable[Concatenate[_S1, _P], Awaitable[_S2]]) –

    Asynchronous function to apply to trcks.AwaitableSuccess values.

  • *args (args, default: () ) –

    Positional arguments to be passed to callable_.

  • **kwargs (kwargs, default: {} ) –

    Keyword arguments to be passed to callable_.

Returns:

Examples:

>>> 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)
Source code in src/trcks/fp/_monads/awaitable_result.py
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
def map_success_to_awaitable(
    callable_: Callable[Concatenate[_S1, _P], Awaitable[_S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[AwaitableResult[_F1, _S1]], AwaitableResult[_F1, _S2]]:
    """Create function that maps [trcks.AwaitableSuccess][]
    to [trcks.AwaitableSuccess][] values.

    [trcks.AwaitableFailure][] values are left unchanged.

    Args:
        callable_: Asynchronous function to apply to [trcks.AwaitableSuccess][] values.
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Leaves [trcks.AwaitableFailure][] values unchanged and
            maps [trcks.AwaitableSuccess][] values
            to new [trcks.AwaitableSuccess][] values
            according to the given function.

    Examples:
        >>> 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)
    """
    return map_success_to_awaitable_result(
        compose(callable_, construct_success_from_awaitable), *args, **kwargs
    )

map_success_to_awaitable_result(callable_, /, *args, **kwargs)

Create function that maps trcks.AwaitableSuccess values to trcks.AwaitableResult values.

trcks.AwaitableFailure values are left unchanged.

Parameters:

  • callable_ (Callable[Concatenate[_S1, _P], AwaitableResult[_F2, _S2]]) –

    Asynchronous function to apply to trcks.AwaitableSuccess values.

  • *args (args, default: () ) –

    Positional arguments to be passed to callable_.

  • **kwargs (kwargs, default: {} ) –

    Keyword arguments to be passed to callable_.

Returns:

Examples:

>>> 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)
Source code in src/trcks/fp/_monads/awaitable_result.py
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
def map_success_to_awaitable_result(
    callable_: Callable[Concatenate[_S1, _P], AwaitableResult[_F2, _S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[AwaitableResult[_F1, _S1]], AwaitableResult[_F1 | _F2, _S2]]:
    """Create function that maps [trcks.AwaitableSuccess][] values
    to [trcks.AwaitableResult][] values.

    [trcks.AwaitableFailure][] values are left unchanged.

    Args:
        callable_: Asynchronous function to apply to [trcks.AwaitableSuccess][] values.
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Leaves [trcks.AwaitableFailure][] values unchanged and
            maps [trcks.AwaitableSuccess][] values
            to [trcks.AwaitableFailure][] and [trcks.AwaitableSuccess][] values
            according to the given asynchronous function.

    Examples:
        >>> 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)
    """

    async def partially_mapped_callable(
        rslt: Result[_F1, _S1],
    ) -> Result[_F1 | _F2, _S2]:
        match rslt:
            case ("failure", _):
                return rslt
            case ("success", value):
                return await callable_(value, *args, **kwargs)
            case _:  # pragma: no cover
                assert_type(rslt, Never)  # type: ignore[unreachable]  # pyright: ignore[reportUnreachable]
                msg = f"{type(rslt).__name__!r} is not a valid Result"
                raise TypeError(msg)

    return a.map_to_awaitable(partially_mapped_callable)

map_success_to_awaitable_result_iterable(callable_, /, *args, **kwargs)

Create function that maps trcks.Success values to trcks.AwaitableResultTuple values.

trcks.Failure values are left unchanged.

Parameters:

  • callable_ (Callable[Concatenate[_S1, _P], AwaitableResultIterable[_F2, _S2]]) –

    Asynchronous function to apply to the trcks.Success values.

  • *args (args, default: () ) –

    Positional arguments to be passed to callable_.

  • **kwargs (kwargs, default: {} ) –

    Keyword arguments to be passed to callable_.

Returns:

Examples:

>>> import asyncio
>>> from trcks import AwaitableResultTuple
>>> from trcks.fp.monads import awaitable_result as ar
>>> from trcks.fp.monads import awaitable_result_tuple as art
>>> async def slowly_expand(x: float) -> AwaitableResultTuple[str, float]:
...     await asyncio.sleep(0.001)
...     if x < 0:
...         return "failure", "negative"
...     return "success", (x, x * 2)
...
>>> expand_success = ar.map_success_to_awaitable_result_iterable(
...     slowly_expand
... )
>>> a_r_tpl_1 = expand_success(ar.construct_failure("not found"))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
('failure', 'not found')
>>> a_r_tpl_2 = expand_success(ar.construct_success(5.0))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
('success', (5.0, 10.0))
Source code in src/trcks/fp/monads/awaitable_result.py
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
def map_success_to_awaitable_result_iterable(
    callable_: Callable[Concatenate[_S1, _P], AwaitableResultIterable[_F2, _S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[AwaitableResult[_F1, _S1]], AwaitableResultTuple[_F1 | _F2, _S2]]:
    """Create function that maps [trcks.Success][] values
    to [trcks.AwaitableResultTuple][] values.

    [trcks.Failure][] values are left unchanged.

    Args:
        callable_: Asynchronous function to apply to the [trcks.Success][] values.
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Leaves [trcks.Failure][] values unchanged and
            maps [trcks.Success][] values to new [trcks.AwaitableResultTuple][]
            values according to the given asynchronous function.

    Examples:
        >>> import asyncio
        >>> from trcks import AwaitableResultTuple
        >>> from trcks.fp.monads import awaitable_result as ar
        >>> from trcks.fp.monads import awaitable_result_tuple as art
        >>> async def slowly_expand(x: float) -> AwaitableResultTuple[str, float]:
        ...     await asyncio.sleep(0.001)
        ...     if x < 0:
        ...         return "failure", "negative"
        ...     return "success", (x, x * 2)
        ...
        >>> expand_success = ar.map_success_to_awaitable_result_iterable(
        ...     slowly_expand
        ... )
        >>> a_r_tpl_1 = expand_success(ar.construct_failure("not found"))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
        ('failure', 'not found')
        >>> a_r_tpl_2 = expand_success(ar.construct_success(5.0))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
        ('success', (5.0, 10.0))
    """
    return compose(
        art.construct_from_awaitable_result,
        art.map_successes_to_awaitable_result_iterable(callable_, *args, **kwargs),
    )

map_success_to_awaitable_result_tuple(callable_, /, *args, **kwargs)

Deprecated alias for trcks.fp.monads.awaitable_result.map_success_to_awaitable_result_iterable.

Source code in src/trcks/fp/monads/awaitable_result.py
388
389
390
391
392
393
394
395
396
397
398
399
400
@deprecated("Use map_success_to_awaitable_result_iterable instead")
def map_success_to_awaitable_result_tuple(
    callable_: Callable[Concatenate[_S1, _P], AwaitableResultTuple[_F2, _S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[AwaitableResult[_F1, _S1]], AwaitableResultTuple[_F1 | _F2, _S2]]:
    """Deprecated alias for
    [trcks.fp.monads.awaitable_result.map_success_to_awaitable_result_iterable][].
    """
    return map_success_to_awaitable_result_iterable(
        callable_, *args, **kwargs
    )  # pragma: no cover

map_success_to_iterable(callable_, /, *args, **kwargs)

Create function that maps trcks.Success values to homogeneous tuples.

trcks.Failure values are left unchanged.

Parameters:

  • callable_ (Callable[Concatenate[_S1, _P], Iterable[_S2]]) –

    Synchronous function to apply to the trcks.Success values, returning an collections.abc.Iterable.

  • *args (args, default: () ) –

    Positional arguments to be passed to callable_.

  • **kwargs (kwargs, default: {} ) –

    Keyword arguments to be passed to callable_.

Returns:

Examples:

>>> import asyncio
>>> from trcks.fp.monads import awaitable_result as ar
>>> from trcks.fp.monads import awaitable_result_tuple as art
>>> def duplicate(x: float) -> tuple[float, ...]:
...     return (x, x)
...
>>> duplicate_success = ar.map_success_to_iterable(duplicate)
>>> a_r_tpl_1 = duplicate_success(ar.construct_failure("not found"))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
('failure', 'not found')
>>> a_r_tpl_2 = duplicate_success(ar.construct_success(5.0))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
('success', (5.0, 5.0))
Source code in src/trcks/fp/monads/awaitable_result.py
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
def map_success_to_iterable(
    callable_: Callable[Concatenate[_S1, _P], Iterable[_S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[AwaitableResult[_F1, _S1]], AwaitableResultTuple[_F1, _S2]]:
    """Create function that maps [trcks.Success][] values to homogeneous [tuple][]s.

    [trcks.Failure][] values are left unchanged.

    Args:
        callable_: Synchronous function to apply to the [trcks.Success][] values,
            returning an [collections.abc.Iterable][].
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Leaves [trcks.Failure][] values unchanged and
            maps [trcks.Success][] values to homogeneous [tuple][]s
            according to the given function.

    Examples:
        >>> import asyncio
        >>> from trcks.fp.monads import awaitable_result as ar
        >>> from trcks.fp.monads import awaitable_result_tuple as art
        >>> def duplicate(x: float) -> tuple[float, ...]:
        ...     return (x, x)
        ...
        >>> duplicate_success = ar.map_success_to_iterable(duplicate)
        >>> a_r_tpl_1 = duplicate_success(ar.construct_failure("not found"))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
        ('failure', 'not found')
        >>> a_r_tpl_2 = duplicate_success(ar.construct_success(5.0))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
        ('success', (5.0, 5.0))
    """
    return compose(
        art.construct_from_awaitable_result,
        art.map_successes_to_iterable(callable_, *args, **kwargs),
    )

map_success_to_result(callable_, /, *args, **kwargs)

Create function that maps trcks.AwaitableSuccess values to trcks.AwaitableResult values.

trcks.AwaitableFailure values are left unchanged.

Parameters:

  • callable_ (Callable[Concatenate[_S1, _P], Result[_F2, _S2]]) –

    Synchronous function to apply to trcks.AwaitableSuccess values.

  • *args (args, default: () ) –

    Positional arguments to be passed to callable_.

  • **kwargs (kwargs, default: {} ) –

    Keyword arguments to be passed to callable_.

Returns:

Examples:

>>> 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)
Source code in src/trcks/fp/_monads/awaitable_result.py
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
def map_success_to_result(
    callable_: Callable[Concatenate[_S1, _P], Result[_F2, _S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[AwaitableResult[_F1, _S1]], AwaitableResult[_F1 | _F2, _S2]]:
    """Create function that maps [trcks.AwaitableSuccess][] values
    to [trcks.AwaitableResult][] values.

    [trcks.AwaitableFailure][] values are left unchanged.

    Args:
        callable_: Synchronous function to apply to [trcks.AwaitableSuccess][] values.
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Leaves [trcks.AwaitableFailure][] values unchanged and
            maps [trcks.AwaitableSuccess][] values
            to [trcks.AwaitableFailure][] and [trcks.AwaitableSuccess][] values
            according to the given function.

    Examples:
        >>> 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)
    """
    return a.map_(r.map_success_to_result(callable_, *args, **kwargs))

map_success_to_result_iterable(callable_, /, *args, **kwargs)

Create function that maps trcks.Success values to new trcks.ResultTuple values.

trcks.Failure values are left unchanged.

Parameters:

  • callable_ (Callable[Concatenate[_S1, _P], ResultIterable[_F2, _S2]]) –

    Synchronous function to apply to the trcks.Success values.

  • *args (args, default: () ) –

    Positional arguments to be passed to callable_.

  • **kwargs (kwargs, default: {} ) –

    Keyword arguments to be passed to callable_.

Returns:

Examples:

>>> import asyncio
>>> from trcks import ResultTuple
>>> from trcks.fp.monads import awaitable_result as ar
>>> from trcks.fp.monads import awaitable_result_tuple as art
>>> def expand(x: float) -> ResultTuple[str, float]:
...     if x < 0:
...         return "failure", "negative"
...     return "success", (x, x * 2)
...
>>> expand_success = ar.map_success_to_result_iterable(expand)
>>> a_r_tpl_1 = expand_success(ar.construct_failure("not found"))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
('failure', 'not found')
>>> a_r_tpl_2 = expand_success(ar.construct_success(5.0))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
('success', (5.0, 10.0))
Source code in src/trcks/fp/monads/awaitable_result.py
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
def map_success_to_result_iterable(
    callable_: Callable[Concatenate[_S1, _P], ResultIterable[_F2, _S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[AwaitableResult[_F1, _S1]], AwaitableResultTuple[_F1 | _F2, _S2]]:
    """Create function that maps [trcks.Success][] values
    to new [trcks.ResultTuple][] values.

    [trcks.Failure][] values are left unchanged.

    Args:
        callable_: Synchronous function to apply to the [trcks.Success][] values.
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Leaves [trcks.Failure][] values unchanged and
            maps [trcks.Success][] values to new [trcks.ResultTuple][] values
            according to the given function.

    Examples:
        >>> import asyncio
        >>> from trcks import ResultTuple
        >>> from trcks.fp.monads import awaitable_result as ar
        >>> from trcks.fp.monads import awaitable_result_tuple as art
        >>> def expand(x: float) -> ResultTuple[str, float]:
        ...     if x < 0:
        ...         return "failure", "negative"
        ...     return "success", (x, x * 2)
        ...
        >>> expand_success = ar.map_success_to_result_iterable(expand)
        >>> a_r_tpl_1 = expand_success(ar.construct_failure("not found"))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
        ('failure', 'not found')
        >>> a_r_tpl_2 = expand_success(ar.construct_success(5.0))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
        ('success', (5.0, 10.0))
    """
    return compose(
        art.construct_from_awaitable_result,
        art.map_successes_to_result_iterable(callable_, *args, **kwargs),
    )

map_success_to_result_tuple(callable_, /, *args, **kwargs)

Deprecated alias for trcks.fp.monads.awaitable_result.map_success_to_result_iterable.

Source code in src/trcks/fp/monads/awaitable_result.py
494
495
496
497
498
499
500
501
502
503
504
505
506
@deprecated("Use map_success_to_result_iterable instead")
def map_success_to_result_tuple(
    callable_: Callable[Concatenate[_S1, _P], ResultTuple[_F2, _S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[AwaitableResult[_F1, _S1]], AwaitableResultTuple[_F1 | _F2, _S2]]:
    """Deprecated alias for
    [trcks.fp.monads.awaitable_result.map_success_to_result_iterable][].
    """
    return map_success_to_result_iterable(
        callable_, *args, **kwargs
    )  # pragma: no cover

map_success_to_tuple(callable_, /, *args, **kwargs)

Deprecated alias for trcks.fp.monads.awaitable_result.map_success_to_iterable.

Source code in src/trcks/fp/monads/awaitable_result.py
509
510
511
512
513
514
515
516
517
518
519
@deprecated("Use map_success_to_iterable instead")
def map_success_to_tuple(
    callable_: Callable[Concatenate[_S1, _P], tuple[_S2, ...]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[AwaitableResult[_F1, _S1]], AwaitableResultTuple[_F1, _S2]]:
    """Deprecated alias for
    [trcks.fp.monads.awaitable_result.map_success_to_iterable][].
    """
    return map_success_to_iterable(callable_, *args, **kwargs)  # pragma: no cover

tap_failure(callable_, /, *args, **kwargs)

Create function that applies a synchronous side effect to trcks.AwaitableFailure values.

trcks.AwaitableSuccess values are passed on without side effects.

Parameters:

  • callable_ (Callable[Concatenate[_F1, _P], object]) –

    Synchronous side effect to apply to the trcks.AwaitableFailure value.

  • *args (args, default: () ) –

    Positional arguments to be passed to callable_.

  • **kwargs (kwargs, default: {} ) –

    Keyword arguments to be passed to callable_.

Returns:

Source code in src/trcks/fp/_monads/awaitable_result.py
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
def tap_failure(
    callable_: Callable[Concatenate[_F1, _P], object],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[AwaitableResult[_F1, _S1]], AwaitableResult[_F1, _S1]]:
    """Create function that applies a synchronous side effect
    to [trcks.AwaitableFailure][] values.

    [trcks.AwaitableSuccess][] values are passed on without side effects.

    Args:
        callable_: Synchronous side effect to apply
            to the [trcks.AwaitableFailure][] value.
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Applies the given side effect to [trcks.AwaitableFailure][] values and
            returns the original [trcks.AwaitableFailure][] value.
            Passes on [trcks.AwaitableSuccess][] values without side effects.
    """
    return a.map_(r.tap_failure(callable_, *args, **kwargs))

tap_failure_to_awaitable(callable_, /, *args, **kwargs)

Create function that applies an asynchronous side effect to trcks.AwaitableFailure values.

trcks.AwaitableSuccess values are passed on without side effects.

Parameters:

  • callable_ (Callable[Concatenate[_F1, _P], Awaitable[object]]) –

    Asynchronous side effect to apply to the trcks.AwaitableFailure value.

  • *args (args, default: () ) –

    Positional arguments to be passed to callable_.

  • **kwargs (kwargs, default: {} ) –

    Keyword arguments to be passed to callable_.

Returns:

Source code in src/trcks/fp/_monads/awaitable_result.py
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
def tap_failure_to_awaitable(
    callable_: Callable[Concatenate[_F1, _P], Awaitable[object]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[AwaitableResult[_F1, _S1]], AwaitableResult[_F1, _S1]]:
    """Create function that applies an asynchronous side effect
    to [trcks.AwaitableFailure][] values.

    [trcks.AwaitableSuccess][] values are passed on without side effects.

    Args:
        callable_: Asynchronous side effect to apply
            to the [trcks.AwaitableFailure][] value.
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Applies the given side effect to [trcks.AwaitableFailure][] values and
            returns the original [trcks.AwaitableFailure][] value.
            Passes on [trcks.AwaitableSuccess][] values without side effects.
    """

    async def bypassed_callable(value: _F1) -> _F1:
        _ = await callable_(value, *args, **kwargs)
        return value

    return map_failure_to_awaitable(bypassed_callable)

tap_failure_to_awaitable_result(callable_, /, *args, **kwargs)

Create function that applies an asynchronous side effect with return type trcks.AwaitableResult to trcks.AwaitableFailure values.

trcks.AwaitableSuccess values are passed on without side effects.

Parameters:

Returns:

Source code in src/trcks/fp/_monads/awaitable_result.py
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
def tap_failure_to_awaitable_result(
    callable_: Callable[Concatenate[_F1, _P], AwaitableResult[object, _S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[AwaitableResult[_F1, _S1]], AwaitableResult[_F1, _S1 | _S2]]:
    """Create function that applies an asynchronous side effect
    with return type [trcks.AwaitableResult][] to [trcks.AwaitableFailure][] values.

    [trcks.AwaitableSuccess][] values are passed on without side effects.

    Args:
        callable_: Asynchronous side effect to apply
            to the [trcks.AwaitableFailure][] value.
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Applies the given side effect to [trcks.AwaitableFailure][] values.
            If the given side effect returns a [trcks.AwaitableFailure][],
            *the original* [trcks.AwaitableFailure][] value is returned.
            If the given side effect returns a [trcks.AwaitableSuccess][],
            *this* [trcks.AwaitableSuccess][] is returned.
            Passes on [trcks.AwaitableSuccess][] values without side effects.
    """

    async def bypassed_callable(value: _F1) -> Result[_F1, _S2]:
        match await callable_(value, *args, **kwargs):
            case ("failure", _):
                return r.construct_failure(value)
            case ("success", _) as rslt:
                return rslt
            case _ as rslt:  # pragma: no cover
                assert_type(rslt, Never)  # type: ignore[unreachable]  # pyright: ignore[reportUnreachable]
                msg = f"{type(rslt).__name__!r} is not a valid Result"
                raise TypeError(msg)

    return map_failure_to_awaitable_result(bypassed_callable)

tap_failure_to_awaitable_result_iterable(callable_, /, *args, **kwargs)

Create function that applies an asynchronous side effect with return type trcks.AwaitableResultIterable to trcks.Failure values.

trcks.Success values are passed on without side effects.

Parameters:

  • callable_ (Callable[Concatenate[_F1, _P], AwaitableResultIterable[object, _S2]]) –

    Asynchronous side effect to apply to the trcks.Failure value.

  • *args (args, default: () ) –

    Positional arguments to be passed to callable_.

  • **kwargs (kwargs, default: {} ) –

    Keyword arguments to be passed to callable_.

Returns:

Examples:

>>> import asyncio
>>> from trcks import AwaitableResultTuple
>>> from trcks.fp.monads import awaitable_result as ar
>>> from trcks.fp.monads import awaitable_result_tuple as art
>>> async def recover(e: str) -> AwaitableResultTuple[object, float]:
...     await asyncio.sleep(0.001)
...     if e == "not found":
...         return "success", (0.0, 1.0)
...     return "failure", e
...
>>> recover_after_failure = ar.tap_failure_to_awaitable_result_iterable(
...     recover
... )
>>> a_r_tpl_1 = recover_after_failure(ar.construct_failure("not found"))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
('success', (0.0, 1.0))
>>> a_r_tpl_2 = recover_after_failure(ar.construct_success(42))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
('success', (42,))
Source code in src/trcks/fp/monads/awaitable_result.py
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
def tap_failure_to_awaitable_result_iterable(
    callable_: Callable[Concatenate[_F1, _P], AwaitableResultIterable[object, _S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[AwaitableResult[_F1, _S1]], AwaitableResultTuple[_F1, _S1 | _S2]]:
    """Create function that applies an asynchronous side effect
    with return type [trcks.AwaitableResultIterable][] to [trcks.Failure][] values.

    [trcks.Success][] values are passed on without side effects.

    Args:
        callable_: Asynchronous side effect to apply to the [trcks.Failure][] value.
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Applies the given side effect to [trcks.Failure][] values.
            If the given side effect returns a [trcks.Failure][],
            *the original* [trcks.Failure][] is returned.
            If the given side effect returns a [trcks.SuccessIterable][],
            *this* [trcks.SuccessIterable][] is returned.
            Passes on [trcks.Success][] values (wrapped as a tuple)
            without side effects.

    Examples:
        >>> import asyncio
        >>> from trcks import AwaitableResultTuple
        >>> from trcks.fp.monads import awaitable_result as ar
        >>> from trcks.fp.monads import awaitable_result_tuple as art
        >>> async def recover(e: str) -> AwaitableResultTuple[object, float]:
        ...     await asyncio.sleep(0.001)
        ...     if e == "not found":
        ...         return "success", (0.0, 1.0)
        ...     return "failure", e
        ...
        >>> recover_after_failure = ar.tap_failure_to_awaitable_result_iterable(
        ...     recover
        ... )
        >>> a_r_tpl_1 = recover_after_failure(ar.construct_failure("not found"))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
        ('success', (0.0, 1.0))
        >>> a_r_tpl_2 = recover_after_failure(ar.construct_success(42))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
        ('success', (42,))
    """
    return compose(
        art.construct_from_awaitable_result,
        art.tap_failure_to_awaitable_result_iterable(callable_, *args, **kwargs),
    )

tap_failure_to_awaitable_result_tuple(callable_, /, *args, **kwargs)

Deprecated alias for trcks.fp.monads.awaitable_result.tap_failure_to_awaitable_result_iterable.

Source code in src/trcks/fp/monads/awaitable_result.py
576
577
578
579
580
581
582
583
584
585
586
587
588
@deprecated("Use tap_failure_to_awaitable_result_iterable instead")
def tap_failure_to_awaitable_result_tuple(
    callable_: Callable[Concatenate[_F1, _P], AwaitableResultTuple[object, _S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[AwaitableResult[_F1, _S1]], AwaitableResultTuple[_F1, _S1 | _S2]]:
    """Deprecated alias for
    [trcks.fp.monads.awaitable_result.tap_failure_to_awaitable_result_iterable][].
    """
    return tap_failure_to_awaitable_result_iterable(
        callable_, *args, **kwargs
    )  # pragma: no cover

tap_failure_to_iterable(callable_, /, *args, **kwargs)

Create function that applies a collections.abc.Iterable-returning side effect to trcks.Failure values.

trcks.Success values are passed on without side effects.

Parameters:

Returns:

Examples:

>>> import asyncio
>>> from trcks.fp.monads import awaitable_result as ar
>>> from trcks.fp.monads import awaitable_result_tuple as art
>>> def log_err(e: str) -> tuple[None, ...]:
...     print(f"Error logged: {e}")
...     print(f"Alert sent: {e}")
...     return (None, None)
...
>>> log_failure = ar.tap_failure_to_iterable(log_err)
>>> a_r_tpl_1 = log_failure(ar.construct_failure("critical"))
>>> r_tpl_1 = asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
Error logged: critical
Alert sent: critical
>>> r_tpl_1
('success', ('critical', 'critical'))
>>> a_r_tpl_2 = log_failure(ar.construct_success(42))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
('success', (42,))
Source code in src/trcks/fp/monads/awaitable_result.py
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
def tap_failure_to_iterable(
    callable_: Callable[Concatenate[_F1, _P], Iterable[object]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[
    [AwaitableResult[_F1, _S1]],
    Awaitable[SuccessTuple[_F1] | SuccessTuple[_S1]],
]:
    """Create function that applies a [collections.abc.Iterable][]-returning
    side effect to [trcks.Failure][] values.

    [trcks.Success][] values are passed on without side effects.

    Args:
        callable_: Side effect to apply to the [trcks.Failure][] value,
            returning an [collections.abc.Iterable][].
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Applies the given side effect to [trcks.Failure][] values and converts them
            to [trcks.AwaitableSuccessTuple][] values containing
            the original failure repeated once per element
            in the [collections.abc.Iterable][] returned by the side effect.
            Passes on [trcks.Success][] values (wrapped as a tuple)
            without side effects.

    Examples:
        >>> import asyncio
        >>> from trcks.fp.monads import awaitable_result as ar
        >>> from trcks.fp.monads import awaitable_result_tuple as art
        >>> def log_err(e: str) -> tuple[None, ...]:
        ...     print(f"Error logged: {e}")
        ...     print(f"Alert sent: {e}")
        ...     return (None, None)
        ...
        >>> log_failure = ar.tap_failure_to_iterable(log_err)
        >>> a_r_tpl_1 = log_failure(ar.construct_failure("critical"))
        >>> r_tpl_1 = asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
        Error logged: critical
        Alert sent: critical
        >>> r_tpl_1
        ('success', ('critical', 'critical'))
        >>> a_r_tpl_2 = log_failure(ar.construct_success(42))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
        ('success', (42,))
    """
    c: tuple[
        Callable[[AwaitableResult[_F1, _S1]], AwaitableResultTuple[_F1, _S1]],
        Callable[
            [AwaitableResultTuple[_F1, _S1]],
            Awaitable[SuccessTuple[_F1] | SuccessTuple[_S1]],
        ],
    ] = (
        art.construct_from_awaitable_result,
        art.tap_failure_to_iterable(callable_, *args, **kwargs),
    )
    return compose(*c)

tap_failure_to_result(callable_, /, *args, **kwargs)

Create function that applies a synchronous side effect with return type trcks.Result to trcks.AwaitableFailure values.

trcks.AwaitableSuccess values are passed on without side effects.

Parameters:

  • callable_ (Callable[Concatenate[_F1, _P], Result[object, _S2]]) –

    Synchronous side effect to apply to the trcks.AwaitableFailure value.

  • *args (args, default: () ) –

    Positional arguments to be passed to callable_.

  • **kwargs (kwargs, default: {} ) –

    Keyword arguments to be passed to callable_.

Returns:

Source code in src/trcks/fp/_monads/awaitable_result.py
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
def tap_failure_to_result(
    callable_: Callable[Concatenate[_F1, _P], Result[object, _S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[AwaitableResult[_F1, _S1]], AwaitableResult[_F1, _S1 | _S2]]:
    """Create function that applies a synchronous side effect
    with return type [trcks.Result][] to [trcks.AwaitableFailure][] values.

    [trcks.AwaitableSuccess][] values are passed on without side effects.

    Args:
        callable_: Synchronous side effect to apply
            to the [trcks.AwaitableFailure][] value.
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Applies the given side effect to [trcks.AwaitableFailure][] values.
            If the given side effect returns a [trcks.Failure][],
            *the original* [trcks.AwaitableFailure][] value is returned.
            If the given side effect returns a [trcks.Success][],
            *this* [trcks.Success][] is returned.
            Passes on [trcks.AwaitableSuccess][] values without side effects.
    """
    return a.map_(r.tap_failure_to_result(callable_, *args, **kwargs))

tap_failure_to_result_iterable(callable_, /, *args, **kwargs)

Create function that applies a side effect with return type trcks.ResultIterable to trcks.Failure values.

trcks.Success values are passed on without side effects.

Parameters:

  • callable_ (Callable[Concatenate[_F1, _P], ResultIterable[object, _S2]]) –

    Side effect to apply to the trcks.Failure value.

  • *args (args, default: () ) –

    Positional arguments to be passed to callable_.

  • **kwargs (kwargs, default: {} ) –

    Keyword arguments to be passed to callable_.

Returns:

Examples:

>>> import asyncio
>>> from trcks import ResultTuple
>>> from trcks.fp.monads import awaitable_result as ar
>>> from trcks.fp.monads import awaitable_result_tuple as art
>>> def recover(e: str) -> ResultTuple[object, float]:
...     if e == "not found":
...         return "success", (0.0, 1.0)
...     return "failure", e
...
>>> recover_after_failure = ar.tap_failure_to_result_iterable(recover)
>>> a_r_tpl_1 = recover_after_failure(ar.construct_failure("not found"))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
('success', (0.0, 1.0))
>>> a_r_tpl_2 = recover_after_failure(ar.construct_success(42))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
('success', (42,))
Source code in src/trcks/fp/monads/awaitable_result.py
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
def tap_failure_to_result_iterable(
    callable_: Callable[Concatenate[_F1, _P], ResultIterable[object, _S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[AwaitableResult[_F1, _S1]], AwaitableResultTuple[_F1, _S1 | _S2]]:
    """Create function that applies a side effect with return type
    [trcks.ResultIterable][] to [trcks.Failure][] values.

    [trcks.Success][] values are passed on without side effects.

    Args:
        callable_: Side effect to apply to the [trcks.Failure][] value.
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Applies the given side effect to [trcks.Failure][] values.
            If the given side effect returns a [trcks.Failure][],
            *the original* [trcks.Failure][] is returned.
            If the given side effect returns a [trcks.SuccessIterable][],
            *this* [trcks.SuccessIterable][] is returned.
            Passes on [trcks.Success][] values (wrapped as a tuple)
            without side effects.

    Examples:
        >>> import asyncio
        >>> from trcks import ResultTuple
        >>> from trcks.fp.monads import awaitable_result as ar
        >>> from trcks.fp.monads import awaitable_result_tuple as art
        >>> def recover(e: str) -> ResultTuple[object, float]:
        ...     if e == "not found":
        ...         return "success", (0.0, 1.0)
        ...     return "failure", e
        ...
        >>> recover_after_failure = ar.tap_failure_to_result_iterable(recover)
        >>> a_r_tpl_1 = recover_after_failure(ar.construct_failure("not found"))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
        ('success', (0.0, 1.0))
        >>> a_r_tpl_2 = recover_after_failure(ar.construct_success(42))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
        ('success', (42,))
    """
    return compose(
        art.construct_from_awaitable_result,
        art.tap_failure_to_result_iterable(callable_, *args, **kwargs),
    )

tap_failure_to_result_tuple(callable_, /, *args, **kwargs)

Deprecated alias for trcks.fp.monads.awaitable_result.tap_failure_to_result_iterable.

Source code in src/trcks/fp/monads/awaitable_result.py
705
706
707
708
709
710
711
712
713
714
715
716
717
@deprecated("Use tap_failure_to_result_iterable instead")
def tap_failure_to_result_tuple(
    callable_: Callable[Concatenate[_F1, _P], ResultTuple[object, _S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[AwaitableResult[_F1, _S1]], AwaitableResultTuple[_F1, _S1 | _S2]]:
    """Deprecated alias for
    [trcks.fp.monads.awaitable_result.tap_failure_to_result_iterable][].
    """
    return tap_failure_to_result_iterable(
        callable_, *args, **kwargs
    )  # pragma: no cover

tap_failure_to_tuple(callable_, /, *args, **kwargs)

Deprecated alias for trcks.fp.monads.awaitable_result.tap_failure_to_iterable.

Source code in src/trcks/fp/monads/awaitable_result.py
720
721
722
723
724
725
726
727
728
729
730
731
732
733
@deprecated("Use tap_failure_to_iterable instead")
def tap_failure_to_tuple(
    callable_: Callable[Concatenate[_F1, _P], tuple[object, ...]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[
    [AwaitableResult[_F1, _S1]],
    Awaitable[SuccessTuple[_F1] | SuccessTuple[_S1]],
]:
    """Deprecated alias for
    [trcks.fp.monads.awaitable_result.tap_failure_to_iterable][].
    """
    return tap_failure_to_iterable(callable_, *args, **kwargs)  # pragma: no cover

tap_success(callable_, /, *args, **kwargs)

Create function that applies a synchronous side effect to trcks.AwaitableSuccess values.

trcks.AwaitableFailure values are passed on without side effects.

Parameters:

  • callable_ (Callable[Concatenate[_S1, _P], object]) –

    Synchronous side effect to apply to the trcks.AwaitableSuccess value.

  • *args (args, default: () ) –

    Positional arguments to be passed to callable_.

  • **kwargs (kwargs, default: {} ) –

    Keyword arguments to be passed to callable_.

Returns:

Source code in src/trcks/fp/_monads/awaitable_result.py
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
def tap_success(
    callable_: Callable[Concatenate[_S1, _P], object],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[AwaitableResult[_F1, _S1]], AwaitableResult[_F1, _S1]]:
    """Create function that applies a synchronous side effect
    to [trcks.AwaitableSuccess][] values.

    [trcks.AwaitableFailure][] values are passed on without side effects.

    Args:
        callable_: Synchronous side effect to apply
            to the [trcks.AwaitableSuccess][] value.
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Passes on [trcks.AwaitableFailure][] values without side effects.
            Applies the given side effect to [trcks.AwaitableSuccess][] values and
            returns the original [trcks.AwaitableSuccess][] value.
    """
    return a.map_(r.tap_success(callable_, *args, **kwargs))

tap_success_to_awaitable(callable_, /, *args, **kwargs)

Create function that applies an asynchronous side effect to trcks.AwaitableSuccess values.

trcks.AwaitableFailure values are passed on without side effects.

Parameters:

  • callable_ (Callable[Concatenate[_S1, _P], Awaitable[object]]) –

    Asynchronous side effect to apply to the trcks.AwaitableSuccess value.

  • *args (args, default: () ) –

    Positional arguments to be passed to callable_.

  • **kwargs (kwargs, default: {} ) –

    Keyword arguments to be passed to callable_.

Returns:

Source code in src/trcks/fp/_monads/awaitable_result.py
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
def tap_success_to_awaitable(
    callable_: Callable[Concatenate[_S1, _P], Awaitable[object]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[AwaitableResult[_F1, _S1]], AwaitableResult[_F1, _S1]]:
    """Create function that applies an asynchronous side effect
    to [trcks.AwaitableSuccess][] values.

    [trcks.AwaitableFailure][] values are passed on without side effects.

    Args:
        callable_: Asynchronous side effect to apply
            to the [trcks.AwaitableSuccess][] value.
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Passes on [trcks.AwaitableFailure][] values without side effects.
            Applies the given side effect to [trcks.AwaitableSuccess][] values and
            returns the original [trcks.AwaitableSuccess][] value.
    """

    async def bypassed_callable(value: _S1) -> _S1:
        _ = await callable_(value, *args, **kwargs)
        return value

    return map_success_to_awaitable(bypassed_callable)

tap_success_to_awaitable_result(callable_, /, *args, **kwargs)

Create function that applies an asynchronous side effect with return type trcks.AwaitableResult to trcks.AwaitableSuccess values.

trcks.AwaitableFailure values are passed on without side effects.

Parameters:

Returns:

Source code in src/trcks/fp/_monads/awaitable_result.py
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
def tap_success_to_awaitable_result(
    callable_: Callable[Concatenate[_S1, _P], AwaitableResult[_F2, object]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[AwaitableResult[_F1, _S1]], AwaitableResult[_F1 | _F2, _S1]]:
    """Create function that applies an asynchronous side effect
    with return type [trcks.AwaitableResult][] to [trcks.AwaitableSuccess][] values.

    [trcks.AwaitableFailure][] values are passed on without side effects.

    Args:
        callable_: Asynchronous side effect to apply
            to the [trcks.AwaitableSuccess][] value.
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Passes on [trcks.AwaitableFailure][] values without side effects.
            Applies the given side effect to [trcks.AwaitableSuccess][] values.
            If the given side effect returns a [trcks.AwaitableFailure][],
            *this* [trcks.AwaitableFailure][] is returned.
            If the given side effect returns a [trcks.AwaitableSuccess][],
            *the original* [trcks.AwaitableSuccess][] value is returned.
    """

    async def bypassed_callable(value: _S1) -> Result[_F2, _S1]:
        match await callable_(value, *args, **kwargs):
            case ("failure", _) as rslt:
                return rslt
            case ("success", _):
                return r.construct_success(value)
            case _ as rslt:  # pragma: no cover
                assert_type(rslt, Never)  # type: ignore[unreachable]  # pyright: ignore[reportUnreachable]
                msg = f"{type(rslt).__name__!r} is not a valid Result"
                raise TypeError(msg)

    return map_success_to_awaitable_result(bypassed_callable)

tap_success_to_awaitable_result_iterable(callable_, /, *args, **kwargs)

Create function that applies an asynchronous side effect with return type trcks.AwaitableResultIterable to trcks.Success values.

trcks.Failure values are passed on without side effects.

Parameters:

  • callable_ (Callable[Concatenate[_S1, _P], AwaitableResultIterable[_F2, object]]) –

    Asynchronous side effect to apply to the trcks.Success value.

  • *args (args, default: () ) –

    Positional arguments to be passed to callable_.

  • **kwargs (kwargs, default: {} ) –

    Keyword arguments to be passed to callable_.

Returns:

Examples:

>>> import asyncio
>>> from trcks import AwaitableResultTuple
>>> from trcks.fp.monads import awaitable_result as ar
>>> from trcks.fp.monads import awaitable_result_tuple as art
>>> async def write_twice(s: str) -> AwaitableResultTuple[str, None]:
...     await asyncio.sleep(0.001)
...     print(f"Wrote '{s}' twice.")
...     return "success", (None, None)
...
>>> write_success_twice = ar.tap_success_to_awaitable_result_iterable(
...     write_twice
... )
>>> a_r_tpl_1 = write_success_twice(ar.construct_failure("missing text"))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
('failure', 'missing text')
>>> a_r_tpl_2 = write_success_twice(ar.construct_success("Hello, world!"))
>>> r_tpl_2 = asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
Wrote 'Hello, world!' twice.
>>> r_tpl_2
('success', ('Hello, world!', 'Hello, world!'))
Source code in src/trcks/fp/monads/awaitable_result.py
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
def tap_success_to_awaitable_result_iterable(
    callable_: Callable[Concatenate[_S1, _P], AwaitableResultIterable[_F2, object]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[AwaitableResult[_F1, _S1]], AwaitableResultTuple[_F1 | _F2, _S1]]:
    """Create function that applies an asynchronous side effect
    with return type [trcks.AwaitableResultIterable][] to [trcks.Success][] values.

    [trcks.Failure][] values are passed on without side effects.

    Args:
        callable_: Asynchronous side effect to apply to the [trcks.Success][] value.
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Passes on [trcks.Failure][] values (wrapped as a tuple)
            without side effects.
            Applies the given side effect to [trcks.Success][] values.
            If the given side effect returns a [trcks.Failure][],
            *this* [trcks.Failure][] is returned.
            If the given side effect returns a [trcks.SuccessIterable][],
            *the original* [trcks.Success][] value is repeated once per element
            in the returned [trcks.SuccessIterable][].

    Examples:
        >>> import asyncio
        >>> from trcks import AwaitableResultTuple
        >>> from trcks.fp.monads import awaitable_result as ar
        >>> from trcks.fp.monads import awaitable_result_tuple as art
        >>> async def write_twice(s: str) -> AwaitableResultTuple[str, None]:
        ...     await asyncio.sleep(0.001)
        ...     print(f"Wrote '{s}' twice.")
        ...     return "success", (None, None)
        ...
        >>> write_success_twice = ar.tap_success_to_awaitable_result_iterable(
        ...     write_twice
        ... )
        >>> a_r_tpl_1 = write_success_twice(ar.construct_failure("missing text"))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
        ('failure', 'missing text')
        >>> a_r_tpl_2 = write_success_twice(ar.construct_success("Hello, world!"))
        >>> r_tpl_2 = asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
        Wrote 'Hello, world!' twice.
        >>> r_tpl_2
        ('success', ('Hello, world!', 'Hello, world!'))
    """
    return compose(
        art.construct_from_awaitable_result,
        art.tap_successes_to_awaitable_result_iterable(callable_, *args, **kwargs),
    )

tap_success_to_awaitable_result_tuple(callable_, /, *args, **kwargs)

Deprecated alias for trcks.fp.monads.awaitable_result.tap_success_to_awaitable_result_iterable.

Source code in src/trcks/fp/monads/awaitable_result.py
792
793
794
795
796
797
798
799
800
801
802
803
804
@deprecated("Use tap_success_to_awaitable_result_iterable instead")
def tap_success_to_awaitable_result_tuple(
    callable_: Callable[Concatenate[_S1, _P], AwaitableResultTuple[_F2, object]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[AwaitableResult[_F1, _S1]], AwaitableResultTuple[_F1 | _F2, _S1]]:
    """Deprecated alias for
    [trcks.fp.monads.awaitable_result.tap_success_to_awaitable_result_iterable][].
    """
    return tap_success_to_awaitable_result_iterable(
        callable_, *args, **kwargs
    )  # pragma: no cover

tap_success_to_iterable(callable_, /, *args, **kwargs)

Create function that applies a collections.abc.Iterable-returning side effect to trcks.Success values.

trcks.Failure values are passed on without side effects.

Parameters:

Returns:

Examples:

>>> import asyncio
>>> from trcks.fp.monads import awaitable_result as ar
>>> from trcks.fp.monads import awaitable_result_tuple as art
>>> def log_mult(n: int) -> tuple[None, ...]:
...     print(f"v={n}")
...     print(f"v={n}")
...     return (None, None)
...
>>> log_success = ar.tap_success_to_iterable(log_mult)
>>> a_r_tpl_1 = log_success(ar.construct_failure("error"))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
('failure', 'error')
>>> a_r_tpl_2 = log_success(ar.construct_success(7))
>>> r_tpl_2 = asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
v=7
v=7
>>> r_tpl_2
('success', (7, 7))
Source code in src/trcks/fp/monads/awaitable_result.py
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
def tap_success_to_iterable(
    callable_: Callable[Concatenate[_S1, _P], Iterable[object]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[AwaitableResult[_F1, _S1]], AwaitableResultTuple[_F1, _S1]]:
    """Create function that applies a [collections.abc.Iterable][]-returning
    side effect to [trcks.Success][] values.

    [trcks.Failure][] values are passed on without side effects.

    Args:
        callable_: Side effect to apply to the [trcks.Success][] value,
            returning an [collections.abc.Iterable][].
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Passes on [trcks.Failure][] values (wrapped as a tuple)
            without side effects.
            Applies the given side effect to [trcks.Success][] values and
            repeats the original element once per element
            in the [collections.abc.Iterable][] returned by the side effect.

    Examples:
        >>> import asyncio
        >>> from trcks.fp.monads import awaitable_result as ar
        >>> from trcks.fp.monads import awaitable_result_tuple as art
        >>> def log_mult(n: int) -> tuple[None, ...]:
        ...     print(f"v={n}")
        ...     print(f"v={n}")
        ...     return (None, None)
        ...
        >>> log_success = ar.tap_success_to_iterable(log_mult)
        >>> a_r_tpl_1 = log_success(ar.construct_failure("error"))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
        ('failure', 'error')
        >>> a_r_tpl_2 = log_success(ar.construct_success(7))
        >>> r_tpl_2 = asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
        v=7
        v=7
        >>> r_tpl_2
        ('success', (7, 7))
    """
    return compose(
        art.construct_from_awaitable_result,
        art.tap_successes_to_iterable(callable_, *args, **kwargs),
    )

tap_success_to_result(callable_, /, *args, **kwargs)

Create function that applies a synchronous side effect with return type trcks.Result to trcks.AwaitableSuccess values.

trcks.AwaitableFailure values are passed on without side effects.

Parameters:

  • callable_ (Callable[Concatenate[_S1, _P], Result[_F2, object]]) –

    Synchronous side effect to apply to the trcks.AwaitableSuccess value.

  • *args (args, default: () ) –

    Positional arguments to be passed to callable_.

  • **kwargs (kwargs, default: {} ) –

    Keyword arguments to be passed to callable_.

Returns:

Source code in src/trcks/fp/_monads/awaitable_result.py
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
def tap_success_to_result(
    callable_: Callable[Concatenate[_S1, _P], Result[_F2, object]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[AwaitableResult[_F1, _S1]], AwaitableResult[_F1 | _F2, _S1]]:
    """Create function that applies a synchronous side effect
    with return type [trcks.Result][] to [trcks.AwaitableSuccess][] values.

    [trcks.AwaitableFailure][] values are passed on without side effects.

    Args:
        callable_: Synchronous side effect to apply
            to the [trcks.AwaitableSuccess][] value.
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Passes on [trcks.AwaitableFailure][] values without side effects.
            Applies the given side effect to [trcks.AwaitableSuccess][] values.
            If the given side effect returns a [trcks.Failure][],
            *this* [trcks.Failure][] is returned.
            If the given side effect returns a [trcks.Success][],
            *the original* [trcks.AwaitableSuccess][] value is returned.
    """
    return a.map_(r.tap_success_to_result(callable_, *args, **kwargs))

tap_success_to_result_iterable(callable_, /, *args, **kwargs)

Create function that applies a side effect with return type trcks.ResultIterable to trcks.Success values.

trcks.Failure values are passed on without side effects.

Parameters:

  • callable_ (Callable[Concatenate[_S1, _P], ResultIterable[_F2, object]]) –

    Side effect to apply to the trcks.Success value.

  • *args (args, default: () ) –

    Positional arguments to be passed to callable_.

  • **kwargs (kwargs, default: {} ) –

    Keyword arguments to be passed to callable_.

Returns:

Examples:

>>> import asyncio
>>> from trcks import ResultTuple
>>> from trcks.fp.monads import awaitable_result as ar
>>> from trcks.fp.monads import awaitable_result_tuple as art
>>> def audit(s: str) -> ResultTuple[str, None]:
...     if s:
...         return "success", (None, None)
...     return "failure", "empty"
...
>>> audit_success = ar.tap_success_to_result_iterable(audit)
>>> a_r_tpl_1 = audit_success(ar.construct_failure("missing"))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
('failure', 'missing')
>>> a_r_tpl_2 = audit_success(ar.construct_success("Hello, world!"))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
('success', ('Hello, world!', 'Hello, world!'))
Source code in src/trcks/fp/monads/awaitable_result.py
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
def tap_success_to_result_iterable(
    callable_: Callable[Concatenate[_S1, _P], ResultIterable[_F2, object]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[AwaitableResult[_F1, _S1]], AwaitableResultTuple[_F1 | _F2, _S1]]:
    """Create function that applies a side effect with return type
    [trcks.ResultIterable][] to [trcks.Success][] values.

    [trcks.Failure][] values are passed on without side effects.

    Args:
        callable_: Side effect to apply to the [trcks.Success][] value.
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Passes on [trcks.Failure][] values (wrapped as a tuple)
            without side effects.
            Applies the given side effect to [trcks.Success][] values.
            If the given side effect returns a [trcks.Failure][],
            *this* [trcks.Failure][] is returned.
            If the given side effect returns a [trcks.SuccessIterable][],
            *the original* [trcks.Success][] value is repeated once per element
            in the returned [trcks.SuccessIterable][].

    Examples:
        >>> import asyncio
        >>> from trcks import ResultTuple
        >>> from trcks.fp.monads import awaitable_result as ar
        >>> from trcks.fp.monads import awaitable_result_tuple as art
        >>> def audit(s: str) -> ResultTuple[str, None]:
        ...     if s:
        ...         return "success", (None, None)
        ...     return "failure", "empty"
        ...
        >>> audit_success = ar.tap_success_to_result_iterable(audit)
        >>> a_r_tpl_1 = audit_success(ar.construct_failure("missing"))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
        ('failure', 'missing')
        >>> a_r_tpl_2 = audit_success(ar.construct_success("Hello, world!"))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
        ('success', ('Hello, world!', 'Hello, world!'))
    """
    return compose(
        art.construct_from_awaitable_result,
        art.tap_successes_to_result_iterable(callable_, *args, **kwargs),
    )

tap_success_to_result_tuple(callable_, /, *args, **kwargs)

Deprecated alias for trcks.fp.monads.awaitable_result.tap_success_to_result_iterable.

Source code in src/trcks/fp/monads/awaitable_result.py
911
912
913
914
915
916
917
918
919
920
921
922
923
@deprecated("Use tap_success_to_result_iterable instead")
def tap_success_to_result_tuple(
    callable_: Callable[Concatenate[_S1, _P], ResultTuple[_F2, object]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[AwaitableResult[_F1, _S1]], AwaitableResultTuple[_F1 | _F2, _S1]]:
    """Deprecated alias for
    [trcks.fp.monads.awaitable_result.tap_success_to_result_iterable][].
    """
    return tap_success_to_result_iterable(
        callable_, *args, **kwargs
    )  # pragma: no cover

tap_success_to_tuple(callable_, /, *args, **kwargs)

Deprecated alias for trcks.fp.monads.awaitable_result.tap_success_to_iterable.

Source code in src/trcks/fp/monads/awaitable_result.py
926
927
928
929
930
931
932
933
934
935
936
@deprecated("Use tap_success_to_iterable instead")
def tap_success_to_tuple(
    callable_: Callable[Concatenate[_S1, _P], tuple[object, ...]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[AwaitableResult[_F1, _S1]], AwaitableResultTuple[_F1, _S1]]:
    """Deprecated alias for
    [trcks.fp.monads.awaitable_result.tap_success_to_iterable][].
    """
    return tap_success_to_iterable(callable_, *args, **kwargs)  # pragma: no cover

to_coroutine_result(a_rslt) async

Turn a trcks.AwaitableResult into a collections.abc.Coroutine.

This is useful for functions that expect a coroutine (e.g. asyncio.run in Python 3.13 and older).

Parameters:

Returns:

Examples:

>>> import asyncio
>>> from trcks import Result
>>> from trcks.fp.monads import awaitable_result as ar
>>> loop = asyncio.new_event_loop()
>>> future: asyncio.Future[Result[str, int]] = loop.create_future()
>>> future.set_result(("success", 42))
>>> future
<Future finished result=('success', 42)>
>>> coro = ar.to_coroutine_result(future)
>>> coro
<coroutine object to_coroutine_result at 0x...>
>>> loop.run_until_complete(coro)
('success', 42)
>>> loop.close()
Source code in src/trcks/fp/_monads/awaitable_result.py
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
async def to_coroutine_result(a_rslt: AwaitableResult[_F, _S], /) -> Result[_F, _S]:
    """Turn a [trcks.AwaitableResult][] into a [collections.abc.Coroutine][].

    This is useful for functions that expect a coroutine
    (e.g. [asyncio.run][] in Python 3.13 and older).

    Args:
        a_rslt:
            The [trcks.AwaitableResult][] to be transformed
                into a [collections.abc.Coroutine][].

    Returns:
        The given [trcks.AwaitableResult][] transformed
            into a [collections.abc.Coroutine][].

    Examples:
        >>> import asyncio
        >>> from trcks import Result
        >>> from trcks.fp.monads import awaitable_result as ar
        >>> loop = asyncio.new_event_loop()
        >>> future: asyncio.Future[Result[str, int]] = loop.create_future()
        >>> future.set_result(("success", 42))
        >>> future
        <Future finished result=('success', 42)>
        >>> coro = ar.to_coroutine_result(future)
        >>> coro
        <coroutine object to_coroutine_result at 0x...>
        >>> loop.run_until_complete(coro)
        ('success', 42)
        >>> loop.close()
    """
    return await a_rslt