Skip to content

trcks.fp.monads.result_tuple

Monadic functions for trcks.ResultTuple.

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

Example

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.

Example
>>> 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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][].

    Example:
        >>> 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:

Example
>>> 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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.

    Example:
        >>> 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_successes(value)

Create a trcks.SuccessTuple object from a single value.

Parameters:

  • value (_S) –

    A single value.

Returns:

Example
>>> 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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.

    Example:
        >>> 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:

Example
>>> 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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.

    Example:
        >>> 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
135
136
137
138
139
140
@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(f)

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

trcks.SuccessTuple values are left unchanged.

Parameters:

Returns:

Example
>>> 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
def map_failure(
    f: Callable[[_F1], _F2],
) -> 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:
        f: Function to apply to the [trcks.Failure][] values.

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

    Example:
        >>> 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(f)

map_failure_to_iterable(f)

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

trcks.SuccessTuple values are left unchanged.

Parameters:

Returns:

Example
>>> 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
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
225
def map_failure_to_iterable(
    f: Callable[[_F1], Iterable[_S2]],
) -> 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:
        f: Function to apply to the [trcks.Failure][] values.

    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.

    Example:
        >>> 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_f(
        r_tpl: ResultTuple[_F1, _S1],
    ) -> SuccessTuple[_S1] | SuccessTuple[_S2]:
        match r_tpl:
            case ("failure", value):
                return "success", tuple(f(value))  # pyrefly: ignore[bad-argument-type]
            case ("success", _):
                return r_tpl
            case _:  # pragma: no cover
                assert_type(r_tpl, Never)  # type: ignore[unreachable]  # pyright: ignore[reportUnreachable]  # pyrefly: ignore [assert-type]
                msg = f"{type(r_tpl).__name__!r} is not a valid ResultTuple"
                raise TypeError(msg)

    return mapped_f

map_failure_to_result(f)

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

trcks.SuccessTuple values are left unchanged.

Parameters:

Returns:

Example
>>> 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
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
def map_failure_to_result(
    f: Callable[[_F1], Result[_F2, _S2]],
) -> 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:
        f: Function to apply to the [trcks.Failure][] values.

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

    Example:
        >>> 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(compose2((f, construct_from_result)))

map_failure_to_result_iterable(f)

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

trcks.SuccessTuple values are left unchanged.

Parameters:

Returns:

Example
>>> 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
def map_failure_to_result_iterable(
    f: Callable[[_F1], ResultIterable[_F2, _S2]],
) -> 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:
        f: Function to apply to the [trcks.Failure][] values.

    Returns:
        Maps [trcks.Failure][] values to new [trcks.ResultTuple][] values
            according to the given function and
            leaves [trcks.SuccessTuple][] values unchanged.

    Example:
        >>> 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(compose2((f, r.map_success(tuple))))

map_failure_to_result_tuple(f)

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

Source code in src/trcks/fp/monads/result_tuple.py
304
305
306
307
308
309
310
311
@deprecated("Use map_failure_to_result_iterable instead")
def map_failure_to_result_tuple(
    f: Callable[[_F1], ResultTuple[_F2, _S2]],
) -> 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(f)  # pragma: no cover

map_failure_to_tuple(f)

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

Source code in src/trcks/fp/monads/result_tuple.py
314
315
316
317
318
319
@deprecated("Use map_failure_to_iterable instead")
def map_failure_to_tuple(
    f: Callable[[_F1], tuple[_S2, ...]],
) -> 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(f)  # pragma: no cover

map_successes(f)

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

trcks.Failure values are left unchanged.

Parameters:

Returns:

Example
>>> 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
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
def map_successes(
    f: Callable[[_S1], _S2],
) -> 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:
        f: Function to apply to each element of the [trcks.SuccessTuple][].

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

    Example:
        >>> 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_(f))

map_successes_to_iterable(f)

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

trcks.Failure values are left unchanged.

Parameters:

Returns:

Example
>>> 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
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
def map_successes_to_iterable(
    f: Callable[[_S1], Iterable[_S2]],
) -> 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:
        f: Function to apply to each element of the [trcks.SuccessTuple][].

    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.

    Example:
        >>> 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(f))

map_successes_to_result(f)

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

trcks.Failure values are left unchanged.

Parameters:

Returns:

Example
>>> 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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
def map_successes_to_result(
    f: Callable[[_S1], Result[_F2, _S2]],
) -> 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:
        f: Function to apply to each element of the [trcks.SuccessTuple][].

    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.

    Example:
        >>> 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(compose2((f, construct_from_result)))

map_successes_to_result_iterable(f)

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

trcks.Failure values are left unchanged.

Parameters:

Returns:

Example
>>> 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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
def map_successes_to_result_iterable(
    f: Callable[[_S1], ResultIterable[_F2, _S2]],
) -> 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:
        f: Function to apply to each element of the [trcks.SuccessTuple][].

    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 `f`, if any.

    Example:
        >>> 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_f(s1s: tuple[_S1, ...]) -> ResultTuple[_F2, _S2]:
        s2s: list[_S2] = []
        for s1 in s1s:
            match f(s1):
                case ("failure", _) as r_it:
                    return r_it
                case ("success", additional_s2s):
                    s2s.extend(additional_s2s)  # pyrefly: ignore[bad-argument-type]
                case _ as r_it:  # pragma: no cover
                    assert_type(r_it, Never)  # type: ignore[unreachable]  # pyright: ignore[reportUnreachable]  # pyrefly: ignore [assert-type]
                    msg = f"{type(r_it).__name__!r} is not a valid ResultIterable"
                    raise TypeError(msg)
        return "success", tuple(s2s)

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

    return mapped_f

map_successes_to_result_tuple(f)

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

Source code in src/trcks/fp/monads/result_tuple.py
494
495
496
497
498
499
500
501
@deprecated("Use map_successes_to_result_iterable instead")
def map_successes_to_result_tuple(
    f: Callable[[_S1], ResultTuple[_F2, _S2]],
) -> 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(f)  # pragma: no cover

map_successes_to_tuple(f)

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

Source code in src/trcks/fp/monads/result_tuple.py
504
505
506
507
508
509
510
511
@deprecated("Use map_successes_to_iterable instead")
def map_successes_to_tuple(
    f: Callable[[_S1], tuple[_S2, ...]],
) -> Callable[[ResultTuple[_F1, _S1]], ResultTuple[_F1, _S2]]:
    """Deprecated alias for
    [trcks.fp.monads.result_tuple.map_successes_to_iterable][].
    """
    return map_successes_to_iterable(f)  # pragma: no cover

tap_failure(f)

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

trcks.SuccessTuple values are passed on without side effects.

Parameters:

Returns:

Example
>>> 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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
def tap_failure(
    f: Callable[[_F1], object],
) -> 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:
        f: Side effect to apply to the [trcks.Failure][] value.

    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.

    Example:
        >>> 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(f)

tap_failure_to_iterable(f)

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:

Returns:

Example
>>> 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
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
590
591
def tap_failure_to_iterable(
    f: Callable[[_F1], Iterable[object]],
) -> 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:
        f: Side effect to apply to the [trcks.Failure][] value.

    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.

    Example:
        >>> 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_f(f1: _F1) -> tuple[_F1, ...]:
        return tuple(f1 for _s2 in f(f1))

    return map_failure_to_iterable(tapped_f)

tap_failure_to_result(f)

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:

Returns:

Example
>>> 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
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
def tap_failure_to_result(
    f: Callable[[_F1], Result[object, _S2]],
) -> 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:
        f: Side effect to apply to the [trcks.Failure][] value.

    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.

    Example:
        >>> 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_f: Callable[[_F1], ResultTuple[object, _S2]] = compose2(
        (f, construct_from_result)
    )
    return tap_failure_to_result_iterable(composed_f)

tap_failure_to_result_iterable(f)

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:

Returns:

Example
>>> 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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
def tap_failure_to_result_iterable(
    f: Callable[[_F1], ResultIterable[object, _S2]],
) -> 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:
        f: Side effect to apply to the [trcks.Failure][] value.

    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.

    Example:
        >>> 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(compose2((f, r.map_success(tuple))))

tap_failure_to_result_tuple(f)

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

Source code in src/trcks/fp/monads/result_tuple.py
677
678
679
680
681
682
683
684
@deprecated("Use tap_failure_to_result_iterable instead")
def tap_failure_to_result_tuple(
    f: Callable[[_F1], ResultTuple[object, _S2]],
) -> 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(f)  # pragma: no cover

tap_failure_to_tuple(f)

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

Source code in src/trcks/fp/monads/result_tuple.py
687
688
689
690
691
692
@deprecated("Use tap_failure_to_iterable instead")
def tap_failure_to_tuple(
    f: Callable[[_F1], tuple[object, ...]],
) -> 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(f)  # pragma: no cover

tap_successes(f)

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

trcks.Failure values are passed on without side effects.

Parameters:

Returns:

Example
>>> 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
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
730
731
def tap_successes(
    f: Callable[[_S1], object],
) -> 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:
        f: Side effect to apply to each element of the [trcks.SuccessTuple][].

    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][].

    Example:
        >>> 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(f))

tap_successes_to_iterable(f)

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:

Returns:

Example
>>> 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
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
def tap_successes_to_iterable(
    f: Callable[[_S1], Iterable[object]],
) -> 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:
        f: Side effect to apply to each element of the [trcks.SuccessTuple][].

    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.

    Example:
        >>> 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(f))

tap_successes_to_result(f)

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:

Returns:

Example
>>> 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
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
def tap_successes_to_result(
    f: Callable[[_S1], Result[_F2, object]],
) -> 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:
        f: Side effect to apply to each element of the [trcks.SuccessTuple][].

    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.

    Example:
        >>> 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_f: Callable[[_S1], ResultTuple[_F2, object]] = compose2(
        (f, construct_from_result)
    )
    return tap_successes_to_result_iterable(composed_f)

tap_successes_to_result_iterable(f)

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:

Returns:

Example
>>> 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
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
def tap_successes_to_result_iterable(
    f: Callable[[_S1], ResultIterable[_F2, object]],
) -> 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:
        f: Side effect to apply to each element of the [trcks.SuccessTuple][].

    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.

    Example:
        >>> 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_f(s1: _S1) -> ResultTuple[_F2, _S1]:
        match f(s1):
            case ("failure", _) as r_it:
                return r_it
            case ("success", s2s):
                return "success", tuple(s1 for _ in s2s)  # pyrefly: ignore[not-iterable]
            case _ as r_it:  # pragma: no cover
                assert_type(r_it, Never)  # type: ignore[unreachable]  # pyright: ignore[reportUnreachable]  # pyrefly: ignore [assert-type]
                msg = f"{type(r_it).__name__!r} is not a valid ResultIterable"
                raise TypeError(msg)

    return map_successes_to_result_iterable(tapped_f)

tap_successes_to_result_tuple(f)

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

Source code in src/trcks/fp/monads/result_tuple.py
868
869
870
871
872
873
874
875
@deprecated("Use tap_successes_to_result_iterable instead")
def tap_successes_to_result_tuple(
    f: Callable[[_S1], ResultTuple[_F2, object]],
) -> 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(f)  # pragma: no cover

tap_successes_to_tuple(f)

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

Source code in src/trcks/fp/monads/result_tuple.py
878
879
880
881
882
883
884
885
@deprecated("Use tap_successes_to_iterable instead")
def tap_successes_to_tuple(
    f: Callable[[_S1], tuple[object, ...]],
) -> Callable[[ResultTuple[_F1, _S1]], ResultTuple[_F1, _S1]]:
    """Deprecated alias for
    [trcks.fp.monads.result_tuple.tap_successes_to_iterable][].
    """
    return tap_successes_to_iterable(f)  # pragma: no cover