Skip to content

trcks.fp.monads.result_tuple

Monadic functions for trcks.ResultTuple.

Provides utilities for functional composition of functions returning trcks.ResultTuple values.

Examples:

Map and tap each element inside a success tuple:

>>> from trcks.fp.composition import pipe
>>> from trcks.fp.monads import result_tuple as rt
>>> def double_integer(n: int) -> int:
...     return n * 2
...
>>> def duplicate_integer(n: int) -> tuple[int, int]:
...     return n, n
...
>>> def log_integer(n: int) -> None:
...     print(f"Received: {n}")
...
>>> result_tuple = pipe(
...     rt.construct_successes_from_iterable((1, 2, 3)),
...     rt.map_successes(double_integer),
...     rt.tap_successes(log_integer),
...     rt.map_successes_to_iterable(duplicate_integer),
... )
Received: 2
Received: 4
Received: 6
>>> result_tuple
('success', (2, 2, 4, 4, 6, 6))

construct_failure(value)

Create a trcks.Failure object from a value.

Parameters:

Returns:

Note

This function is an alias for trcks.fp.monads.result.construct_failure.

Examples:

>>> from trcks.fp.monads import result_tuple as rt
>>> rt.construct_failure("not found")
('failure', 'not found')
Source code in src/trcks/fp/_monads/result_tuple.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
def construct_failure(value: _F, /) -> Failure[_F]:
    """Create a [trcks.Failure][] object from a value.

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

    Returns:
        [trcks.Failure][] object containing the given value.

    Note:
        This function is an alias for
        [trcks.fp.monads.result.construct_failure][].

    Examples:
        >>> from trcks.fp.monads import result_tuple as rt
        >>> rt.construct_failure("not found")
        ('failure', 'not found')
    """
    return r.construct_failure(value)

construct_from_result(rslt)

Create a trcks.ResultTuple object from a trcks.Result.

Parameters:

Returns:

Examples:

>>> from trcks.fp.monads import result_tuple as rt
>>> rt.construct_from_result(("success", 7))
('success', (7,))
>>> rt.construct_from_result(("failure", "oops"))
('failure', 'oops')
Source code in src/trcks/fp/_monads/result_tuple.py
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def construct_from_result(rslt: Result[_F, _S], /) -> ResultTuple[_F, _S]:
    """Create a [trcks.ResultTuple][] object from a [trcks.Result][].

    Args:
        rslt: The [trcks.Result][] object to be wrapped.

    Returns:
        A new [trcks.ResultTuple][] instance with the success payload
            wrapped in a homogeneous tuple.

    Examples:
        >>> from trcks.fp.monads import result_tuple as rt
        >>> rt.construct_from_result(("success", 7))
        ('success', (7,))
        >>> rt.construct_from_result(("failure", "oops"))
        ('failure', 'oops')
    """
    return r.map_success(t.construct)(rslt)

construct_from_result_iterable(r_it)

Create a trcks.ResultTuple object from a trcks.ResultIterable.

Parameters:

Returns:

Examples:

>>> from trcks.fp.monads import result_tuple as rt
>>> rt.construct_from_result_iterable(("success", [1, 2]))
('success', (1, 2))
>>> rt.construct_from_result_iterable(("failure", "oops"))
('failure', 'oops')
Source code in src/trcks/fp/_monads/result_tuple.py
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
def construct_from_result_iterable(
    r_it: ResultIterable[_F, _S], /
) -> ResultTuple[_F, _S]:
    """Create a [trcks.ResultTuple][] object from a [trcks.ResultIterable][].

    Args:
        r_it: The [trcks.ResultIterable][] object to be converted.

    Returns:
        A new [trcks.ResultTuple][] instance
            with the success payload converted to a tuple,
            or the original failure.

    Examples:
        >>> from trcks.fp.monads import result_tuple as rt
        >>> rt.construct_from_result_iterable(("success", [1, 2]))
        ('success', (1, 2))
        >>> rt.construct_from_result_iterable(("failure", "oops"))
        ('failure', 'oops')
    """
    mapped: Callable[[ResultIterable[_F, _S]], ResultTuple[_F, _S]] = (
        r.map_success_to_result(construct_successes_from_iterable)
    )
    return mapped(r_it)

construct_from_result_tuple(r_tpl)

Deprecated alias for trcks.fp.monads.result_tuple.construct_from_result_iterable.

Source code in src/trcks/fp/monads/result_tuple.py
145
146
147
148
149
150
151
152
153
@deprecated("Use construct_from_result_iterable instead")
def construct_from_result_tuple(
    r_tpl: ResultTuple[_F1, _S1],
    /,
) -> ResultTuple[_F1, _S1]:
    """Deprecated alias for
    [trcks.fp.monads.result_tuple.construct_from_result_iterable][].
    """
    return construct_from_result_iterable(r_tpl)  # pragma: no cover

construct_successes(value)

Create a trcks.SuccessTuple object from a single value.

Parameters:

  • value (_S) –

    A single value.

Returns:

Examples:

>>> from trcks.fp.monads import result_tuple as rt
>>> rt.construct_successes(42)
('success', (42,))
Source code in src/trcks/fp/_monads/result_tuple.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
def construct_successes(value: _S, /) -> SuccessTuple[_S]:
    """Create a [trcks.SuccessTuple][] object from a single value.

    Args:
        value: A single value.

    Returns:
        A new [trcks.SuccessTuple][] instance containing the single value.

    Examples:
        >>> from trcks.fp.monads import result_tuple as rt
        >>> rt.construct_successes(42)
        ('success', (42,))
    """
    return r.construct_success(t.construct(value))

construct_successes_from_iterable(it)

Create a trcks.SuccessTuple object from an iterable.

Parameters:

Returns:

Examples:

>>> from trcks.fp.monads import result_tuple as rt
>>> rt.construct_successes_from_iterable((1, 2))
('success', (1, 2))
Source code in src/trcks/fp/_monads/result_tuple.py
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
def construct_successes_from_iterable(it: Iterable[_S], /) -> SuccessTuple[_S]:
    """Create a [trcks.SuccessTuple][] object from an iterable.

    Args:
        it: The iterable to create
            the [trcks.SuccessTuple][] from.

    Returns:
        The [trcks.SuccessTuple][] created from the iterable.

    Examples:
        >>> from trcks.fp.monads import result_tuple as rt
        >>> rt.construct_successes_from_iterable((1, 2))
        ('success', (1, 2))
    """
    return r.construct_success(tuple(it))

construct_successes_from_tuple(tpl)

Deprecated alias for trcks.fp.monads.result_tuple.construct_successes_from_iterable.

Source code in src/trcks/fp/monads/result_tuple.py
156
157
158
159
160
161
@deprecated("Use construct_successes_from_iterable instead")
def construct_successes_from_tuple(tpl: tuple[_S, ...], /) -> SuccessTuple[_S]:
    """Deprecated alias for
    [trcks.fp.monads.result_tuple.construct_successes_from_iterable][].
    """
    return construct_successes_from_iterable(tpl)  # pragma: no cover

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

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

trcks.SuccessTuple values are left unchanged.

Parameters:

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

    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:

>>> from collections.abc import Callable
>>> from trcks import ResultTuple
>>> from trcks.fp.monads import result_tuple as rt
>>> def _add_prefix(description: str) -> str:
...     return f"err: {description}"
...
>>> add_prefix: Callable[
...     [ResultTuple[str, int]], ResultTuple[str, int]
... ] = rt.map_failure(_add_prefix)
>>> add_prefix(("failure", "not found"))
('failure', 'err: not found')
>>> add_prefix(("success", (1, 2)))
('success', (1, 2))
Source code in src/trcks/fp/_monads/result_tuple.py
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
153
154
155
156
157
158
159
160
161
162
163
164
165
def map_failure(
    callable_: Callable[Concatenate[_F1, _P], _F2],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_F1, _S1]], ResultTuple[_F2, _S1]]:
    """Create function that maps [trcks.Failure][] values to [trcks.Failure][] values.

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

    Args:
        callable_: 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.Failure][] values
            according to the given function and
            leaves [trcks.SuccessTuple][] values unchanged.

    Examples:
        >>> from collections.abc import Callable
        >>> from trcks import ResultTuple
        >>> from trcks.fp.monads import result_tuple as rt
        >>> def _add_prefix(description: str) -> str:
        ...     return f"err: {description}"
        ...
        >>> add_prefix: Callable[
        ...     [ResultTuple[str, int]], ResultTuple[str, int]
        ... ] = rt.map_failure(_add_prefix)
        >>> add_prefix(("failure", "not found"))
        ('failure', 'err: not found')
        >>> add_prefix(("success", (1, 2)))
        ('success', (1, 2))
    """
    return r.map_failure(callable_, *args, **kwargs)

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

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

trcks.Success values are left unchanged.

Parameters:

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

    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.fp.monads import awaitable_result_tuple as art
>>> from trcks.fp.monads import result_tuple as rt
>>> async def prefix_slowly(e: str) -> str:
...     await asyncio.sleep(0.001)
...     return f"err: {e}"
...
>>> add_prefix_to_failure = rt.map_failure_to_awaitable(prefix_slowly)
>>> a_r_tpl_1 = add_prefix_to_failure(("failure", "not found"))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
('failure', 'err: not found')
>>> a_r_tpl_2 = add_prefix_to_failure(("success", (1, 2)))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
('success', (1, 2))
Source code in src/trcks/fp/monads/result_tuple.py
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
195
196
197
198
199
200
201
202
203
204
205
206
def map_failure_to_awaitable(
    callable_: Callable[Concatenate[_F1, _P], Awaitable[_F2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_F1, _S1]], AwaitableResultTuple[_F2, _S1]]:
    """Create function that maps [trcks.Failure][] values
    to [trcks.AwaitableFailure][] 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 [trcks.AwaitableFailure][] values
            according to the given asynchronous function and
            leaves [trcks.Success][] values unchanged.

    Examples:
        >>> import asyncio
        >>> from trcks.fp.monads import awaitable_result_tuple as art
        >>> from trcks.fp.monads import result_tuple as rt
        >>> async def prefix_slowly(e: str) -> str:
        ...     await asyncio.sleep(0.001)
        ...     return f"err: {e}"
        ...
        >>> add_prefix_to_failure = rt.map_failure_to_awaitable(prefix_slowly)
        >>> a_r_tpl_1 = add_prefix_to_failure(("failure", "not found"))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
        ('failure', 'err: not found')
        >>> a_r_tpl_2 = add_prefix_to_failure(("success", (1, 2)))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
        ('success', (1, 2))
    """
    return compose(
        art.construct_from_result_iterable,
        art.map_failure_to_awaitable(callable_, *args, **kwargs),
    )

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

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

trcks.Success values are left unchanged.

Parameters:

Returns:

Examples:

