Skip to content

trcks.fp.monads.tuple_

Monadic functions for homogeneous tuples.

Provides utilities for functional composition of functions returning homogeneous tuple values.

Note

The underscore in the module name helps to avoid collisions with the built-in class tuple.

Examples:

Create and process a homogeneous tuple:

>>> from trcks.fp.composition import pipe
>>> from trcks.fp.monads import tuple_ as t
>>> def double_integer(n: int) -> int:
...     return n * 2
...
>>> def log_integer(n: int) -> None:
...     print(f"Received: {n}")
...
>>> tpl = pipe(
...     (1, 2, 3),
...     t.map_(double_integer),
...     t.tap(log_integer),
... )
Received: 2
Received: 4
Received: 6
>>> tpl
(2, 4, 6)

Map each element to a homogeneous tuple and flatten the result:

>>> from trcks.fp.composition import pipe
>>> from trcks.fp.monads import tuple_ as t
>>> def duplicate_integer(n: int) -> tuple[int, int]:
...     return n, n
...
>>> tpl = pipe(
...     (1, 2, 3),
...     t.map_to_iterable(duplicate_integer),
... )
>>> tpl
(1, 1, 2, 2, 3, 3)

construct(value)

Create a tuple from a single value.

Parameters:

  • value (_T) –

    A single value.

Returns:

  • tuple[_T,] –

    Contains the single value.

Examples:

>>> from trcks.fp.monads import tuple_ as t
>>> t.construct(42)
(42,)
Source code in src/trcks/fp/_monads/tuple_.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def construct(value: _T, /) -> tuple[_T,]:
    """Create a [tuple][] from a single value.

    Args:
        value: A single value.

    Returns:
        Contains the single value.

    Examples:
        >>> from trcks.fp.monads import tuple_ as t
        >>> t.construct(42)
        (42,)
    """
    return (value,)

construct_from_iterable(it)

Create a homogeneous tuple from an iterable.

Parameters:

Returns:

  • tuple[_T, ...] –

    The tuple created from the iterable.

Examples:

>>> from trcks.fp.monads import tuple_ as t
>>> t.construct_from_iterable([1, 2, 3])
(1, 2, 3)
Source code in src/trcks/fp/_monads/tuple_.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def construct_from_iterable(it: Iterable[_T], /) -> tuple[_T, ...]:
    """Create a homogeneous [tuple][] from an iterable.

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

    Returns:
        The [tuple][] created from the iterable.

    Examples:
        >>> from trcks.fp.monads import tuple_ as t
        >>> t.construct_from_iterable([1, 2, 3])
        (1, 2, 3)
    """
    return tuple(it)

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

Create function that maps homogeneous tuples to homogeneous tuples of the same length.

Parameters:

  • callable_ (Callable[Concatenate[_T1, _P], _T2]) –

    Function to apply to each element.

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

    Positional arguments to be passed to callable_.

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

    Keyword arguments to be passed to callable_.

Returns:

  • Callable[[tuple[_T1, ...]], tuple[_T2, ...]] –

    Maps homogeneous tuples to homogeneous tuples of the same length according to the given function.

Note

The underscore in the function name helps to avoid collisions with the built-in function map.

Examples:

>>> from collections.abc import Callable
>>> from trcks.fp.monads import tuple_ as t
>>> def double_integer(n: int) -> int:
...     return n * 2
...
>>> double_integers: Callable[
...     [tuple[int, ...]], tuple[int, ...]
... ] = t.map_(double_integer)
>>> double_integers((1, 2, 3))
(2, 4, 6)
Source code in src/trcks/fp/_monads/tuple_.py
54
55
56
57
58
59
60
61
62
63
64
65
66
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 map_(
    callable_: Callable[Concatenate[_T1, _P], _T2],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[tuple[_T1, ...]], tuple[_T2, ...]]:
    """Create function that maps homogeneous [tuple][]s to
    homogeneous [tuple][]s of the same length.

    Args:
        callable_: Function to apply to each element.
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Maps homogeneous [tuple][]s to homogeneous [tuple][]s of the same length
            according to the given function.

    Note:
        The underscore in the function name helps to avoid collisions
            with the built-in function [map][].

    Examples:
        >>> from collections.abc import Callable
        >>> from trcks.fp.monads import tuple_ as t
        >>> def double_integer(n: int) -> int:
        ...     return n * 2
        ...
        >>> double_integers: Callable[
        ...     [tuple[int, ...]], tuple[int, ...]
        ... ] = t.map_(double_integer)
        >>> double_integers((1, 2, 3))
        (2, 4, 6)
    """
    return map_to_iterable(compose(callable_, construct), *args, **kwargs)

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

Create function that maps each element of a homogeneous tuple to a new element of a trcks.AwaitableTuple.

Parameters:

  • callable_ (Callable[Concatenate[_T1, _P], Awaitable[_T2]]) –

    Asynchronous function to apply to each 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_tuple as at
>>> from trcks.fp.monads import tuple_ as t
>>> async def slowly_add_one(n: int) -> int:
...     await asyncio.sleep(0.001)
...     return n + 1
...
>>> add_one_to_each = t.map_to_awaitable(slowly_add_one)
>>> a_tpl = add_one_to_each((1, 2, 3))
>>> asyncio.run(at.to_coroutine_tuple(a_tpl))
(2, 3, 4)
Source code in src/trcks/fp/monads/tuple_.py
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
def map_to_awaitable(
    callable_: Callable[Concatenate[_T1, _P], Awaitable[_T2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[tuple[_T1, ...]], AwaitableTuple[_T2]]:
    """Create function that maps each element of a homogeneous [tuple][]
    to a new element of a [trcks.AwaitableTuple][].

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

    Returns:
        Maps homogeneous [tuple][]s to [trcks.AwaitableTuple][]s of the same length
            according to the given asynchronous function.

    Examples:
        >>> import asyncio
        >>> from trcks.fp.monads import awaitable_tuple as at
        >>> from trcks.fp.monads import tuple_ as t
        >>> async def slowly_add_one(n: int) -> int:
        ...     await asyncio.sleep(0.001)
        ...     return n + 1
        ...
        >>> add_one_to_each = t.map_to_awaitable(slowly_add_one)
        >>> a_tpl = add_one_to_each((1, 2, 3))
        >>> asyncio.run(at.to_coroutine_tuple(a_tpl))
        (2, 3, 4)
    """
    return compose(
        at.construct_from_iterable, at.map_to_awaitable(callable_, *args, **kwargs)
    )

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

Create function that maps each element of a homogeneous tuple to a trcks.AwaitableIterable and flattens the result.

Parameters:

  • callable_ (Callable[Concatenate[_T1, _P], AwaitableIterable[_T2]]) –

    Asynchronous function to apply to each element, returning a 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_tuple as at
>>> from trcks.fp.monads import tuple_ as t
>>> async def slowly_duplicate(n: int) -> tuple[int, int]:
...     await asyncio.sleep(0.001)
...     return n, n
...
>>> duplicate_each = t.map_to_awaitable_iterable(slowly_duplicate)
>>> a_tpl = duplicate_each((1, 2))
>>> asyncio.run(at.to_coroutine_tuple(a_tpl))
(1, 1, 2, 2)
Source code in src/trcks/fp/monads/tuple_.py
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
def map_to_awaitable_iterable(
    callable_: Callable[Concatenate[_T1, _P], AwaitableIterable[_T2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[tuple[_T1, ...]], AwaitableTuple[_T2]]:
    """Create function that maps each element of a homogeneous [tuple][]
    to a [trcks.AwaitableIterable][] and flattens the result.

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

    Returns:
        Maps homogeneous [tuple][]s to [trcks.AwaitableTuple][]s of varying length
            according to the given asynchronous function.

    Examples:
        >>> import asyncio
        >>> from trcks.fp.monads import awaitable_tuple as at
        >>> from trcks.fp.monads import tuple_ as t
        >>> async def slowly_duplicate(n: int) -> tuple[int, int]:
        ...     await asyncio.sleep(0.001)
        ...     return n, n
        ...
        >>> duplicate_each = t.map_to_awaitable_iterable(slowly_duplicate)
        >>> a_tpl = duplicate_each((1, 2))
        >>> asyncio.run(at.to_coroutine_tuple(a_tpl))
        (1, 1, 2, 2)
    """
    return compose(
        at.construct_from_iterable,
        at.map_to_awaitable_iterable(callable_, *args, **kwargs),
    )

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

Create function that maps each element of a homogeneous tuple to a trcks.AwaitableResult value.

Parameters:

  • callable_ (Callable[Concatenate[_T1, _P], AwaitableResult[_F, _S]]) –

    Asynchronous function to apply to each element, returning a trcks.AwaitableResult.

  • *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 tuple_ as t
>>> 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_if_positive = t.map_to_awaitable_result(slowly_double_if_positive)
>>> a_r_tpl_1 = double_if_positive((1, 2))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
('success', (2, 4))
>>> a_r_tpl_2 = double_if_positive((1, -1, 2))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
('failure', 'negative')
Source code in src/trcks/fp/monads/tuple_.py
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
def map_to_awaitable_result(
    callable_: Callable[Concatenate[_T1, _P], AwaitableResult[_F, _S]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[tuple[_T1, ...]], AwaitableResultTuple[_F, _S]]:
    """Create function that maps each element of a homogeneous [tuple][]
    to a [trcks.AwaitableResult][] value.

    Args:
        callable_: Asynchronous function to apply to each element,
            returning a [trcks.AwaitableResult][].
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Maps homogeneous [tuple][]s to [trcks.AwaitableResultTuple][]s with

            - the first [trcks.Failure][] returned by the function, or
            - a [trcks.SuccessTuple][] if the function returns [trcks.Success][]
                for all elements.

    Examples:
        >>> import asyncio
        >>> from trcks import Result
        >>> from trcks.fp.monads import awaitable_result_tuple as art
        >>> from trcks.fp.monads import tuple_ as t
        >>> 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_if_positive = t.map_to_awaitable_result(slowly_double_if_positive)
        >>> a_r_tpl_1 = double_if_positive((1, 2))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
        ('success', (2, 4))
        >>> a_r_tpl_2 = double_if_positive((1, -1, 2))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
        ('failure', 'negative')
    """
    c: Composable2[
        [tuple[_T1, ...]],
        AwaitableResultTuple[Never, _T1],
        AwaitableResultTuple[_F, _S],
    ] = (
        art.construct_successes_from_iterable,
        art.map_successes_to_awaitable_result(callable_, *args, **kwargs),
    )
    return compose(*c)

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

Create function that maps each element of a homogeneous tuple to a trcks.AwaitableResultIterable and flattens the result.

Parameters:

Returns:

Examples:

>>> import asyncio
>>> from trcks import ResultTuple
>>> from trcks.fp.monads import awaitable_result_tuple as art
>>> from trcks.fp.monads import tuple_ as t
>>> async def slowly_expand_if_positive(n: int) -> ResultTuple[str, int]:
...     await asyncio.sleep(0.001)
...     if n > 0:
...         return "success", (n, -n)
...     return "failure", "negative"
...
>>> expand_if_positive = (
...     t.map_to_awaitable_result_iterable(slowly_expand_if_positive)
... )
>>> a_r_tpl_1 = expand_if_positive((1, 2))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
('success', (1, -1, 2, -2))
>>> a_r_tpl_2 = expand_if_positive((1, -1, 2))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
('failure', 'negative')
Source code in src/trcks/fp/monads/tuple_.py
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
def map_to_awaitable_result_iterable(
    callable_: Callable[Concatenate[_T1, _P], AwaitableResultIterable[_F, _S]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[tuple[_T1, ...]], AwaitableResultTuple[_F, _S]]:
    """Create function that maps each element of a homogeneous [tuple][]
    to a [trcks.AwaitableResultIterable][] and flattens the result.

    Args:
        callable_: Asynchronous function to apply to each element,
            returning a [trcks.AwaitableResultIterable][].
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Maps homogeneous [tuple][]s to [trcks.AwaitableResultTuple][]s with

            - the first [trcks.Failure][] returned by the function, or
            - a flattened [trcks.SuccessTuple][] if the function returns
                [trcks.SuccessTuple][] for all elements.

    Examples:
        >>> import asyncio
        >>> from trcks import ResultTuple
        >>> from trcks.fp.monads import awaitable_result_tuple as art
        >>> from trcks.fp.monads import tuple_ as t
        >>> async def slowly_expand_if_positive(n: int) -> ResultTuple[str, int]:
        ...     await asyncio.sleep(0.001)
        ...     if n > 0:
        ...         return "success", (n, -n)
        ...     return "failure", "negative"
        ...
        >>> expand_if_positive = (
        ...     t.map_to_awaitable_result_iterable(slowly_expand_if_positive)
        ... )
        >>> a_r_tpl_1 = expand_if_positive((1, 2))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
        ('success', (1, -1, 2, -2))
        >>> a_r_tpl_2 = expand_if_positive((1, -1, 2))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
        ('failure', 'negative')
    """
    c: Composable2[
        [tuple[_T1, ...]],
        AwaitableResultTuple[Never, _T1],
        AwaitableResultTuple[_F, _S],
    ] = (
        art.construct_successes_from_iterable,
        art.map_successes_to_awaitable_result_iterable(callable_, *args, **kwargs),
    )
    return compose(*c)

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

Deprecated alias for trcks.fp.monads.tuple_.map_to_awaitable_result_iterable.

Source code in src/trcks/fp/monads/tuple_.py
304
305
306
307
308
309
310
311
312
313
314
315
316
@deprecated("Use map_to_awaitable_result_iterable instead")
def map_to_awaitable_result_tuple(
    callable_: Callable[Concatenate[_T1, _P], AwaitableResultTuple[_F, _S]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[tuple[_T1, ...]], AwaitableResultTuple[_F, _S]]:
    """Deprecated alias for
    [trcks.fp.monads.tuple_.map_to_awaitable_result_iterable][].
    """
    return map_to_awaitable_result_iterable(
        callable_, *args, **kwargs
    )  # pragma: no cover

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

Deprecated alias for trcks.fp.monads.tuple_.map_to_awaitable_iterable.

Source code in src/trcks/fp/monads/tuple_.py
319
320
321
322
323
324
325
326
327
@deprecated("Use map_to_awaitable_iterable instead")
def map_to_awaitable_tuple(
    callable_: Callable[Concatenate[_T1, _P], AwaitableTuple[_T2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[tuple[_T1, ...]], AwaitableTuple[_T2]]:
    """Deprecated alias for [trcks.fp.monads.tuple_.map_to_awaitable_iterable][]."""
    return map_to_awaitable_iterable(callable_, *args, **kwargs)  # pragma: no cover

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

Create function that maps homogeneous tuples to homogeneous tuples of varying length.

Parameters:

  • callable_ (Callable[Concatenate[_T1, _P], Iterable[_T2]]) –

    Function to apply to each element.

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

    Positional arguments to be passed to callable_.

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

    Keyword arguments to be passed to callable_.

Returns:

  • Callable[[tuple[_T1, ...]], tuple[_T2, ...]] –

    Maps homogeneous tuples to homogeneous tuples of varying length according to the given function.

Examples:

>>> from collections.abc import Callable
>>> from trcks.fp.monads import tuple_ as t
>>> def duplicate_integer(n: int) -> tuple[int, int]:
...     return n, n
...
>>> duplicate_integers: Callable[
...     [tuple[int, ...]], tuple[int, ...]
... ] = t.map_to_iterable(duplicate_integer)
>>> duplicate_integers((1, 2, 3))
(1, 1, 2, 2, 3, 3)
Source code in src/trcks/fp/_monads/tuple_.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
def map_to_iterable(
    callable_: Callable[Concatenate[_T1, _P], Iterable[_T2]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[tuple[_T1, ...]], tuple[_T2, ...]]:
    """Create function that maps homogeneous [tuple][]s to
    homogeneous [tuple][]s of varying length.

    Args:
        callable_: Function to apply to each element.
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Maps homogeneous [tuple][]s to homogeneous [tuple][]s of varying length
            according to the given function.

    Examples:
        >>> from collections.abc import Callable
        >>> from trcks.fp.monads import tuple_ as t
        >>> def duplicate_integer(n: int) -> tuple[int, int]:
        ...     return n, n
        ...
        >>> duplicate_integers: Callable[
        ...     [tuple[int, ...]], tuple[int, ...]
        ... ] = t.map_to_iterable(duplicate_integer)
        >>> duplicate_integers((1, 2, 3))
        (1, 1, 2, 2, 3, 3)
    """

    def mapped_callable(t1s: tuple[_T1, ...]) -> tuple[_T2, ...]:
        return tuple(t2 for t1 in t1s for t2 in callable_(t1, *args, **kwargs))

    return mapped_callable

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

Create function that maps each element of a homogeneous tuple to a trcks.Result value.

Parameters:

  • callable_ (Callable[Concatenate[_T1, _P], Result[_F, _S]]) –

    Synchronous function to apply to each element.

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

    Positional arguments to be passed to callable_.

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

    Keyword arguments to be passed to callable_.

Returns:

Examples:

>>> from trcks import Result
>>> from trcks.fp.monads import tuple_ as t
>>> def double_if_positive(n: int) -> Result[str, int]:
...     if n > 0:
...         return "success", n * 2
...     return "failure", "negative"
...
>>> double_if_positive_tuple = t.map_to_result(double_if_positive)
>>> double_if_positive_tuple((1, 2, 3))
('success', (2, 4, 6))
>>> double_if_positive_tuple((1, -1, 2))
('failure', 'negative')
Source code in src/trcks/fp/monads/tuple_.py
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
def map_to_result(
    callable_: Callable[Concatenate[_T1, _P], Result[_F, _S]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[tuple[_T1, ...]], ResultTuple[_F, _S]]:
    """Create function that maps each element of a homogeneous [tuple][]
    to a [trcks.Result][] value.

    Args:
        callable_: Synchronous function to apply to each element.
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Maps homogeneous [tuple][]s to [trcks.ResultTuple][]s with

            - the first [trcks.Failure][] returned by the function, or
            - a [trcks.SuccessTuple][] with all transformed elements if the
                function returns [trcks.Success][] for all elements.

    Examples:
        >>> from trcks import Result
        >>> from trcks.fp.monads import tuple_ as t
        >>> def double_if_positive(n: int) -> Result[str, int]:
        ...     if n > 0:
        ...         return "success", n * 2
        ...     return "failure", "negative"
        ...
        >>> double_if_positive_tuple = t.map_to_result(double_if_positive)
        >>> double_if_positive_tuple((1, 2, 3))
        ('success', (2, 4, 6))
        >>> double_if_positive_tuple((1, -1, 2))
        ('failure', 'negative')
    """
    c: Composable2[
        [tuple[_T1, ...]],
        ResultTuple[Never, _T1],
        ResultTuple[_F, _S],
    ] = (
        rt.construct_successes_from_iterable,
        rt.map_successes_to_result(callable_, *args, **kwargs),
    )
    return compose(*c)

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

Create function that maps each element of a homogeneous tuple to a trcks.ResultIterable and flattens the result.

Parameters:

  • callable_ (Callable[Concatenate[_T1, _P], ResultIterable[_F, _S]]) –

    Synchronous function to apply to each element, returning a trcks.ResultIterable.

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

    Positional arguments to be passed to callable_.

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

    Keyword arguments to be passed to callable_.

Returns:

Examples:

>>> from trcks import ResultTuple
>>> from trcks.fp.monads import tuple_ as t
>>> def expand_if_positive(n: int) -> ResultTuple[str, int]:
...     if n > 0:
...         return "success", (n, -n)
...     return "failure", "negative"
...
>>> expand_if_positive_tuple = t.map_to_result_iterable(expand_if_positive)
>>> expand_if_positive_tuple((1, 2))
('success', (1, -1, 2, -2))
>>> expand_if_positive_tuple((1, -1, 2))
('failure', 'negative')
Source code in src/trcks/fp/monads/tuple_.py
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
def map_to_result_iterable(
    callable_: Callable[Concatenate[_T1, _P], ResultIterable[_F, _S]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[tuple[_T1, ...]], ResultTuple[_F, _S]]:
    """Create function that maps each element of a homogeneous [tuple][]
    to a [trcks.ResultIterable][] and flattens the result.

    Args:
        callable_: Synchronous function to apply to each element,
            returning a [trcks.ResultIterable][].
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Maps homogeneous [tuple][]s to [trcks.ResultTuple][]s with

            - the first [trcks.Failure][] returned by the function, or
            - a flattened [trcks.SuccessTuple][] if the function returns
                [trcks.SuccessTuple][] for all elements.

    Examples:
        >>> from trcks import ResultTuple
        >>> from trcks.fp.monads import tuple_ as t
        >>> def expand_if_positive(n: int) -> ResultTuple[str, int]:
        ...     if n > 0:
        ...         return "success", (n, -n)
        ...     return "failure", "negative"
        ...
        >>> expand_if_positive_tuple = t.map_to_result_iterable(expand_if_positive)
        >>> expand_if_positive_tuple((1, 2))
        ('success', (1, -1, 2, -2))
        >>> expand_if_positive_tuple((1, -1, 2))
        ('failure', 'negative')
    """
    c: Composable2[
        [tuple[_T1, ...]],
        ResultTuple[Never, _T1],
        ResultTuple[_F, _S],
    ] = (
        rt.construct_successes_from_iterable,
        rt.map_successes_to_result_iterable(callable_, *args, **kwargs),
    )
    return compose(*c)

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

Deprecated alias for trcks.fp.monads.tuple_.map_to_result_iterable.

Source code in src/trcks/fp/monads/tuple_.py
427
428
429
430
431
432
433
434
435
@deprecated("Use map_to_result_iterable instead")
def map_to_result_tuple(
    callable_: Callable[Concatenate[_T1, _P], ResultTuple[_F, _S]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[tuple[_T1, ...]], ResultTuple[_F, _S]]:
    """Deprecated alias for [trcks.fp.monads.tuple_.map_to_result_iterable][]."""
    return map_to_result_iterable(callable_, *args, **kwargs)  # pragma: no cover

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

Deprecated alias for trcks.fp.monads.tuple_.map_to_iterable.

Source code in src/trcks/fp/monads/tuple_.py
438
439
440
441
442
443
444
445
446
@deprecated("Use map_to_iterable instead")
def map_to_tuple(
    callable_: Callable[Concatenate[_T1, _P], tuple[_T2, ...]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[tuple[_T1, ...]], tuple[_T2, ...]]:
    """Deprecated alias for [trcks.fp.monads.tuple_.map_to_iterable][]."""
    return map_to_iterable(callable_, *args, **kwargs)  # pragma: no cover

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

Create function that applies a side effect to each element of a homogeneous tuple.

Parameters:

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

    Side effect to apply to each element.

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

    Positional arguments to be passed to callable_.

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

    Keyword arguments to be passed to callable_.

Returns:

  • Callable[[tuple[_T1, ...]], tuple[_T1, ...]] –

    Applies the given side effect to each element of a homogeneous tuple and returns the original homogeneous tuple.

Examples:

>>> from collections.abc import Callable
>>> from trcks.fp.monads import tuple_ as t
>>> def log_integer(n: int) -> None:
...     print(f"Received: {n}")
...
>>> log_and_pass_integers: Callable[
...     [tuple[int, ...]], tuple[int, ...]
... ] = t.tap(log_integer)
>>> tpl = log_and_pass_integers((1, 2, 3))
Received: 1
Received: 2
Received: 3
>>> tpl
(1, 2, 3)
Source code in src/trcks/fp/_monads/tuple_.py
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
166
167
168
def tap(
    callable_: Callable[Concatenate[_T1, _P], object],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[tuple[_T1, ...]], tuple[_T1, ...]]:
    """Create function that applies a side effect to each element of a homogeneous
    [tuple][].

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

    Returns:
        Applies the given side effect to each element of a homogeneous [tuple][] and
            returns the original homogeneous [tuple][].

    Examples:
        >>> from collections.abc import Callable
        >>> from trcks.fp.monads import tuple_ as t
        >>> def log_integer(n: int) -> None:
        ...     print(f"Received: {n}")
        ...
        >>> log_and_pass_integers: Callable[
        ...     [tuple[int, ...]], tuple[int, ...]
        ... ] = t.tap(log_integer)
        >>> tpl = log_and_pass_integers((1, 2, 3))
        Received: 1
        Received: 2
        Received: 3
        >>> tpl
        (1, 2, 3)
    """
    return map_(i.tap(callable_, *args, **kwargs))

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

Create function that applies an asynchronous side effect to each element of a homogeneous tuple.

Parameters:

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

    Asynchronous side effect to apply to each 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_tuple as at
>>> from trcks.fp.monads import tuple_ as t
>>> async def slowly_log_integer(n: int) -> None:
...     await asyncio.sleep(0.001)
...     print(f"Received: {n}")
...
>>> log_each = t.tap_to_awaitable(slowly_log_integer)
>>> a_tpl = log_each((1, 2, 3))
>>> asyncio.run(at.to_coroutine_tuple(a_tpl))
Received: 1
Received: 2
Received: 3
(1, 2, 3)
Source code in src/trcks/fp/monads/tuple_.py
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
def tap_to_awaitable(
    callable_: Callable[Concatenate[_T1, _P], Awaitable[object]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[tuple[_T1, ...]], AwaitableTuple[_T1]]:
    """Create function that applies an asynchronous side effect
    to each element of a homogeneous [tuple][].

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

    Returns:
        Applies the given side effect to each element of a homogeneous [tuple][] and
            returns a [trcks.AwaitableTuple][] of the original elements.

    Examples:
        >>> import asyncio
        >>> from trcks.fp.monads import awaitable_tuple as at
        >>> from trcks.fp.monads import tuple_ as t
        >>> async def slowly_log_integer(n: int) -> None:
        ...     await asyncio.sleep(0.001)
        ...     print(f"Received: {n}")
        ...
        >>> log_each = t.tap_to_awaitable(slowly_log_integer)
        >>> a_tpl = log_each((1, 2, 3))
        >>> asyncio.run(at.to_coroutine_tuple(a_tpl))
        Received: 1
        Received: 2
        Received: 3
        (1, 2, 3)
    """
    return compose(
        at.construct_from_iterable, at.tap_to_awaitable(callable_, *args, **kwargs)
    )

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

Create function that applies an asynchronous side effect with return type trcks.AwaitableIterable to each element of a homogeneous tuple.

Parameters:

Returns:

Examples:

>>> import asyncio
>>> from trcks.fp.monads import awaitable_tuple as at
>>> from trcks.fp.monads import tuple_ as t
>>> async def write_to_disk(n: int) -> tuple[str, str]:
...     await asyncio.sleep(0.001)
...     print(f"Wrote {n} to disk.")
...     return str(n), str(n)
...
>>> write_each_to_disk = t.tap_to_awaitable_iterable(write_to_disk)
>>> a_tpl = write_each_to_disk((1, 2, 3))
>>> asyncio.run(at.to_coroutine_tuple(a_tpl))
Wrote 1 to disk.
Wrote 2 to disk.
Wrote 3 to disk.
(1, 1, 2, 2, 3, 3)
Source code in src/trcks/fp/monads/tuple_.py
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
524
525
526
527
528
529
530
531
532
533
def tap_to_awaitable_iterable(
    callable_: Callable[Concatenate[_T1, _P], AwaitableIterable[object]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[tuple[_T1, ...]], AwaitableTuple[_T1]]:
    """Create function that applies an asynchronous side effect
    with return type [trcks.AwaitableIterable][]
    to each element of a homogeneous [tuple][].

    Args:
        callable_: Asynchronous side effect to apply to each element,
            returning a [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 each element of a homogeneous [tuple][] and
            returns a [trcks.AwaitableTuple][] of the original elements,
            each repeated once per element in the side effect output.

    Examples:
        >>> import asyncio
        >>> from trcks.fp.monads import awaitable_tuple as at
        >>> from trcks.fp.monads import tuple_ as t
        >>> async def write_to_disk(n: int) -> tuple[str, str]:
        ...     await asyncio.sleep(0.001)
        ...     print(f"Wrote {n} to disk.")
        ...     return str(n), str(n)
        ...
        >>> write_each_to_disk = t.tap_to_awaitable_iterable(write_to_disk)
        >>> a_tpl = write_each_to_disk((1, 2, 3))
        >>> asyncio.run(at.to_coroutine_tuple(a_tpl))
        Wrote 1 to disk.
        Wrote 2 to disk.
        Wrote 3 to disk.
        (1, 1, 2, 2, 3, 3)
    """
    return compose(
        at.construct_from_iterable,
        at.tap_to_awaitable_iterable(callable_, *args, **kwargs),
    )

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

Create function that applies an asynchronous side effect with return type trcks.Result to each element of a homogeneous tuple.

Parameters:

  • callable_ (Callable[Concatenate[_T1, _P], AwaitableResult[_F, object]]) –

    Asynchronous side effect to apply to each element, returning a trcks.Result.

  • *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 tuple_ as t
>>> 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_if_positive = t.tap_to_awaitable_result(slowly_check_if_positive)
>>> a_r_tpl_1 = check_if_positive((1, 2))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
('success', (1, 2))
>>> a_r_tpl_2 = check_if_positive((1, -1, 2))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
('failure', 'negative')
Source code in src/trcks/fp/monads/tuple_.py
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
def tap_to_awaitable_result(
    callable_: Callable[Concatenate[_T1, _P], AwaitableResult[_F, object]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[tuple[_T1, ...]], AwaitableResultTuple[_F, _T1]]:
    """Create function that applies an asynchronous side effect
    with return type [trcks.Result][]
    to each element of a homogeneous [tuple][].

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

    Returns:
        Maps homogeneous [tuple][]s to [trcks.AwaitableResultTuple][]s with

            - *the returned* [trcks.Failure][] if the applied side effect returns
                a [trcks.Failure][] for an element, or
            - *the original* homogeneous [tuple][] if the applied side effect
                returns [trcks.Success][] for all elements.

    Examples:
        >>> import asyncio
        >>> from trcks import Result
        >>> from trcks.fp.monads import awaitable_result_tuple as art
        >>> from trcks.fp.monads import tuple_ as t
        >>> 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_if_positive = t.tap_to_awaitable_result(slowly_check_if_positive)
        >>> a_r_tpl_1 = check_if_positive((1, 2))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
        ('success', (1, 2))
        >>> a_r_tpl_2 = check_if_positive((1, -1, 2))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
        ('failure', 'negative')
    """
    c: Composable2[
        [tuple[_T1, ...]],
        AwaitableResultTuple[Never, _T1],
        AwaitableResultTuple[_F, _T1],
    ] = (
        art.construct_successes_from_iterable,
        art.tap_successes_to_awaitable_result(callable_, *args, **kwargs),
    )
    return compose(*c)

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

Create function that applies an asynchronous side effect with return type trcks.AwaitableResultIterable to each element of a homogeneous tuple.

Parameters:

Returns:

Examples:

>>> import asyncio
>>> from trcks import ResultTuple
>>> from trcks.fp.monads import awaitable_result_tuple as art
>>> from trcks.fp.monads import tuple_ as t
>>> async def audit(n: int) -> ResultTuple[str, None]:
...     await asyncio.sleep(0.001)
...     if n > 0:
...         return "success", (None, None)
...     return "failure", "negative"
...
>>> audit_successes = t.tap_to_awaitable_result_iterable(audit)
>>> a_r_tpl_1 = audit_successes((1, 2))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
('success', (1, 1, 2, 2))
>>> a_r_tpl_2 = audit_successes((1, -1, 2))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
('failure', 'negative')
Source code in src/trcks/fp/monads/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
640
641
642
643
644
645
646
def tap_to_awaitable_result_iterable(
    callable_: Callable[Concatenate[_T1, _P], AwaitableResultIterable[_F, object]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[tuple[_T1, ...]], AwaitableResultTuple[_F, _T1]]:
    """Create function that applies an asynchronous side effect
    with return type [trcks.AwaitableResultIterable][]
    to each element of a homogeneous [tuple][].

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

    Returns:
        Maps homogeneous [tuple][]s to [trcks.AwaitableResultTuple][]s with

            - *the returned* [trcks.Failure][] if the applied side effect returns
                a [trcks.Failure][] for an element, or
            - *the original* homogeneous [tuple][] with each element repeated once
                per side effect output element if the applied side effect returns
                [trcks.SuccessTuple][] for all elements.

    Examples:
        >>> import asyncio
        >>> from trcks import ResultTuple
        >>> from trcks.fp.monads import awaitable_result_tuple as art
        >>> from trcks.fp.monads import tuple_ as t
        >>> async def audit(n: int) -> ResultTuple[str, None]:
        ...     await asyncio.sleep(0.001)
        ...     if n > 0:
        ...         return "success", (None, None)
        ...     return "failure", "negative"
        ...
        >>> audit_successes = t.tap_to_awaitable_result_iterable(audit)
        >>> a_r_tpl_1 = audit_successes((1, 2))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
        ('success', (1, 1, 2, 2))
        >>> a_r_tpl_2 = audit_successes((1, -1, 2))
        >>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
        ('failure', 'negative')
    """
    c: Composable2[
        [tuple[_T1, ...]],
        AwaitableResultTuple[Never, _T1],
        AwaitableResultTuple[_F, _T1],
    ] = (
        art.construct_successes_from_iterable,
        art.tap_successes_to_awaitable_result_iterable(callable_, *args, **kwargs),
    )
    return compose(*c)

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

Deprecated alias for trcks.fp.monads.tuple_.tap_to_awaitable_result_iterable.

Source code in src/trcks/fp/monads/tuple_.py
649
650
651
652
653
654
655
656
657
658
659
660
661
@deprecated("Use tap_to_awaitable_result_iterable instead")
def tap_to_awaitable_result_tuple(
    callable_: Callable[Concatenate[_T1, _P], AwaitableResultTuple[_F, object]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[tuple[_T1, ...]], AwaitableResultTuple[_F, _T1]]:
    """Deprecated alias for
    [trcks.fp.monads.tuple_.tap_to_awaitable_result_iterable][].
    """
    return tap_to_awaitable_result_iterable(
        callable_, *args, **kwargs
    )  # pragma: no cover

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

Deprecated alias for trcks.fp.monads.tuple_.tap_to_awaitable_iterable.

Source code in src/trcks/fp/monads/tuple_.py
664
665
666
667
668
669
670
671
672
@deprecated("Use tap_to_awaitable_iterable instead")
def tap_to_awaitable_tuple(
    callable_: Callable[Concatenate[_T1, _P], AwaitableTuple[object]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[tuple[_T1, ...]], AwaitableTuple[_T1]]:
    """Deprecated alias for [trcks.fp.monads.tuple_.tap_to_awaitable_iterable][]."""
    return tap_to_awaitable_iterable(callable_, *args, **kwargs)  # pragma: no cover

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

Create function that applies a side effect with return type collections.abc.Iterable to each element of a homogeneous tuple.

Parameters:

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

    Side effect to apply to each element.

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

    Positional arguments to be passed to callable_.

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

    Keyword arguments to be passed to callable_.

Returns:

  • Callable[[tuple[_T1, ...]], tuple[_T1, ...]] –

    Applies the given side effect to each element of a homogeneous tuple. Returns each element as many times as the side effect returns elements.

Examples:

>>> from collections.abc import Callable
>>> from trcks.fp.monads import tuple_ as t
>>> def get_divisors(n: int) -> tuple[int, ...]:
...     candidates = range(1, n + 1)
...     return tuple(c for c in candidates if n % c == 0)
...
>>> repeat_integers_according_to_number_of_divisors: Callable[
...     [tuple[int, ...]], tuple[int, ...]
... ] = t.tap_to_iterable(get_divisors)
>>> repeat_integers_according_to_number_of_divisors((1, 2, 3, 4))
(1, 2, 2, 3, 3, 4, 4, 4)
Source code in src/trcks/fp/_monads/tuple_.py
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
def tap_to_iterable(
    callable_: Callable[Concatenate[_T1, _P], Iterable[object]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[tuple[_T1, ...]], tuple[_T1, ...]]:
    """Create function that applies a side effect
    with return type [collections.abc.Iterable][]
    to each element of a homogeneous [tuple][].

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

    Returns:
        Applies the given side effect to each element of a homogeneous [tuple][].
            Returns each element as many times as the side effect returns elements.

    Examples:
        >>> from collections.abc import Callable
        >>> from trcks.fp.monads import tuple_ as t
        >>> def get_divisors(n: int) -> tuple[int, ...]:
        ...     candidates = range(1, n + 1)
        ...     return tuple(c for c in candidates if n % c == 0)
        ...
        >>> repeat_integers_according_to_number_of_divisors: Callable[
        ...     [tuple[int, ...]], tuple[int, ...]
        ... ] = t.tap_to_iterable(get_divisors)
        >>> repeat_integers_according_to_number_of_divisors((1, 2, 3, 4))
        (1, 2, 2, 3, 3, 4, 4, 4)
    """

    def bypassed_callable(t1: _T1) -> tuple[_T1, ...]:
        return tuple(t1 for _t2 in callable_(t1, *args, **kwargs))

    return map_to_iterable(bypassed_callable)

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

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

Parameters:

  • callable_ (Callable[Concatenate[_T1, _P], Result[_F, object]]) –

    Synchronous side effect to apply to each element.

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

    Positional arguments to be passed to callable_.

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

    Keyword arguments to be passed to callable_.

Returns:

Examples:

>>> from trcks import Result
>>> from trcks.fp.monads import tuple_ as t
>>> def audit(n: int) -> Result[str, None]:
...     if n > 0:
...         return "success", None
...     return "failure", "negative"
...
>>> audit_successes = t.tap_to_result(audit)
>>> audit_successes((1, 2))
('success', (1, 2))
>>> audit_successes((1, -1, 2))
('failure', 'negative')
Source code in src/trcks/fp/monads/tuple_.py
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
714
715
716
717
718
719
720
721
722
def tap_to_result(
    callable_: Callable[Concatenate[_T1, _P], Result[_F, object]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[tuple[_T1, ...]], ResultTuple[_F, _T1]]:
    """Create function that applies a synchronous side effect
    with return type [trcks.Result][]
    to each element of a homogeneous [tuple][].

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

    Returns:
        Maps homogeneous [tuple][]s to [trcks.ResultTuple][]s with

            - *the returned* [trcks.Failure][] if the applied side effect returns
                a [trcks.Failure][] for an element, or
            - *the original* homogeneous [tuple][] if the applied side effect
                returns [trcks.Success][] for all elements.

    Examples:
        >>> from trcks import Result
        >>> from trcks.fp.monads import tuple_ as t
        >>> def audit(n: int) -> Result[str, None]:
        ...     if n > 0:
        ...         return "success", None
        ...     return "failure", "negative"
        ...
        >>> audit_successes = t.tap_to_result(audit)
        >>> audit_successes((1, 2))
        ('success', (1, 2))
        >>> audit_successes((1, -1, 2))
        ('failure', 'negative')
    """
    c: Composable2[
        [tuple[_T1, ...]],
        ResultTuple[Never, _T1],
        ResultTuple[_F, _T1],
    ] = (
        rt.construct_successes_from_iterable,
        rt.tap_successes_to_result(callable_, *args, **kwargs),
    )
    return compose(*c)

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

Create function that applies a synchronous side effect with return type trcks.ResultIterable to each element of a homogeneous tuple.

Parameters:

  • callable_ (Callable[Concatenate[_T1, _P], ResultIterable[_F, object]]) –

    Synchronous side effect to apply to each element, returning a trcks.ResultIterable.

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

    Positional arguments to be passed to callable_.

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

    Keyword arguments to be passed to callable_.

Returns:

Examples:

>>> from trcks import ResultTuple
>>> from trcks.fp.monads import tuple_ as t
>>> def audit(n: int) -> ResultTuple[str, None]:
...     if n > 0:
...         return "success", (None, None)
...     return "failure", "negative"
...
>>> audit_successes = t.tap_to_result_iterable(audit)
>>> audit_successes((7,))
('success', (7, 7))
>>> audit_successes((1, -1))
('failure', 'negative')
Source code in src/trcks/fp/monads/tuple_.py
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
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
def tap_to_result_iterable(
    callable_: Callable[Concatenate[_T1, _P], ResultIterable[_F, object]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[tuple[_T1, ...]], ResultTuple[_F, _T1]]:
    """Create function that applies a synchronous side effect
    with return type [trcks.ResultIterable][]
    to each element of a homogeneous [tuple][].

    Args:
        callable_: Synchronous side effect to apply to each element,
            returning a [trcks.ResultIterable][].
        *args:
            Positional arguments to be passed to `callable_`.
        **kwargs:
            Keyword arguments to be passed to `callable_`.

    Returns:
        Maps homogeneous [tuple][]s to [trcks.ResultTuple][]s with

            - *the returned* [trcks.Failure][] if the applied side effect returns
                a [trcks.Failure][] for an element, or
            - *the original* homogeneous [tuple][] with each element repeated once
                per side effect output element if the applied side effect returns
                [trcks.SuccessTuple][] for all elements.

    Examples:
        >>> from trcks import ResultTuple
        >>> from trcks.fp.monads import tuple_ as t
        >>> def audit(n: int) -> ResultTuple[str, None]:
        ...     if n > 0:
        ...         return "success", (None, None)
        ...     return "failure", "negative"
        ...
        >>> audit_successes = t.tap_to_result_iterable(audit)
        >>> audit_successes((7,))
        ('success', (7, 7))
        >>> audit_successes((1, -1))
        ('failure', 'negative')
    """
    c: Composable2[
        [tuple[_T1, ...]],
        ResultTuple[Never, _T1],
        ResultTuple[_F, _T1],
    ] = (
        rt.construct_successes_from_iterable,
        rt.tap_successes_to_result_iterable(callable_, *args, **kwargs),
    )
    return compose(*c)

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

Deprecated alias for trcks.fp.monads.tuple_.tap_to_result_iterable.

Source code in src/trcks/fp/monads/tuple_.py
777
778
779
780
781
782
783
784
785
@deprecated("Use tap_to_result_iterable instead")
def tap_to_result_tuple(
    callable_: Callable[Concatenate[_T1, _P], ResultTuple[_F, object]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[tuple[_T1, ...]], ResultTuple[_F, _T1]]:
    """Deprecated alias for [trcks.fp.monads.tuple_.tap_to_result_iterable][]."""
    return tap_to_result_iterable(callable_, *args, **kwargs)  # pragma: no cover

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

Deprecated alias for trcks.fp.monads.tuple_.tap_to_iterable.

Source code in src/trcks/fp/monads/tuple_.py
788
789
790
791
792
793
794
795
796
@deprecated("Use tap_to_iterable instead")
def tap_to_tuple(
    callable_: Callable[Concatenate[_T1, _P], tuple[object, ...]],
    /,
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> Callable[[tuple[_T1, ...]], tuple[_T1, ...]]:
    """Deprecated alias for [trcks.fp.monads.tuple_.tap_to_iterable][]."""
    return tap_to_iterable(callable_, *args, **kwargs)  # pragma: no cover