>>> import asyncio
>>> from trcks.fp.monads import awaitable_result_tuple as art
>>> from trcks.fp.monads import result_tuple as rt
>>> async def slowly_recover_from_failure(description: str) -> list[int]:
...     await asyncio.sleep(0.001)
...     if description == "not found":
...         return [0]
...     return []
...
>>> recover_from_failure = rt.map_failure_to_awaitable_iterable(
...     slowly_recover_from_failure
... )
>>> a_r_tpl_1 = recover_from_failure(("failure", "not found"))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
('success', (0,))
>>> a_r_tpl_2 = recover_from_failure(("success", (1, 2)))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
('success', (1, 2))
Source code in src/trcks/fp/monads/result_tuple.py
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
258
259
260
261
262
263
264
265
266
def map_failure_to_awaitable_iterable(
    callable_: Callable[Concatenate[_F1, _P], AwaitableIterable[_S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[
    [ResultTuple[_F1, _S1]],
    Awaitable[SuccessTuple[_S1] | SuccessTuple[_S2]],
]:
    """Create function that maps [trcks.Failure][] values
    to new [trcks.AwaitableSuccessTuple][] values.

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

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

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

    Examples:
        >>> import asyncio
        >>> from trcks.fp.monads import awaitable_result_tuple as art
        >>> from trcks.fp.monads import result_tuple as rt
        >>> async def slowly_recover_from_failure(description: str) -> list[int]:
        ...     await asyncio.sleep(0.001)
        ...     if description == "not found":
        ...         return [0]
        ...     return []
        ...
        >>> recover_from_failure = rt.map_failure_to_awaitable_iterable(
        ...     slowly_recover_from_failure
        ... )
        >>> a_r_tpl_1 = recover_from_failure(("failure", "not found"))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
        ('success', (0,))
        >>> a_r_tpl_2 = recover_from_failure(("success", (1, 2)))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
        ('success', (1, 2))
    """
    c: tuple[
        Callable[[ResultTuple[_F1, _S1]], AwaitableResultTuple[_F1, _S1]],
        Callable[
            [AwaitableResultTuple[_F1, _S1]],
            Awaitable[SuccessTuple[_S1] | SuccessTuple[_S2]],
        ],
    ] = (
        art.construct_from_result_iterable,
        art.map_failure_to_awaitable_iterable(callable_, *args, **kwargs),
    )
    return compose(*c)

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

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

trcks.Success values are left unchanged.

Parameters:

  • callable_ (Callable[Concatenate[_F1, _P], AwaitableResult[_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 Result
>>> from trcks.fp.monads import awaitable_result_tuple as art
>>> from trcks.fp.monads import result_tuple as rt
>>> async def slowly_recover_from_not_found(e: str) -> Result[str, int]:
...     await asyncio.sleep(0.001)
...     if e == "not found":
...         return "success", 0
...     return "failure", e
...
>>> recover_from_failure = rt.map_failure_to_awaitable_result(
...     slowly_recover_from_not_found
... )
>>> a_r_tpl_1 = recover_from_failure(("failure", "not found"))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
('success', (0,))
>>> a_r_tpl_2 = recover_from_failure(("failure", "fatal"))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
('failure', 'fatal')
>>> a_r_tpl_3 = recover_from_failure(("success", (1, 2)))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_3))
('success', (1, 2))
Source code in src/trcks/fp/monads/result_tuple.py
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
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
def map_failure_to_awaitable_result(
    callable_: Callable[Concatenate[_F1, _P], AwaitableResult[_F2, _S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_F1, _S1]], AwaitableResultTuple[_F2, _S1 | _S2]]:
    """Create function that maps [trcks.Failure][] values
    to [trcks.AwaitableResult][] 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 [trcks.AwaitableFailure][] and [trcks.AwaitableSuccess][] values
            according to the given asynchronous function and
            leaves [trcks.Success][] values unchanged.

    Examples:
        >>> import asyncio
        >>> from trcks import Result
        >>> from trcks.fp.monads import awaitable_result_tuple as art
        >>> from trcks.fp.monads import result_tuple as rt
        >>> async def slowly_recover_from_not_found(e: str) -> Result[str, int]:
        ...     await asyncio.sleep(0.001)
        ...     if e == "not found":
        ...         return "success", 0
        ...     return "failure", e
        ...
        >>> recover_from_failure = rt.map_failure_to_awaitable_result(
        ...     slowly_recover_from_not_found
        ... )
        >>> a_r_tpl_1 = recover_from_failure(("failure", "not found"))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
        ('success', (0,))
        >>> a_r_tpl_2 = recover_from_failure(("failure", "fatal"))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
        ('failure', 'fatal')
        >>> a_r_tpl_3 = recover_from_failure(("success", (1, 2)))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_3))
        ('success', (1, 2))
    """
    return compose(
        art.construct_from_result_iterable,
        art.map_failure_to_awaitable_result(callable_, *args, **kwargs),
    )

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_tuple as art
>>> from trcks.fp.monads import result_tuple as rt
>>> async def recover(e: str) -> AwaitableResultTuple[str, int]:
...     await asyncio.sleep(0.001)
...     if e == "not found":
...         return "success", (0, 1)
...     return "failure", e
...
>>> recover_from_failure = rt.map_failure_to_awaitable_result_iterable(
...     recover
... )
>>> a_r_tpl_1 = recover_from_failure(("failure", "not found"))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
('success', (0, 1))
>>> a_r_tpl_2 = recover_from_failure(("success", (1, 2)))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
('success', (1, 2))
Source code in src/trcks/fp/monads/result_tuple.py
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
386
387
388
def map_failure_to_awaitable_result_iterable(
    callable_: Callable[Concatenate[_F1, _P], AwaitableResultIterable[_F2, _S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_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_tuple as art
        >>> from trcks.fp.monads import result_tuple as rt
        >>> async def recover(e: str) -> AwaitableResultTuple[str, int]:
        ...     await asyncio.sleep(0.001)
        ...     if e == "not found":
        ...         return "success", (0, 1)
        ...     return "failure", e
        ...
        >>> recover_from_failure = rt.map_failure_to_awaitable_result_iterable(
        ...     recover
        ... )
        >>> a_r_tpl_1 = recover_from_failure(("failure", "not found"))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
        ('success', (0, 1))
        >>> a_r_tpl_2 = recover_from_failure(("success", (1, 2)))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
        ('success', (1, 2))
    """
    return compose(
        art.construct_from_result_iterable,
        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.result_tuple.map_failure_to_awaitable_result_iterable.

Source code in src/trcks/fp/monads/result_tuple.py
391
392
393
394
395
396
397
398
399
400
401
402
403
@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[[ResultTuple[_F1, _S1]], AwaitableResultTuple[_F2, _S1 | _S2]]:
    """Deprecated alias for
    [trcks.fp.monads.result_tuple.map_failure_to_awaitable_result_iterable][].
    """
    return map_failure_to_awaitable_result_iterable(
        callable_, *args, **kwargs
    )  # pragma: no cover

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

Deprecated alias for trcks.fp.monads.result_tuple.map_failure_to_awaitable_iterable.

Source code in src/trcks/fp/monads/result_tuple.py
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
@deprecated("Use map_failure_to_awaitable_iterable instead")
def map_failure_to_awaitable_tuple(
    callable_: Callable[Concatenate[_F1, _P], AwaitableTuple[_S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[
    [ResultTuple[_F1, _S1]],
    Awaitable[SuccessTuple[_S1] | SuccessTuple[_S2]],
]:
    """Deprecated alias for
    [trcks.fp.monads.result_tuple.map_failure_to_awaitable_iterable][].
    """
    return map_failure_to_awaitable_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.SuccessTuple values are left unchanged.

Parameters:

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

    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:

>>> from collections.abc import Callable
>>> from trcks import ResultTuple, SuccessTuple
>>> from trcks.fp.monads import result_tuple as rt
>>> def _recover_from_not_found(description: str) -> tuple[int, ...]:
...     if description == "not found":
...         return (0,)
...     return ()
...
>>> recover_from_not_found: Callable[
...     [ResultTuple[str, int]], SuccessTuple[int]
... ] = rt.map_failure_to_iterable(_recover_from_not_found)
>>> recover_from_not_found(("failure", "not found"))
('success', (0,))
>>> recover_from_not_found(("failure", "not authorized"))
('success', ())
>>> recover_from_not_found(("success", (1, 2)))
('success', (1, 2))
Source code in src/trcks/fp/_monads/result_tuple.py
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
195
196
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
def map_failure_to_iterable(
    callable_: Callable[Concatenate[_F1, _P], Iterable[_S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_F1, _S1]], SuccessTuple[_S1] | SuccessTuple[_S2]]:
    """Create function that maps [trcks.Failure][] values
    to homogeneous [tuple][]s.

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

    Args:
        callable_: 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 homogeneous [tuple][]s wrapped
            in a [trcks.Success][] according to the given function and
            leaves [trcks.SuccessTuple][] values unchanged.

    Examples:
        >>> from collections.abc import Callable
        >>> from trcks import ResultTuple, SuccessTuple
        >>> from trcks.fp.monads import result_tuple as rt
        >>> def _recover_from_not_found(description: str) -> tuple[int, ...]:
        ...     if description == "not found":
        ...         return (0,)
        ...     return ()
        ...
        >>> recover_from_not_found: Callable[
        ...     [ResultTuple[str, int]], SuccessTuple[int]
        ... ] = rt.map_failure_to_iterable(_recover_from_not_found)
        >>> recover_from_not_found(("failure", "not found"))
        ('success', (0,))
        >>> recover_from_not_found(("failure", "not authorized"))
        ('success', ())
        >>> recover_from_not_found(("success", (1, 2)))
        ('success', (1, 2))
    """

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

    return mapped_callable

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

Create function that maps trcks.Failure values to trcks.Failure and trcks.Success values.

trcks.SuccessTuple values are left unchanged.

Parameters:

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

    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:

>>> from collections.abc import Callable
>>> from trcks import ResultTuple
>>> from trcks.fp.monads import result_tuple as rt
>>> def _recover_from_not_found(description: str) -> Result[str, int]:
...     if description == "not found":
...         return "success", 0
...     return "failure", description
...
>>> recover_from_not_found: Callable[
...     [ResultTuple[str, int]], ResultTuple[str, int]
... ] = rt.map_failure_to_result(_recover_from_not_found)
>>> recover_from_not_found(("failure", "not found"))
('success', (0,))
>>> recover_from_not_found(("failure", "not authorized"))
('failure', 'not authorized')
>>> recover_from_not_found(("success", (1, 2)))
('success', (1, 2))
Source code in src/trcks/fp/_monads/result_tuple.py
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
def map_failure_to_result(
    callable_: Callable[Concatenate[_F1, _P], Result[_F2, _S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_F1, _S1]], Result[_F2, tuple[_S1, ...] | tuple[_S2, ...]]]:
    """Create function that maps [trcks.Failure][] values
    to [trcks.Failure][] and [trcks.Success][] values.

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

    Args:
        callable_: 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.Failure][] and [trcks.Success][]
            values according to the given function and
            leaves [trcks.SuccessTuple][] values unchanged.

    Examples:
        >>> from collections.abc import Callable
        >>> from trcks import ResultTuple
        >>> from trcks.fp.monads import result_tuple as rt
        >>> def _recover_from_not_found(description: str) -> Result[str, int]:
        ...     if description == "not found":
        ...         return "success", 0
        ...     return "failure", description
        ...
        >>> recover_from_not_found: Callable[
        ...     [ResultTuple[str, int]], ResultTuple[str, int]
        ... ] = rt.map_failure_to_result(_recover_from_not_found)
        >>> recover_from_not_found(("failure", "not found"))
        ('success', (0,))
        >>> recover_from_not_found(("failure", "not authorized"))
        ('failure', 'not authorized')
        >>> recover_from_not_found(("success", (1, 2)))
        ('success', (1, 2))
    """
    return map_failure_to_result_iterable(
        compose(callable_, construct_from_result), *args, **kwargs
    )

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

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

trcks.SuccessTuple values are left unchanged.

Parameters:

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

    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:

>>> from collections.abc import Callable
>>> from trcks import ResultTuple
>>> from trcks.fp.monads import result_tuple as rt
>>> def _recover_from_not_found(description: str) -> ResultTuple[str, int]:
...     if description == "not found":
...         return "success", (0,)
...     return "failure", description
...
>>> recover_from_not_found: Callable[
...     [ResultTuple[str, int]], ResultTuple[str, int]
... ] = rt.map_failure_to_result_iterable(_recover_from_not_found)
>>> recover_from_not_found(("failure", "not found"))
('success', (0,))
>>> recover_from_not_found(("failure", "not authorized"))
('failure', 'not authorized')
>>> recover_from_not_found(("success", (1, 2)))
('success', (1, 2))
Source code in src/trcks/fp/_monads/result_tuple.py
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
310
311
312
313
314
315
316
317
318
def map_failure_to_result_iterable(
    callable_: Callable[Concatenate[_F1, _P], ResultIterable[_F2, _S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_F1, _S1]], Result[_F2, tuple[_S1, ...] | tuple[_S2, ...]]]:
    """Create function that maps [trcks.Failure][] values
    to new [trcks.ResultTuple][] values.

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

    Args:
        callable_: 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.SuccessTuple][] values unchanged.

    Examples:
        >>> from collections.abc import Callable
        >>> from trcks import ResultTuple
        >>> from trcks.fp.monads import result_tuple as rt
        >>> def _recover_from_not_found(description: str) -> ResultTuple[str, int]:
        ...     if description == "not found":
        ...         return "success", (0,)
        ...     return "failure", description
        ...
        >>> recover_from_not_found: Callable[
        ...     [ResultTuple[str, int]], ResultTuple[str, int]
        ... ] = rt.map_failure_to_result_iterable(_recover_from_not_found)
        >>> recover_from_not_found(("failure", "not found"))
        ('success', (0,))
        >>> recover_from_not_found(("failure", "not authorized"))
        ('failure', 'not authorized')
        >>> recover_from_not_found(("success", (1, 2)))
        ('success', (1, 2))
    """
    return r.map_failure_to_result(
        compose(callable_, construct_from_result_iterable), *args, **kwargs
    )

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

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

Source code in src/trcks/fp/monads/result_tuple.py
406
407
408
409
410
411
412
413
414
415
416
417
418
@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[[ResultTuple[_F1, _S1]], Result[_F2, tuple[_S1, ...] | tuple[_S2, ...]]]:
    """Deprecated alias for
    [trcks.fp.monads.result_tuple.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.result_tuple.map_failure_to_iterable.

Source code in src/trcks/fp/monads/result_tuple.py
421
422
423
424
425
426
427
428
429
@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[[ResultTuple[_F1, _S1]], SuccessTuple[_S1] | SuccessTuple[_S2]]:
    """Deprecated alias for [trcks.fp.monads.result_tuple.map_failure_to_iterable][]."""
    return map_failure_to_iterable(callable_, *args, **kwargs)  # pragma: no cover

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

Create function that maps each element of a trcks.SuccessTuple to a new element.

trcks.Failure values are left unchanged.

Parameters:

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

    Function to apply to each element of the trcks.SuccessTuple.

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

    Positional arguments to be passed to callable_.

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

    Keyword arguments to be passed to callable_.

Returns:

Examples:

>>> from collections.abc import Callable
>>> from trcks import ResultTuple
>>> from trcks.fp.monads import result_tuple as rt
>>> def _double_integer(n: int) -> int:
...     return n * 2
...
>>> double_integers: Callable[
...     [ResultTuple[str, int]], ResultTuple[str, int]
... ] = rt.map_successes(_double_integer)
>>> double_integers(("success", (1, 2, 3)))
('success', (2, 4, 6))
>>> double_integers(("failure", "not found"))
('failure', 'not found')
Source code in src/trcks/fp/_monads/result_tuple.py
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
359
def map_successes(
    callable_: Callable[Concatenate[_S1, _P], _S2],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_F1, _S1]], ResultTuple[_F1, _S2]]:
    """Create function that maps each element of a [trcks.SuccessTuple][]
    to a new element.

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

    Args:
        callable_: Function to apply to each element of the [trcks.SuccessTuple][].
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Leaves [trcks.Failure][] values unchanged and
            maps each element of a [trcks.SuccessTuple][] to a new element
            according to the given function.

    Examples:
        >>> from collections.abc import Callable
        >>> from trcks import ResultTuple
        >>> from trcks.fp.monads import result_tuple as rt
        >>> def _double_integer(n: int) -> int:
        ...     return n * 2
        ...
        >>> double_integers: Callable[
        ...     [ResultTuple[str, int]], ResultTuple[str, int]
        ... ] = rt.map_successes(_double_integer)
        >>> double_integers(("success", (1, 2, 3)))
        ('success', (2, 4, 6))
        >>> double_integers(("failure", "not found"))
        ('failure', 'not found')
    """
    return r.map_success(t.map_(callable_, *args, **kwargs))

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

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

trcks.Failure values are left unchanged.

Parameters:

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

    Asynchronous function to apply to each success element.

  • *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_tuple as art
>>> from trcks.fp.monads import result_tuple as rt
>>> async def double_slowly(n: int) -> int:
...     await asyncio.sleep(0.001)
...     return n * 2
...
>>> double_successes = rt.map_successes_to_awaitable(double_slowly)
>>> a_r_tpl_1 = double_successes(("failure", "not found"))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
('failure', 'not found')
>>> a_r_tpl_2 = double_successes(("success", (1, 2)))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
('success', (2, 4))
Source code in src/trcks/fp/monads/result_tuple.py
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
def map_successes_to_awaitable(
    callable_: Callable[Concatenate[_S1, _P], Awaitable[_S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_F1, _S1]], AwaitableResultTuple[_F1, _S2]]:
    """Create function that maps [trcks.Success][] values
    to [trcks.AwaitableSuccess][] values.

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

    Args:
        callable_: Asynchronous function to apply to each success element.
        *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.AwaitableSuccess][]
            values according to the given asynchronous function.

    Examples:
        >>> import asyncio
        >>> from trcks.fp.monads import awaitable_result_tuple as art
        >>> from trcks.fp.monads import result_tuple as rt
        >>> async def double_slowly(n: int) -> int:
        ...     await asyncio.sleep(0.001)
        ...     return n * 2
        ...
        >>> double_successes = rt.map_successes_to_awaitable(double_slowly)
        >>> a_r_tpl_1 = double_successes(("failure", "not found"))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
        ('failure', 'not found')
        >>> a_r_tpl_2 = double_successes(("success", (1, 2)))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
        ('success', (2, 4))
    """
    return compose(
        art.construct_from_result_iterable,
        art.map_successes_to_awaitable(callable_, *args, **kwargs),
    )

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

Create function that maps trcks.Success values to homogeneous tuples and flattens the result.

trcks.Failure values are left unchanged.

Parameters:

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

    Asynchronous function to apply to each success element, returning an trcks.AwaitableIterable.

  • *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_tuple as art
>>> from trcks.fp.monads import result_tuple as rt
>>> async def slowly_duplicate_integer(n: int) -> tuple[int, int]:
...     await asyncio.sleep(0.001)
...     return n, n
...
>>> duplicate_successes = rt.map_successes_to_awaitable_iterable(
...     slowly_duplicate_integer
... )
>>> a_r_tpl_1 = duplicate_successes(("success", (1, 2)))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
('success', (1, 1, 2, 2))
>>> a_r_tpl_2 = duplicate_successes(("failure", "oops"))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
('failure', 'oops')
Source code in src/trcks/fp/monads/result_tuple.py
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_successes_to_awaitable_iterable(
    callable_: Callable[Concatenate[_S1, _P], AwaitableIterable[_S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_F1, _S1]], AwaitableResultTuple[_F1, _S2]]:
    """Create function that maps [trcks.Success][] values to homogeneous [tuple][]s
    and flattens the result.

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

    Args:
        callable_: Asynchronous function to apply to each success element,
            returning an [trcks.AwaitableIterable][].
        *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 flattened
            [trcks.AwaitableSuccessTuple][] values
            according to the given asynchronous function.

    Examples:
        >>> import asyncio
        >>> from trcks.fp.monads import awaitable_result_tuple as art
        >>> from trcks.fp.monads import result_tuple as rt
        >>> async def slowly_duplicate_integer(n: int) -> tuple[int, int]:
        ...     await asyncio.sleep(0.001)
        ...     return n, n
        ...
        >>> duplicate_successes = rt.map_successes_to_awaitable_iterable(
        ...     slowly_duplicate_integer
        ... )
        >>> a_r_tpl_1 = duplicate_successes(("success", (1, 2)))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
        ('success', (1, 1, 2, 2))
        >>> a_r_tpl_2 = duplicate_successes(("failure", "oops"))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
        ('failure', 'oops')
    """
    return compose(
        art.construct_from_result_iterable,
        art.map_successes_to_awaitable_iterable(callable_, *args, **kwargs),
    )

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

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

trcks.Failure values are left unchanged.

Parameters:

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

    Asynchronous function to apply to each success element.

  • *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_tuple as art
>>> from trcks.fp.monads import result_tuple as rt
>>> async def slowly_double_if_positive(n: int) -> Result[str, int]:
...     await asyncio.sleep(0.001)
...     if n > 0:
...         return "success", n * 2
...     return "failure", "negative"
...
>>> double_successes = rt.map_successes_to_awaitable_result(
...     slowly_double_if_positive
... )
>>> a_r_tpl_1 = double_successes(("failure", "not found"))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
('failure', 'not found')
>>> a_r_tpl_2 = double_successes(("success", (1, 2)))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
('success', (2, 4))
Source code in src/trcks/fp/monads/result_tuple.py
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
def map_successes_to_awaitable_result(
    callable_: Callable[Concatenate[_S1, _P], AwaitableResult[_F2, _S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_F1, _S1]], AwaitableResultTuple[_F1 | _F2, _S2]]:
    """Create function that maps [trcks.Success][] values
    to [trcks.AwaitableResult][] values.

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

    Args:
        callable_: Asynchronous function to apply to each success element.
        *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 [trcks.AwaitableFailure][] and [trcks.AwaitableSuccess][] values
            according to the given asynchronous function.

    Examples:
        >>> import asyncio
        >>> from trcks import Result
        >>> from trcks.fp.monads import awaitable_result_tuple as art
        >>> from trcks.fp.monads import result_tuple as rt
        >>> async def slowly_double_if_positive(n: int) -> Result[str, int]:
        ...     await asyncio.sleep(0.001)
        ...     if n > 0:
        ...         return "success", n * 2
        ...     return "failure", "negative"
        ...
        >>> double_successes = rt.map_successes_to_awaitable_result(
        ...     slowly_double_if_positive
        ... )
        >>> a_r_tpl_1 = double_successes(("failure", "not found"))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
        ('failure', 'not found')
        >>> a_r_tpl_2 = double_successes(("success", (1, 2)))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
        ('success', (2, 4))
    """
    return compose(
        art.construct_from_result_iterable,
        art.map_successes_to_awaitable_result(callable_, *args, **kwargs),
    )

map_successes_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 each success element.

  • *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_tuple as art
>>> from trcks.fp.monads import result_tuple as rt
>>> async def slowly_expand(n: int) -> AwaitableResultTuple[str, int]:
...     await asyncio.sleep(0.001)
...     if n > 0:
...         return "success", (n, -n)
...     return "failure", "negative"
...
>>> expand_successes = rt.map_successes_to_awaitable_result_iterable(
...     slowly_expand
... )
>>> a_r_tpl_1 = expand_successes(("failure", "not found"))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
('failure', 'not found')
>>> a_r_tpl_2 = expand_successes(("success", (1, 2)))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
('success', (1, -1, 2, -2))
Source code in src/trcks/fp/monads/result_tuple.py
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
def map_successes_to_awaitable_result_iterable(
    callable_: Callable[Concatenate[_S1, _P], AwaitableResultIterable[_F2, _S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_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 each success element.
        *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_tuple as art
        >>> from trcks.fp.monads import result_tuple as rt
        >>> async def slowly_expand(n: int) -> AwaitableResultTuple[str, int]:
        ...     await asyncio.sleep(0.001)
        ...     if n > 0:
        ...         return "success", (n, -n)
        ...     return "failure", "negative"
        ...
        >>> expand_successes = rt.map_successes_to_awaitable_result_iterable(
        ...     slowly_expand
        ... )
        >>> a_r_tpl_1 = expand_successes(("failure", "not found"))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
        ('failure', 'not found')
        >>> a_r_tpl_2 = expand_successes(("success", (1, 2)))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
        ('success', (1, -1, 2, -2))
    """
    return compose(
        art.construct_from_result_iterable,
        art.map_successes_to_awaitable_result_iterable(callable_, *args, **kwargs),
    )

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

Deprecated alias for trcks.fp.monads.result_tuple.map_successes_to_awaitable_result_iterable.

Source code in src/trcks/fp/monads/result_tuple.py
642
643
644
645
646
647
648
649
650
651
652
653
654
@deprecated("Use map_successes_to_awaitable_result_iterable instead")
def map_successes_to_awaitable_result_tuple(
    callable_: Callable[Concatenate[_S1, _P], AwaitableResultTuple[_F2, _S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_F1, _S1]], AwaitableResultTuple[_F1 | _F2, _S2]]:
    """Deprecated alias for
    [trcks.fp.monads.result_tuple.map_successes_to_awaitable_result_iterable][].
    """
    return map_successes_to_awaitable_result_iterable(
        callable_, *args, **kwargs
    )  # pragma: no cover

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

Deprecated alias for trcks.fp.monads.result_tuple.map_successes_to_awaitable_iterable.

Source code in src/trcks/fp/monads/result_tuple.py
526
527
528
529
530
531
532
533
534
535
536
537
538
@deprecated("Use map_successes_to_awaitable_iterable instead")
def map_successes_to_awaitable_tuple(
    callable_: Callable[Concatenate[_S1, _P], AwaitableTuple[_S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_F1, _S1]], AwaitableResultTuple[_F1, _S2]]:
    """Deprecated alias for
    [trcks.fp.monads.result_tuple.map_successes_to_awaitable_iterable][].
    """
    return map_successes_to_awaitable_iterable(
        callable_, *args, **kwargs
    )  # pragma: no cover

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

Create function that maps each element of a trcks.SuccessTuple to a collections.abc.Iterable.

trcks.Failure values are left unchanged.

Parameters:

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

    Function to apply to each element of the trcks.SuccessTuple.

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

    Positional arguments to be passed to callable_.

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

    Keyword arguments to be passed to callable_.

Returns:

Examples:

>>> from collections.abc import Callable
>>> from trcks import ResultTuple
>>> from trcks.fp.monads import result_tuple as rt
>>> def _duplicate_integer(n: int) -> tuple[int, int]:
...     return n, n
...
>>> duplicate_integers: Callable[
...     [ResultTuple[str, int]], ResultTuple[str, int]
... ] = rt.map_successes_to_iterable(_duplicate_integer)
>>> duplicate_integers(("success", (1, 2)))
('success', (1, 1, 2, 2))
>>> duplicate_integers(("failure", "not found"))
('failure', 'not found')
Source code in src/trcks/fp/_monads/result_tuple.py
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
def map_successes_to_iterable(
    callable_: Callable[Concatenate[_S1, _P], Iterable[_S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_F1, _S1]], ResultTuple[_F1, _S2]]:
    """Create function that maps each element of a [trcks.SuccessTuple][]
    to a [collections.abc.Iterable][].

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

    Args:
        callable_: Function to apply to each element of the [trcks.SuccessTuple][].
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Leaves [trcks.Failure][] values unchanged and
            flat-maps each element of a [trcks.SuccessTuple][] to a
            [collections.abc.Iterable][] according to the given function.

    Examples:
        >>> from collections.abc import Callable
        >>> from trcks import ResultTuple
        >>> from trcks.fp.monads import result_tuple as rt
        >>> def _duplicate_integer(n: int) -> tuple[int, int]:
        ...     return n, n
        ...
        >>> duplicate_integers: Callable[
        ...     [ResultTuple[str, int]], ResultTuple[str, int]
        ... ] = rt.map_successes_to_iterable(_duplicate_integer)
        >>> duplicate_integers(("success", (1, 2)))
        ('success', (1, 1, 2, 2))
        >>> duplicate_integers(("failure", "not found"))
        ('failure', 'not found')
    """
    return r.map_success(t.map_to_iterable(callable_, *args, **kwargs))

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

Create function that maps each element of a trcks.SuccessTuple to trcks.Failure and trcks.Success values.

trcks.Failure values are left unchanged.

Parameters:

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

    Function to apply to each element of the trcks.SuccessTuple.

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

    Positional arguments to be passed to callable_.

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

    Keyword arguments to be passed to callable_.

Returns:

Examples:

>>> from collections.abc import Callable
>>> from trcks import Result, ResultTuple
>>> from trcks.fp.monads import result_tuple as rt
>>> def _double_if_positive(n: int) -> Result[str, int]:
...     if n > 0:
...         return "success", n * 2
...     return "failure", "not positive"
...
>>> double_if_positive: Callable[
...     [ResultTuple[str, int]], ResultTuple[str, int]
... ] = rt.map_successes_to_result(_double_if_positive)
>>> double_if_positive(("success", (1, 2)))
('success', (2, 4))
>>> double_if_positive(("success", (1, -1, 2)))
('failure', 'not positive')
>>> double_if_positive(("failure", "oops"))
('failure', 'oops')
Source code in src/trcks/fp/_monads/result_tuple.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
445
446
447
448
def map_successes_to_result(
    callable_: Callable[Concatenate[_S1, _P], Result[_F2, _S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_F1, _S1]], ResultTuple[_F1 | _F2, _S2]]:
    """Create function that maps each element of a [trcks.SuccessTuple][]
    to [trcks.Failure][] and [trcks.Success][] values.

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

    Args:
        callable_: Function to apply to each element of the [trcks.SuccessTuple][].
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Leaves [trcks.Failure][] values unchanged and
            maps each element of a [trcks.SuccessTuple][] to
            [trcks.Failure][] and [trcks.Success][] values according to the given
            function, returning the first [trcks.Failure][] encountered, if any.

    Examples:
        >>> from collections.abc import Callable
        >>> from trcks import Result, ResultTuple
        >>> from trcks.fp.monads import result_tuple as rt
        >>> def _double_if_positive(n: int) -> Result[str, int]:
        ...     if n > 0:
        ...         return "success", n * 2
        ...     return "failure", "not positive"
        ...
        >>> double_if_positive: Callable[
        ...     [ResultTuple[str, int]], ResultTuple[str, int]
        ... ] = rt.map_successes_to_result(_double_if_positive)
        >>> double_if_positive(("success", (1, 2)))
        ('success', (2, 4))
        >>> double_if_positive(("success", (1, -1, 2)))
        ('failure', 'not positive')
        >>> double_if_positive(("failure", "oops"))
        ('failure', 'oops')
    """
    return map_successes_to_result_iterable(
        compose(callable_, construct_from_result), *args, **kwargs
    )

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

Create function that maps each element of a trcks.SuccessTuple to new trcks.ResultTuple values.

trcks.Failure values are left unchanged.

Parameters:

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

    Function to apply to each element of the trcks.SuccessTuple.

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

    Positional arguments to be passed to callable_.

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

    Keyword arguments to be passed to callable_.

Returns:

Examples:

>>> from collections.abc import Callable
>>> from trcks import ResultTuple
>>> from trcks.fp.monads import result_tuple as rt
>>> def _duplicate_if_positive(n: int) -> ResultTuple[str, int]:
...     if n > 0:
...         return "success", (n, n)
...     return "failure", "not positive"
...
>>> duplicate_if_positive: Callable[
...     [ResultTuple[str, int]], ResultTuple[str, int]
... ] = rt.map_successes_to_result_iterable(_duplicate_if_positive)
>>> duplicate_if_positive(("success", (1, 2)))
('success', (1, 1, 2, 2))
>>> duplicate_if_positive(("success", (1, -1, 2)))
('failure', 'not positive')
>>> duplicate_if_positive(("failure", "oops"))
('failure', 'oops')
Source code in src/trcks/fp/_monads/result_tuple.py
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
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
def map_successes_to_result_iterable(
    callable_: Callable[Concatenate[_S1, _P], ResultIterable[_F2, _S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_F1, _S1]], ResultTuple[_F1 | _F2, _S2]]:
    """Create function that maps each element of a [trcks.SuccessTuple][]
    to new [trcks.ResultTuple][] values.

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

    Args:
        callable_: Function to apply to each element of the [trcks.SuccessTuple][].
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Leaves [trcks.Failure][] values unchanged and
            maps each element of a [trcks.SuccessTuple][] to new
            [trcks.ResultTuple][] values according to the given function,
            returning the first [trcks.Failure][] returned by `callable_`, if any.

    Examples:
        >>> from collections.abc import Callable
        >>> from trcks import ResultTuple
        >>> from trcks.fp.monads import result_tuple as rt
        >>> def _duplicate_if_positive(n: int) -> ResultTuple[str, int]:
        ...     if n > 0:
        ...         return "success", (n, n)
        ...     return "failure", "not positive"
        ...
        >>> duplicate_if_positive: Callable[
        ...     [ResultTuple[str, int]], ResultTuple[str, int]
        ... ] = rt.map_successes_to_result_iterable(_duplicate_if_positive)
        >>> duplicate_if_positive(("success", (1, 2)))
        ('success', (1, 1, 2, 2))
        >>> duplicate_if_positive(("success", (1, -1, 2)))
        ('failure', 'not positive')
        >>> duplicate_if_positive(("failure", "oops"))
        ('failure', 'oops')
    """

    def partially_mapped_callable(s1s: tuple[_S1, ...]) -> ResultTuple[_F2, _S2]:
        s2s = list[_S2]()
        for s1 in s1s:
            match callable_(s1, *args, **kwargs):
                case ("failure", _) as r_it:
                    return r_it
                case ("success", additional_s2s):
                    s2s.extend(additional_s2s)
                case _ as r_it:  # pragma: no cover
                    assert_type(r_it, Never)  # type: ignore[unreachable]  # pyright: ignore[reportUnreachable]
                    msg = f"{type(r_it).__name__!r} is not a valid ResultIterable"
                    raise TypeError(msg)
        return "success", tuple(s2s)

    def mapped_callable(r_tpl: ResultTuple[_F1, _S1]) -> ResultTuple[_F1 | _F2, _S2]:
        match r_tpl:
            case ("failure", _):
                return r_tpl
            case ("success", s1s):
                return partially_mapped_callable(s1s)
            case _:  # pragma: no cover
                assert_type(r_tpl, Never)  # type: ignore[unreachable]  # pyright: ignore[reportUnreachable]
                msg = f"{type(r_tpl).__name__!r} is not a valid ResultTuple"
                raise TypeError(msg)

    return mapped_callable

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

Deprecated alias for trcks.fp.monads.result_tuple.map_successes_to_result_iterable.

Source code in src/trcks/fp/monads/result_tuple.py
657
658
659
660
661
662
663
664
665
666
667
668
669
@deprecated("Use map_successes_to_result_iterable instead")
def map_successes_to_result_tuple(
    callable_: Callable[Concatenate[_S1, _P], ResultTuple[_F2, _S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_F1, _S1]], ResultTuple[_F1 | _F2, _S2]]:
    """Deprecated alias for
    [trcks.fp.monads.result_tuple.map_successes_to_result_iterable][].
    """
    return map_successes_to_result_iterable(
        callable_, *args, **kwargs
    )  # pragma: no cover

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

Deprecated alias for trcks.fp.monads.result_tuple.map_successes_to_iterable.

Source code in src/trcks/fp/monads/result_tuple.py
672
673
674
675
676
677
678
679
680
681
682
@deprecated("Use map_successes_to_iterable instead")
def map_successes_to_tuple(
    callable_: Callable[Concatenate[_S1, _P], tuple[_S2, ...]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_F1, _S1]], ResultTuple[_F1, _S2]]:
    """Deprecated alias for
    [trcks.fp.monads.result_tuple.map_successes_to_iterable][].
    """
    return map_successes_to_iterable(callable_, *args, **kwargs)  # pragma: no cover

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

Create function that applies a side effect to trcks.Failure values.

trcks.SuccessTuple values are passed on without side effects.

Parameters:

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

    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:

>>> from collections.abc import Callable
>>> from trcks import ResultTuple
>>> from trcks.fp.monads import result_tuple as rt
>>> def _log_error(description: str) -> None:
...     print(f"Error: {description}")
...
>>> log_error: Callable[
...     [ResultTuple[str, int]], ResultTuple[str, int]
... ] = rt.tap_failure(_log_error)
>>> log_error(("failure", "oops"))
Error: oops
('failure', 'oops')
>>> log_error(("success", (1,)))
('success', (1,))
Source code in src/trcks/fp/_monads/result_tuple.py
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
def tap_failure(
    callable_: Callable[Concatenate[_F1, _P], object],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_F1, _S1]], ResultTuple[_F1, _S1]]:
    """Create function that applies a side effect to [trcks.Failure][] values.

    [trcks.SuccessTuple][] 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 and
            returns the original [trcks.Failure][] value.
            Passes on [trcks.SuccessTuple][] values without side effects.

    Examples:
        >>> from collections.abc import Callable
        >>> from trcks import ResultTuple
        >>> from trcks.fp.monads import result_tuple as rt
        >>> def _log_error(description: str) -> None:
        ...     print(f"Error: {description}")
        ...
        >>> log_error: Callable[
        ...     [ResultTuple[str, int]], ResultTuple[str, int]
        ... ] = rt.tap_failure(_log_error)
        >>> log_error(("failure", "oops"))
        Error: oops
        ('failure', 'oops')
        >>> log_error(("success", (1,)))
        ('success', (1,))
    """
    return r.tap_failure(callable_, *args, **kwargs)

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

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

trcks.Success values are passed on without side effects.

Parameters:

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

    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.fp.monads import awaitable_result_tuple as art
>>> from trcks.fp.monads import result_tuple as rt
>>> async def log_slowly(e: str) -> None:
...     await asyncio.sleep(0.001)
...     print(f"Error: {e}")
...
>>> log_failure = rt.tap_failure_to_awaitable(log_slowly)
>>> a_r_tpl_1 = log_failure(("failure", "oops"))
>>> r_tpl_1 = asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
Error: oops
>>> r_tpl_1
('failure', 'oops')
>>> a_r_tpl_2 = log_failure(("success", (1,)))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
('success', (1,))
Source code in src/trcks/fp/monads/result_tuple.py
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
def tap_failure_to_awaitable(
    callable_: Callable[Concatenate[_F1, _P], Awaitable[object]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_F1, _S1]], AwaitableResultTuple[_F1, _S1]]:
    """Create function that applies an asynchronous side effect
    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 and
            returns the original [trcks.Failure][] value.
            Passes on [trcks.Success][] values without side effects.

    Examples:
        >>> import asyncio
        >>> from trcks.fp.monads import awaitable_result_tuple as art
        >>> from trcks.fp.monads import result_tuple as rt
        >>> async def log_slowly(e: str) -> None:
        ...     await asyncio.sleep(0.001)
        ...     print(f"Error: {e}")
        ...
        >>> log_failure = rt.tap_failure_to_awaitable(log_slowly)
        >>> a_r_tpl_1 = log_failure(("failure", "oops"))
        >>> r_tpl_1 = asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
        Error: oops
        >>> r_tpl_1
        ('failure', 'oops')
        >>> a_r_tpl_2 = log_failure(("success", (1,)))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
        ('success', (1,))
    """
    return compose(
        art.construct_from_result_iterable,
        art.tap_failure_to_awaitable(callable_, *args, **kwargs),
    )

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

Create function that applies an asynchronous side effect with return type trcks.AwaitableIterable 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_tuple as art
>>> from trcks.fp.monads import result_tuple as rt
>>> async def slowly_log_and_alert(description: str) -> tuple[None, None]:
...     await asyncio.sleep(0.001)
...     return (
...         print(f"Failure: {description}"),
...         print(f"Logged: {description}"),
...     )
...
>>> log_and_alert = rt.tap_failure_to_awaitable_iterable(
...     slowly_log_and_alert
... )
>>> a_r_tpl_1 = log_and_alert(("failure", "critical"))
>>> r_tpl_1 = asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
Failure: critical
Logged: critical
>>> r_tpl_1
('success', ('critical', 'critical'))
>>> a_r_tpl_2 = log_and_alert(("success", (1, 2)))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
('success', (1, 2))
Source code in src/trcks/fp/monads/result_tuple.py
732
733
734
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
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
790
791
792
793
794
795
def tap_failure_to_awaitable_iterable(
    callable_: Callable[Concatenate[_F1, _P], AwaitableIterable[object]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[
    [ResultTuple[_F1, _S1]],
    Awaitable[SuccessTuple[_F1] | SuccessTuple[_S1]],
]:
    """Create function that applies an asynchronous side effect
    with return type [trcks.AwaitableIterable][] 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,
            returning an [trcks.AwaitableIterable][].
        *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 [trcks.AwaitableIterable][] returned by the side effect.
            Passes on [trcks.Success][] values without side effects.

    Examples:
        >>> import asyncio
        >>> from trcks.fp.monads import awaitable_result_tuple as art
        >>> from trcks.fp.monads import result_tuple as rt
        >>> async def slowly_log_and_alert(description: str) -> tuple[None, None]:
        ...     await asyncio.sleep(0.001)
        ...     return (
        ...         print(f"Failure: {description}"),
        ...         print(f"Logged: {description}"),
        ...     )
        ...
        >>> log_and_alert = rt.tap_failure_to_awaitable_iterable(
        ...     slowly_log_and_alert
        ... )
        >>> a_r_tpl_1 = log_and_alert(("failure", "critical"))
        >>> r_tpl_1 = asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
        Failure: critical
        Logged: critical
        >>> r_tpl_1
        ('success', ('critical', 'critical'))
        >>> a_r_tpl_2 = log_and_alert(("success", (1, 2)))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
        ('success', (1, 2))
    """
    c: tuple[
        Callable[[ResultTuple[_F1, _S1]], AwaitableResultTuple[_F1, _S1]],
        Callable[
            [AwaitableResultTuple[_F1, _S1]],
            Awaitable[SuccessTuple[_F1] | SuccessTuple[_S1]],
        ],
    ] = (
        art.construct_from_result_iterable,
        art.tap_failure_to_awaitable_iterable(callable_, *args, **kwargs),
    )
    return compose(*c)

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

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

trcks.Success values are passed on without side effects.

Parameters:

  • callable_ (Callable[Concatenate[_F1, _P], AwaitableResult[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 Result
>>> from trcks.fp.monads import awaitable_result_tuple as art
>>> from trcks.fp.monads import result_tuple as rt
>>> async def recover(e: str) -> Result[object, int]:
...     await asyncio.sleep(0.001)
...     if e == "not found":
...         return "success", 0
...     return "failure", e
...
>>> recover_after_failure = rt.tap_failure_to_awaitable_result(recover)
>>> a_r_tpl_1 = recover_after_failure(("failure", "not found"))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
('success', (0,))
>>> a_r_tpl_2 = recover_after_failure(("success", (1,)))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
('success', (1,))
Source code in src/trcks/fp/monads/result_tuple.py
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
857
858
859
860
861
862
863
864
def tap_failure_to_awaitable_result(
    callable_: Callable[Concatenate[_F1, _P], AwaitableResult[object, _S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_F1, _S1]], AwaitableResultTuple[_F1, _S1 | _S2]]:
    """Create function that applies an asynchronous side effect
    with return type [trcks.AwaitableResult][] 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.Success][],
            *this* [trcks.Success][] is returned (wrapped as a tuple).
            Passes on [trcks.Success][] values without side effects.

    Examples:
        >>> import asyncio
        >>> from trcks import Result
        >>> from trcks.fp.monads import awaitable_result_tuple as art
        >>> from trcks.fp.monads import result_tuple as rt
        >>> async def recover(e: str) -> Result[object, int]:
        ...     await asyncio.sleep(0.001)
        ...     if e == "not found":
        ...         return "success", 0
        ...     return "failure", e
        ...
        >>> recover_after_failure = rt.tap_failure_to_awaitable_result(recover)
        >>> a_r_tpl_1 = recover_after_failure(("failure", "not found"))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
        ('success', (0,))
        >>> a_r_tpl_2 = recover_after_failure(("success", (1,)))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
        ('success', (1,))
    """
    return compose(
        art.construct_from_result_iterable,
        art.tap_failure_to_awaitable_result(callable_, *args, **kwargs),
    )

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_tuple as art
>>> from trcks.fp.monads import result_tuple as rt
>>> async def recover(e: str) -> AwaitableResultTuple[object, int]:
...     await asyncio.sleep(0.001)
...     if e == "not found":
...         return "success", (0, 1)
...     return "failure", e
...
>>> recover_after_failure = rt.tap_failure_to_awaitable_result_iterable(
...     recover
... )
>>> a_r_tpl_1 = recover_after_failure(("failure", "not found"))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
('success', (0, 1))
>>> a_r_tpl_2 = recover_after_failure(("success", (1,)))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
('success', (1,))
Source code in src/trcks/fp/monads/result_tuple.py
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
909
910
911
912
913
914
915
916
917
def tap_failure_to_awaitable_result_iterable(
    callable_: Callable[Concatenate[_F1, _P], AwaitableResultIterable[object, _S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_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 without side effects.

    Examples:
        >>> import asyncio
        >>> from trcks import AwaitableResultTuple
        >>> from trcks.fp.monads import awaitable_result_tuple as art
        >>> from trcks.fp.monads import result_tuple as rt
        >>> async def recover(e: str) -> AwaitableResultTuple[object, int]:
        ...     await asyncio.sleep(0.001)
        ...     if e == "not found":
        ...         return "success", (0, 1)
        ...     return "failure", e
        ...
        >>> recover_after_failure = rt.tap_failure_to_awaitable_result_iterable(
        ...     recover
        ... )
        >>> a_r_tpl_1 = recover_after_failure(("failure", "not found"))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
        ('success', (0, 1))
        >>> a_r_tpl_2 = recover_after_failure(("success", (1,)))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
        ('success', (1,))
    """
    return compose(
        art.construct_from_result_iterable,
        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.result_tuple.tap_failure_to_awaitable_result_iterable.

Source code in src/trcks/fp/monads/result_tuple.py
920
921
922
923
924
925
926
927
928
929
930
931
932
@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[[ResultTuple[_F1, _S1]], AwaitableResultTuple[_F1, _S1 | _S2]]:
    """Deprecated alias for
    [trcks.fp.monads.result_tuple.tap_failure_to_awaitable_result_iterable][].
    """
    return tap_failure_to_awaitable_result_iterable(
        callable_, *args, **kwargs
    )  # pragma: no cover

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

Deprecated alias for trcks.fp.monads.result_tuple.tap_failure_to_awaitable_iterable.

Source code in src/trcks/fp/monads/result_tuple.py
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
@deprecated("Use tap_failure_to_awaitable_iterable instead")
def tap_failure_to_awaitable_tuple(
    callable_: Callable[Concatenate[_F1, _P], AwaitableTuple[object]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[
    [ResultTuple[_F1, _S1]],
    Awaitable[SuccessTuple[_F1] | SuccessTuple[_S1]],
]:
    """Deprecated alias for
    [trcks.fp.monads.result_tuple.tap_failure_to_awaitable_iterable][].
    """
    return tap_failure_to_awaitable_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.SuccessTuple values are passed on without side effects.

Parameters:

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

    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:

>>> from collections.abc import Callable
>>> from trcks import ResultTuple, SuccessTuple
>>> from trcks.fp.monads import result_tuple as rt
>>> def _log_and_alert(description: str) -> tuple[None, None]:
...     return (
...         print(f"Error logged: {description}"),
...         print(f"Alert sent: {description}"),
...     )
...
>>> log_and_alert: Callable[
...     [ResultTuple[str, int]],
...     SuccessTuple[str] | SuccessTuple[int],
... ] = rt.tap_failure_to_iterable(_log_and_alert)
>>> log_and_alert(("failure", "critical"))
Error logged: critical
Alert sent: critical
('success', ('critical', 'critical'))
>>> log_and_alert(("success", (1, 2)))
('success', (1, 2))
Source code in src/trcks/fp/_monads/result_tuple.py
564
565
566
567
568
569
570
571
572
573
574
575
576
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
602
603
604
605
606
607
608
609
610
611
612
613
614
def tap_failure_to_iterable(
    callable_: Callable[Concatenate[_F1, _P], Iterable[object]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_F1, _S1]], SuccessTuple[_F1] | SuccessTuple[_S1]]:
    """Create function that applies a [collections.abc.Iterable][]-returning
    side effect to [trcks.Failure][] values.

    [trcks.SuccessTuple][] 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 and converts them
            to [trcks.SuccessTuple][] values containing the original failure
            repeated once per element in the [collections.abc.Iterable][] returned
            by the side effect.
            Passes on [trcks.SuccessTuple][] values without side effects.

    Examples:
        >>> from collections.abc import Callable
        >>> from trcks import ResultTuple, SuccessTuple
        >>> from trcks.fp.monads import result_tuple as rt
        >>> def _log_and_alert(description: str) -> tuple[None, None]:
        ...     return (
        ...         print(f"Error logged: {description}"),
        ...         print(f"Alert sent: {description}"),
        ...     )
        ...
        >>> log_and_alert: Callable[
        ...     [ResultTuple[str, int]],
        ...     SuccessTuple[str] | SuccessTuple[int],
        ... ] = rt.tap_failure_to_iterable(_log_and_alert)
        >>> log_and_alert(("failure", "critical"))
        Error logged: critical
        Alert sent: critical
        ('success', ('critical', 'critical'))
        >>> log_and_alert(("success", (1, 2)))
        ('success', (1, 2))
    """

    def tapped_callable(f1: _F1) -> tuple[_F1, ...]:
        return tuple(f1 for _s2 in callable_(f1, *args, **kwargs))

    return map_failure_to_iterable(tapped_callable)

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

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

trcks.SuccessTuple values are passed on without side effects.

Parameters:

  • callable_ (Callable[Concatenate[_F1, _P], Result[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:

>>> from collections.abc import Callable
>>> from trcks import Result, ResultTuple
>>> from trcks.fp.monads import result_tuple as rt
>>> def _recover_from_not_found(description: str) -> Result[None, int]:
...     if description == "not found":
...         return "success", 42
...     return "failure", None
>>> recover_from_not_found: Callable[
...     [ResultTuple[str, int]], Result[str, tuple[int, ...]]
... ] = rt.tap_failure_to_result(_recover_from_not_found)
>>> recover_from_not_found(("failure", "not found"))
('success', (42,))
>>> recover_from_not_found(("failure", "fatal"))
('failure', 'fatal')
>>> recover_from_not_found(("success", (1, 2)))
('success', (1, 2))
Source code in src/trcks/fp/_monads/result_tuple.py
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
652
653
654
655
656
657
658
659
660
661
662
663
664
def tap_failure_to_result(
    callable_: Callable[Concatenate[_F1, _P], Result[object, _S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_F1, _S1]], Result[_F1, tuple[_S1, ...] | tuple[_S2, ...]]]:
    """Create function that applies a side effect with return type [trcks.Result][]
    to [trcks.Failure][] values.

    [trcks.SuccessTuple][] 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.Success][],
            *this* [trcks.Success][] is returned (wrapped as a homogeneous tuple).
            Passes on [trcks.SuccessTuple][] values without side effects.

    Examples:
        >>> from collections.abc import Callable
        >>> from trcks import Result, ResultTuple
        >>> from trcks.fp.monads import result_tuple as rt
        >>> def _recover_from_not_found(description: str) -> Result[None, int]:
        ...     if description == "not found":
        ...         return "success", 42
        ...     return "failure", None
        >>> recover_from_not_found: Callable[
        ...     [ResultTuple[str, int]], Result[str, tuple[int, ...]]
        ... ] = rt.tap_failure_to_result(_recover_from_not_found)
        >>> recover_from_not_found(("failure", "not found"))
        ('success', (42,))
        >>> recover_from_not_found(("failure", "fatal"))
        ('failure', 'fatal')
        >>> recover_from_not_found(("success", (1, 2)))
        ('success', (1, 2))
    """
    composed_callable: Callable[Concatenate[_F1, _P], ResultTuple[object, _S2]] = (
        compose(callable_, construct_from_result)
    )
    return tap_failure_to_result_iterable(composed_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.SuccessTuple 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:

>>> from collections.abc import Callable
>>> from trcks import Result, ResultTuple
>>> from trcks.fp.monads import result_tuple as rt
>>> def _recover_from_not_found(description: str) -> ResultTuple[None, int]:
...     if description == "not found":
...         return "success", (42,)
...     return "failure", None
>>> recover_from_not_found: Callable[
...     [ResultTuple[str, int]], Result[str, tuple[int, ...]]
... ] = rt.tap_failure_to_result_iterable(_recover_from_not_found)
>>> recover_from_not_found(("failure", "not found"))
('success', (42,))
>>> recover_from_not_found(("failure", "fatal"))
('failure', 'fatal')
>>> recover_from_not_found(("success", (1, 2)))
('success', (1, 2))
Source code in src/trcks/fp/_monads/result_tuple.py
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
703
704
705
706
707
708
709
710
711
712
713
def tap_failure_to_result_iterable(
    callable_: Callable[Concatenate[_F1, _P], ResultIterable[object, _S2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_F1, _S1]], Result[_F1, tuple[_S1, ...] | tuple[_S2, ...]]]:
    """Create function that applies a side effect with return type
    [trcks.ResultIterable][] to [trcks.Failure][] values.

    [trcks.SuccessTuple][] 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.SuccessTuple][] values without side effects.

    Examples:
        >>> from collections.abc import Callable
        >>> from trcks import Result, ResultTuple
        >>> from trcks.fp.monads import result_tuple as rt
        >>> def _recover_from_not_found(description: str) -> ResultTuple[None, int]:
        ...     if description == "not found":
        ...         return "success", (42,)
        ...     return "failure", None
        >>> recover_from_not_found: Callable[
        ...     [ResultTuple[str, int]], Result[str, tuple[int, ...]]
        ... ] = rt.tap_failure_to_result_iterable(_recover_from_not_found)
        >>> recover_from_not_found(("failure", "not found"))
        ('success', (42,))
        >>> recover_from_not_found(("failure", "fatal"))
        ('failure', 'fatal')
        >>> recover_from_not_found(("success", (1, 2)))
        ('success', (1, 2))
    """
    return r.tap_failure_to_result(
        compose(callable_, construct_from_result_iterable), *args, **kwargs
    )

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

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

Source code in src/trcks/fp/monads/result_tuple.py
935
936
937
938
939
940
941
942
943
944
945
946
947
@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[[ResultTuple[_F1, _S1]], Result[_F1, tuple[_S1, ...] | tuple[_S2, ...]]]:
    """Deprecated alias for
    [trcks.fp.monads.result_tuple.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.result_tuple.tap_failure_to_iterable.

Source code in src/trcks/fp/monads/result_tuple.py
950
951
952
953
954
955
956
957
958
@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[[ResultTuple[_F1, _S1]], SuccessTuple[_F1] | SuccessTuple[_S1]]:
    """Deprecated alias for [trcks.fp.monads.result_tuple.tap_failure_to_iterable][]."""
    return tap_failure_to_iterable(callable_, *args, **kwargs)  # pragma: no cover

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

Create function that applies a side effect to each element of a trcks.SuccessTuple.

trcks.Failure values are passed on without side effects.

Parameters:

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

    Side effect to apply to each element of the trcks.SuccessTuple.

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

    Positional arguments to be passed to callable_.

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

    Keyword arguments to be passed to callable_.

Returns:

Examples:

>>> from collections.abc import Callable
>>> from trcks import ResultTuple
>>> from trcks.fp.monads import result_tuple as rt
>>> def _log_integer(n: int) -> None:
...     print(f"Received: {n}")
...
>>> log_integers: Callable[
...     [ResultTuple[str, int]], ResultTuple[str, int]
... ] = rt.tap_successes(_log_integer)
>>> r_tpl_1 = log_integers(("success", (1, 2)))
Received: 1
Received: 2
>>> r_tpl_1
('success', (1, 2))
>>> r_tpl_2 = log_integers(("failure", "oops"))
>>> r_tpl_2
('failure', 'oops')
Source code in src/trcks/fp/_monads/result_tuple.py
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
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
def tap_successes(
    callable_: Callable[Concatenate[_S1, _P], object],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_F1, _S1]], ResultTuple[_F1, _S1]]:
    """Create function that applies a side effect to each element
    of a [trcks.SuccessTuple][].

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

    Args:
        callable_: Side effect to apply to each element of the [trcks.SuccessTuple][].
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Passes on [trcks.Failure][] values without side effects.
            Applies the given side effect to each element of the
            [trcks.SuccessTuple][] and returns the original
            [trcks.SuccessTuple][].

    Examples:
        >>> from collections.abc import Callable
        >>> from trcks import ResultTuple
        >>> from trcks.fp.monads import result_tuple as rt
        >>> def _log_integer(n: int) -> None:
        ...     print(f"Received: {n}")
        ...
        >>> log_integers: Callable[
        ...     [ResultTuple[str, int]], ResultTuple[str, int]
        ... ] = rt.tap_successes(_log_integer)
        >>> r_tpl_1 = log_integers(("success", (1, 2)))
        Received: 1
        Received: 2
        >>> r_tpl_1
        ('success', (1, 2))
        >>> r_tpl_2 = log_integers(("failure", "oops"))
        >>> r_tpl_2
        ('failure', 'oops')
    """
    return r.map_success(t.tap(callable_, *args, **kwargs))

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

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

trcks.Failure values are passed on without side effects.

Parameters:

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

    Asynchronous side effect to apply to each success element.

  • *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_tuple as art
>>> from trcks.fp.monads import result_tuple as rt
>>> async def print_slowly(n: int) -> None:
...     await asyncio.sleep(0.001)
...     print(f"Value: {n}")
...
>>> print_successes = rt.tap_successes_to_awaitable(print_slowly)
>>> a_r_tpl_1 = print_successes(("failure", "oops"))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
('failure', 'oops')
>>> a_r_tpl_2 = print_successes(("success", (1, 2)))
>>> r_tpl_2 = asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
Value: 1
Value: 2
>>> r_tpl_2
('success', (1, 2))
Source code in src/trcks/fp/monads/result_tuple.py
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
def tap_successes_to_awaitable(
    callable_: Callable[Concatenate[_S1, _P], Awaitable[object]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_F1, _S1]], AwaitableResultTuple[_F1, _S1]]:
    """Create function that applies an asynchronous side effect
    to [trcks.Success][] values.

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

    Args:
        callable_: Asynchronous side effect to apply to each success element.
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

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

    Examples:
        >>> import asyncio
        >>> from trcks.fp.monads import awaitable_result_tuple as art
        >>> from trcks.fp.monads import result_tuple as rt
        >>> async def print_slowly(n: int) -> None:
        ...     await asyncio.sleep(0.001)
        ...     print(f"Value: {n}")
        ...
        >>> print_successes = rt.tap_successes_to_awaitable(print_slowly)
        >>> a_r_tpl_1 = print_successes(("failure", "oops"))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
        ('failure', 'oops')
        >>> a_r_tpl_2 = print_successes(("success", (1, 2)))
        >>> r_tpl_2 = asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
        Value: 1
        Value: 2
        >>> r_tpl_2
        ('success', (1, 2))
    """
    return compose(
        art.construct_from_result_iterable,
        art.tap_successes_to_awaitable(callable_, *args, **kwargs),
    )

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

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

trcks.Failure values are passed on without side effects.

Parameters:

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

    Asynchronous side effect to apply to each success element, returning an trcks.AwaitableIterable.

  • *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_tuple as art
>>> from trcks.fp.monads import result_tuple as rt
>>> async def slowly_log_twice(n: int) -> tuple[None, None]:
...     await asyncio.sleep(0.001)
...     return print(f"Received: {n}"), print(f"Received: {n}")
...
>>> log_successes = rt.tap_successes_to_awaitable_iterable(slowly_log_twice)
>>> a_r_tpl = log_successes(("success", (7,)))
>>> r_tpl = asyncio.run(art.to_coroutine_result_tuple(a_r_tpl))
Received: 7
Received: 7
>>> r_tpl
('success', (7, 7))
Source code in src/trcks/fp/monads/result_tuple.py
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
def tap_successes_to_awaitable_iterable(
    callable_: Callable[Concatenate[_S1, _P], AwaitableIterable[object]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_F1, _S1]], AwaitableResultTuple[_F1, _S1]]:
    """Create function that applies an asynchronous side effect
    with return type [trcks.AwaitableIterable][] to [trcks.Success][] values.

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

    Args:
        callable_: Asynchronous side effect to apply to each success element,
            returning an [trcks.AwaitableIterable][].
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Passes on [trcks.Failure][] values without side effects.
            Applies the given side effect to [trcks.Success][] values and
            repeats each original element once per element
            in the [trcks.AwaitableIterable][] returned by the side effect.

    Examples:
        >>> import asyncio
        >>> from trcks.fp.monads import awaitable_result_tuple as art
        >>> from trcks.fp.monads import result_tuple as rt
        >>> async def slowly_log_twice(n: int) -> tuple[None, None]:
        ...     await asyncio.sleep(0.001)
        ...     return print(f"Received: {n}"), print(f"Received: {n}")
        ...
        >>> log_successes = rt.tap_successes_to_awaitable_iterable(slowly_log_twice)
        >>> a_r_tpl = log_successes(("success", (7,)))
        >>> r_tpl = asyncio.run(art.to_coroutine_result_tuple(a_r_tpl))
        Received: 7
        Received: 7
        >>> r_tpl
        ('success', (7, 7))
    """
    return compose(
        art.construct_from_result_iterable,
        art.tap_successes_to_awaitable_iterable(callable_, *args, **kwargs),
    )

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

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

trcks.Failure values are passed on without side effects.

Parameters:

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

    Asynchronous side effect to apply to each success element.

  • *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_tuple as art
>>> from trcks.fp.monads import result_tuple as rt
>>> async def slowly_check_if_positive(n: int) -> Result[str, None]:
...     await asyncio.sleep(0.001)
...     if n > 0:
...         return "success", None
...     return "failure", "negative"
...
>>> check_successes = rt.tap_successes_to_awaitable_result(
...     slowly_check_if_positive
... )
>>> a_r_tpl_1 = check_successes(("failure", "oops"))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
('failure', 'oops')
>>> a_r_tpl_2 = check_successes(("success", (1, 2)))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
('success', (1, 2))
Source code in src/trcks/fp/monads/result_tuple.py
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
def tap_successes_to_awaitable_result(
    callable_: Callable[Concatenate[_S1, _P], AwaitableResult[_F2, object]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_F1, _S1]], AwaitableResultTuple[_F1 | _F2, _S1]]:
    """Create function that applies an asynchronous side effect
    with return type [trcks.AwaitableResult][] to [trcks.Success][] values.

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

    Args:
        callable_: Asynchronous side effect to apply to each success element.
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Passes on [trcks.Failure][] values 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.Success][],
            *the original* [trcks.Success][] value is returned.

    Examples:
        >>> import asyncio
        >>> from trcks import Result
        >>> from trcks.fp.monads import awaitable_result_tuple as art
        >>> from trcks.fp.monads import result_tuple as rt
        >>> async def slowly_check_if_positive(n: int) -> Result[str, None]:
        ...     await asyncio.sleep(0.001)
        ...     if n > 0:
        ...         return "success", None
        ...     return "failure", "negative"
        ...
        >>> check_successes = rt.tap_successes_to_awaitable_result(
        ...     slowly_check_if_positive
        ... )
        >>> a_r_tpl_1 = check_successes(("failure", "oops"))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
        ('failure', 'oops')
        >>> a_r_tpl_2 = check_successes(("success", (1, 2)))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
        ('success', (1, 2))
    """
    return compose(
        art.construct_from_result_iterable,
        art.tap_successes_to_awaitable_result(callable_, *args, **kwargs),
    )

tap_successes_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 each success element.

  • *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_tuple as art
>>> from trcks.fp.monads import result_tuple as rt
>>> async def audit(n: int) -> AwaitableResultTuple[str, None]:
...     await asyncio.sleep(0.001)
...     if n > 0:
...         return "success", (None, None)
...     return "failure", "negative"
...
>>> audit_successes = rt.tap_successes_to_awaitable_result_iterable(audit)
>>> a_r_tpl_1 = audit_successes(("failure", "oops"))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
('failure', 'oops')
>>> a_r_tpl_2 = audit_successes(("success", (1, 2)))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
('success', (1, 1, 2, 2))
Source code in src/trcks/fp/monads/result_tuple.py
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
def tap_successes_to_awaitable_result_iterable(
    callable_: Callable[Concatenate[_S1, _P], AwaitableResultIterable[_F2, object]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_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 each success element.
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Passes on [trcks.Failure][] values 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_tuple as art
        >>> from trcks.fp.monads import result_tuple as rt
        >>> async def audit(n: int) -> AwaitableResultTuple[str, None]:
        ...     await asyncio.sleep(0.001)
        ...     if n > 0:
        ...         return "success", (None, None)
        ...     return "failure", "negative"
        ...
        >>> audit_successes = rt.tap_successes_to_awaitable_result_iterable(audit)
        >>> a_r_tpl_1 = audit_successes(("failure", "oops"))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
        ('failure', 'oops')
        >>> a_r_tpl_2 = audit_successes(("success", (1, 2)))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
        ('success', (1, 1, 2, 2))
    """
    return compose(
        art.construct_from_result_iterable,
        art.tap_successes_to_awaitable_result_iterable(callable_, *args, **kwargs),
    )

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

Deprecated alias for trcks.fp.monads.result_tuple.tap_successes_to_awaitable_result_iterable.

Source code in src/trcks/fp/monads/result_tuple.py
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
@deprecated("Use tap_successes_to_awaitable_result_iterable instead")
def tap_successes_to_awaitable_result_tuple(
    callable_: Callable[Concatenate[_S1, _P], AwaitableResultTuple[_F2, object]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_F1, _S1]], AwaitableResultTuple[_F1 | _F2, _S1]]:
    """Deprecated alias for
    [trcks.fp.monads.result_tuple.tap_successes_to_awaitable_result_iterable][].
    """
    return tap_successes_to_awaitable_result_iterable(
        callable_, *args, **kwargs
    )  # pragma: no cover

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

Deprecated alias for trcks.fp.monads.result_tuple.tap_successes_to_awaitable_iterable.

Source code in src/trcks/fp/monads/result_tuple.py
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
@deprecated("Use tap_successes_to_awaitable_iterable instead")
def tap_successes_to_awaitable_tuple(
    callable_: Callable[Concatenate[_S1, _P], AwaitableTuple[object]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_F1, _S1]], AwaitableResultTuple[_F1, _S1]]:
    """Deprecated alias for
    [trcks.fp.monads.result_tuple.tap_successes_to_awaitable_iterable][].
    """
    return tap_successes_to_awaitable_iterable(
        callable_, *args, **kwargs
    )  # pragma: no cover

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

Create function that applies a collections.abc.Iterable-returning side effect to each element of a trcks.SuccessTuple.

trcks.Failure values are passed on without side effects.

Parameters:

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

    Side effect to apply to each element of the trcks.SuccessTuple.

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

    Positional arguments to be passed to callable_.

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

    Keyword arguments to be passed to callable_.

Returns:

Examples:

>>> from collections.abc import Callable
>>> from trcks import ResultTuple
>>> from trcks.fp.monads import result_tuple as rt
>>> def _log_twice(n: int) -> tuple[None, None]:
...     return print(f"Received: {n}"), print(f"Received: {n}")
...
>>> log_twice: Callable[
...     [ResultTuple[str, int]], ResultTuple[str, int]
... ] = rt.tap_successes_to_iterable(_log_twice)
>>> log_twice(("success", (7,)))
Received: 7
Received: 7
('success', (7, 7))
Source code in src/trcks/fp/_monads/result_tuple.py
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
790
791
792
793
794
795
796
797
798
799
800
801
802
def tap_successes_to_iterable(
    callable_: Callable[Concatenate[_S1, _P], Iterable[object]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_F1, _S1]], ResultTuple[_F1, _S1]]:
    """Create function that applies a [collections.abc.Iterable][]-returning
    side effect to each element of a [trcks.SuccessTuple][].

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

    Args:
        callable_: Side effect to apply to each element of the [trcks.SuccessTuple][].
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Passes on [trcks.Failure][] values without side effects.
            Applies the given side effect to each element of the
            [trcks.SuccessTuple][]
            and repeats each original element once per element in the
            [collections.abc.Iterable][] returned by the side effect.

    Examples:
        >>> from collections.abc import Callable
        >>> from trcks import ResultTuple
        >>> from trcks.fp.monads import result_tuple as rt
        >>> def _log_twice(n: int) -> tuple[None, None]:
        ...     return print(f"Received: {n}"), print(f"Received: {n}")
        ...
        >>> log_twice: Callable[
        ...     [ResultTuple[str, int]], ResultTuple[str, int]
        ... ] = rt.tap_successes_to_iterable(_log_twice)
        >>> log_twice(("success", (7,)))
        Received: 7
        Received: 7
        ('success', (7, 7))
    """
    return r.map_success(t.tap_to_iterable(callable_, *args, **kwargs))

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

Create function that applies a side effect with return type trcks.Result to each element of a trcks.SuccessTuple.

trcks.Failure values are passed on without side effects.

Parameters:

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

    Side effect to apply to each element of the trcks.SuccessTuple.

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

    Positional arguments to be passed to callable_.

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

    Keyword arguments to be passed to callable_.

Returns:

Examples:

>>> from collections.abc import Callable
>>> from trcks import Result, ResultTuple
>>> from trcks.fp.monads import result_tuple as rt
>>> def _validate_positive(n: int) -> Result[str, None]:
...     if n > 0:
...         return "success", None
...     return "failure", "not positive"
...
>>> validate_positive: Callable[
...     [ResultTuple[str, int]], ResultTuple[str, int]
... ] = rt.tap_successes_to_result(_validate_positive)
>>> validate_positive(("success", (1, 2)))
('success', (1, 2))
>>> validate_positive(("success", (1, -1, 2)))
('failure', 'not positive')
>>> validate_positive(("failure", "oops"))
('failure', 'oops')
Source code in src/trcks/fp/_monads/result_tuple.py
805
806
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
def tap_successes_to_result(
    callable_: Callable[Concatenate[_S1, _P], Result[_F2, object]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_F1, _S1]], ResultTuple[_F1 | _F2, _S1]]:
    """Create function that applies a side effect with return type [trcks.Result][]
    to each element of a [trcks.SuccessTuple][].

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

    Args:
        callable_: Side effect to apply to each element of the [trcks.SuccessTuple][].
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Passes on [trcks.Failure][] values without side effects.
            Applies the given side effect to each element of the
            [trcks.SuccessTuple][].
            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.SuccessTuple][] element is returned.

    Examples:
        >>> from collections.abc import Callable
        >>> from trcks import Result, ResultTuple
        >>> from trcks.fp.monads import result_tuple as rt
        >>> def _validate_positive(n: int) -> Result[str, None]:
        ...     if n > 0:
        ...         return "success", None
        ...     return "failure", "not positive"
        ...
        >>> validate_positive: Callable[
        ...     [ResultTuple[str, int]], ResultTuple[str, int]
        ... ] = rt.tap_successes_to_result(_validate_positive)
        >>> validate_positive(("success", (1, 2)))
        ('success', (1, 2))
        >>> validate_positive(("success", (1, -1, 2)))
        ('failure', 'not positive')
        >>> validate_positive(("failure", "oops"))
        ('failure', 'oops')
    """
    composed_callable: Callable[Concatenate[_S1, _P], ResultTuple[_F2, object]] = (
        compose(callable_, construct_from_result)
    )
    return tap_successes_to_result_iterable(composed_callable, *args, **kwargs)

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

Create function that applies a side effect with return type trcks.ResultTuple to each element of a trcks.SuccessTuple.

trcks.Failure values are passed on without side effects.

Parameters:

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

    Side effect to apply to each element of the trcks.SuccessTuple.

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

    Positional arguments to be passed to callable_.

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

    Keyword arguments to be passed to callable_.

Returns:

Examples:

>>> from collections.abc import Callable
>>> from trcks import ResultTuple
>>> from trcks.fp.monads import result_tuple as rt
>>> def _validate_positive_twice(n: int) -> ResultTuple[str, None]:
...     if n > 0:
...         return "success", (None, None)
...     return "failure", "not positive"
...
>>> validate_positive_twice: Callable[
...     [ResultTuple[str, int]], ResultTuple[str, int]
... ] = rt.tap_successes_to_result_iterable(_validate_positive_twice)
>>> validate_positive_twice(("success", (7,)))
('success', (7, 7))
>>> validate_positive_twice(("success", (1, -1)))
('failure', 'not positive')
Source code in src/trcks/fp/_monads/result_tuple.py
857
858
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
909
910
911
912
913
914
def tap_successes_to_result_iterable(
    callable_: Callable[Concatenate[_S1, _P], ResultIterable[_F2, object]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_F1, _S1]], ResultTuple[_F1 | _F2, _S1]]:
    """Create function that applies a side effect with return type
    [trcks.ResultTuple][] to each element of a [trcks.SuccessTuple][].

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

    Args:
        callable_: Side effect to apply to each element of the [trcks.SuccessTuple][].
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Passes on [trcks.Failure][] values without side effects.
            Applies the given side effect to each element of the
            [trcks.SuccessTuple][].
            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.SuccessTuple][] element is repeated once
            per element in the side effect output.

    Examples:
        >>> from collections.abc import Callable
        >>> from trcks import ResultTuple
        >>> from trcks.fp.monads import result_tuple as rt
        >>> def _validate_positive_twice(n: int) -> ResultTuple[str, None]:
        ...     if n > 0:
        ...         return "success", (None, None)
        ...     return "failure", "not positive"
        ...
        >>> validate_positive_twice: Callable[
        ...     [ResultTuple[str, int]], ResultTuple[str, int]
        ... ] = rt.tap_successes_to_result_iterable(_validate_positive_twice)
        >>> validate_positive_twice(("success", (7,)))
        ('success', (7, 7))
        >>> validate_positive_twice(("success", (1, -1)))
        ('failure', 'not positive')
    """

    def tapped_callable(s1: _S1) -> ResultTuple[_F2, _S1]:
        match callable_(s1, *args, **kwargs):
            case ("failure", _) as r_it:
                return r_it
            case ("success", s2s):
                return "success", tuple(s1 for _ in s2s)
            case _ as r_it:  # pragma: no cover
                assert_type(r_it, Never)  # type: ignore[unreachable]  # pyright: ignore[reportUnreachable]
                msg = f"{type(r_it).__name__!r} is not a valid ResultIterable"
                raise TypeError(msg)

    return map_successes_to_result_iterable(tapped_callable)

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

Deprecated alias for trcks.fp.monads.result_tuple.tap_successes_to_result_iterable.

Source code in src/trcks/fp/monads/result_tuple.py
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
@deprecated("Use tap_successes_to_result_iterable instead")
def tap_successes_to_result_tuple(
    callable_: Callable[Concatenate[_S1, _P], ResultTuple[_F2, object]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_F1, _S1]], ResultTuple[_F1 | _F2, _S1]]:
    """Deprecated alias for
    [trcks.fp.monads.result_tuple.tap_successes_to_result_iterable][].
    """
    return tap_successes_to_result_iterable(
        callable_, *args, **kwargs
    )  # pragma: no cover

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

Deprecated alias for trcks.fp.monads.result_tuple.tap_successes_to_iterable.

Source code in src/trcks/fp/monads/result_tuple.py
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
@deprecated("Use tap_successes_to_iterable instead")
def tap_successes_to_tuple(
    callable_: Callable[Concatenate[_S1, _P], tuple[object, ...]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[ResultTuple[_F1, _S1]], ResultTuple[_F1, _S1]]:
    """Deprecated alias for
    [trcks.fp.monads.result_tuple.tap_successes_to_iterable][].
    """
    return tap_successes_to_iterable(callable_, *args, **kwargs)  # pragma: no cover