Skip to content

trcks.fp.monads.awaitable_tuple

Monadic functions for trcks.AwaitableTuple.

Provides utilities for functional composition of asynchronous homogeneous-tuple-returning functions.

Example

Map and tap over an awaitable homogeneous tuple:

>>> import asyncio
>>> from trcks.fp.composition import pipe
>>> from trcks.fp.monads import awaitable_tuple as at
>>> def double_integer(n: int) -> int:
...     return n * 2
...
>>> def log_integer(n: int) -> None:
...     print(f"Received: {n}")
...
>>> async def main() -> tuple[int, ...]:
...     return await pipe(
...         (
...             at.construct_from_iterable((4, 2, 0)),
...             at.map_(double_integer),
...             at.tap(log_integer),
...         )
...     )
...
>>> tpl = asyncio.run(main())
Received: 8
Received: 4
Received: 0
>>> tpl
(8, 4, 0)

Map each element to an awaitable homogeneous tuple and flatten the result:

>>> import asyncio
>>> from trcks.fp.composition import pipe
>>> from trcks.fp.monads import awaitable_tuple as at
>>> async def slowly_duplicate_integer(n: int) -> tuple[int, int]:
...     await asyncio.sleep(0.001)
...     return n, n
...
>>> async def main() -> tuple[int, ...]:
...     return await pipe(
...         (
...             at.construct_from_iterable((1, 2, 3)),
...             at.map_to_awaitable_iterable(slowly_duplicate_integer),
...         )
...     )
...
>>> asyncio.run(main())
(1, 1, 2, 2, 3, 3)

construct(value)

Create a trcks.AwaitableTuple from a value.

Parameters:

Returns:

Example
>>> import asyncio
>>> from trcks import AwaitableTuple
>>> from trcks.fp.monads import awaitable_tuple as at
>>> a_tpl: AwaitableTuple[int] = at.construct(42)
>>> asyncio.run(at.to_coroutine_tuple(a_tpl))
(42,)
Source code in src/trcks/fp/monads/awaitable_tuple.py
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def construct(value: _T) -> AwaitableTuple[_T]:
    """Create a [trcks.AwaitableTuple][] from a value.

    Args:
        value: The value to create the [trcks.AwaitableTuple][] from.

    Returns:
        The [trcks.AwaitableTuple][] created from the value.

    Example:
        >>> import asyncio
        >>> from trcks import AwaitableTuple
        >>> from trcks.fp.monads import awaitable_tuple as at
        >>> a_tpl: AwaitableTuple[int] = at.construct(42)
        >>> asyncio.run(at.to_coroutine_tuple(a_tpl))
        (42,)
    """
    return a.construct(t.construct(value))

construct_from_awaitable(awtbl)

Create a trcks.AwaitableTuple from an awaitable value.

Parameters:

Returns:

Example
>>> import asyncio
>>> from collections.abc import Awaitable
>>> from trcks import AwaitableTuple
>>> from trcks.fp.monads import awaitable as a
>>> from trcks.fp.monads import awaitable_tuple as at
>>> awtbl: Awaitable[int] = a.construct(7)
>>> a_tpl: AwaitableTuple[int] = at.construct_from_awaitable(awtbl)
>>> asyncio.run(at.to_coroutine_tuple(a_tpl))
(7,)
Source code in src/trcks/fp/monads/awaitable_tuple.py
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
def construct_from_awaitable(awtbl: Awaitable[_T]) -> AwaitableTuple[_T]:
    """Create a [trcks.AwaitableTuple][] from an awaitable value.

    Args:
        awtbl: The awaitable value to create the [trcks.AwaitableTuple][] from.

    Returns:
        The [trcks.AwaitableTuple][] created from the awaitable value.

    Example:
        >>> import asyncio
        >>> from collections.abc import Awaitable
        >>> from trcks import AwaitableTuple
        >>> from trcks.fp.monads import awaitable as a
        >>> from trcks.fp.monads import awaitable_tuple as at
        >>> awtbl: Awaitable[int] = a.construct(7)
        >>> a_tpl: AwaitableTuple[int] = at.construct_from_awaitable(awtbl)
        >>> asyncio.run(at.to_coroutine_tuple(a_tpl))
        (7,)
    """
    return a.map_(t.construct)(awtbl)

construct_from_iterable(it)

Create a trcks.AwaitableTuple from an iterable.

Parameters:

Returns:

Example
>>> import asyncio
>>> from trcks import AwaitableTuple
>>> from trcks.fp.monads import awaitable_tuple as at
>>> a_tpl: AwaitableTuple[int] = at.construct_from_iterable((1, 2))
>>> asyncio.run(at.to_coroutine_tuple(a_tpl))
(1, 2)
Source code in src/trcks/fp/monads/awaitable_tuple.py
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
def construct_from_iterable(it: Iterable[_T]) -> AwaitableTuple[_T]:
    """Create a [trcks.AwaitableTuple][] from an iterable.

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

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

    Example:
        >>> import asyncio
        >>> from trcks import AwaitableTuple
        >>> from trcks.fp.monads import awaitable_tuple as at
        >>> a_tpl: AwaitableTuple[int] = at.construct_from_iterable((1, 2))
        >>> asyncio.run(at.to_coroutine_tuple(a_tpl))
        (1, 2)
    """
    return a.construct(tuple(it))

construct_from_tuple(tpl)

Deprecated alias for trcks.fp.monads.awaitable_tuple.construct_from_iterable.

Source code in src/trcks/fp/monads/awaitable_tuple.py
140
141
142
143
144
145
@deprecated("Use construct_from_iterable instead")
def construct_from_tuple(tpl: tuple[_T, ...]) -> AwaitableTuple[_T]:
    """Deprecated alias for
    [trcks.fp.monads.awaitable_tuple.construct_from_iterable][].
    """
    return construct_from_iterable(tpl)  # pragma: no cover

map_(f)

Turn synchronous function into a function expecting and returning trcks.AwaitableTuples of the same length.

Parameters:

  • f (Callable[[_T1], _T2]) –

    The synchronous function to be transformed into a function expecting and returning trcks.AwaitableTuples of the same length.

Returns:

Note

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

Example
>>> import asyncio
>>> from trcks import AwaitableTuple
>>> from trcks.fp.composition import pipe
>>> from trcks.fp.monads import awaitable_tuple as at
>>> def double_integer(n: int) -> int:
...     return n * 2
>>> a_tpl: AwaitableTuple[int] = pipe(
...     (
...         at.construct_from_iterable((1, 2, 3)),
...         at.map_(double_integer),
...     )
... )
>>> asyncio.run(at.to_coroutine_tuple(a_tpl))
(2, 4, 6)
Source code in src/trcks/fp/monads/awaitable_tuple.py
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
def map_(
    f: Callable[[_T1], _T2],
) -> Callable[[AwaitableTuple[_T1]], AwaitableTuple[_T2]]:
    """Turn synchronous function into a function
    expecting and returning [trcks.AwaitableTuple][]s
    of the same length.

    Args:
        f:
            The synchronous function to be transformed into
            a function expecting and returning
            [trcks.AwaitableTuple][]s of the same length.

    Returns:
        The given function transformed into
            a function expecting and returning
            [trcks.AwaitableTuple][]s of the same length.

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

    Example:
        >>> import asyncio
        >>> from trcks import AwaitableTuple
        >>> from trcks.fp.composition import pipe
        >>> from trcks.fp.monads import awaitable_tuple as at
        >>> def double_integer(n: int) -> int:
        ...     return n * 2
        >>> a_tpl: AwaitableTuple[int] = pipe(
        ...     (
        ...         at.construct_from_iterable((1, 2, 3)),
        ...         at.map_(double_integer),
        ...     )
        ... )
        >>> asyncio.run(at.to_coroutine_tuple(a_tpl))
        (2, 4, 6)
    """
    return a.map_(t.map_(f))

map_to_awaitable(f)

Turn collections.abc.Awaitable-returning function into a function expecting and returning trcks.AwaitableTuples of the same length.

Parameters:

Returns:

Example
>>> import asyncio
>>> from trcks import AwaitableTuple
>>> from trcks.fp.composition import pipe
>>> from trcks.fp.monads import awaitable_tuple as at
>>> async def slowly_add_one(n: int) -> int:
...     await asyncio.sleep(0.001)
...     return n + 1
>>> a_tpl: AwaitableTuple[int] = pipe(
...     (
...         at.construct_from_iterable((1, 2)),
...         at.map_to_awaitable(slowly_add_one),
...     )
... )
>>> asyncio.run(at.to_coroutine_tuple(a_tpl))
(2, 3)
Source code in src/trcks/fp/monads/awaitable_tuple.py
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
def map_to_awaitable(
    f: Callable[[_T1], Awaitable[_T2]],
) -> Callable[[AwaitableTuple[_T1]], AwaitableTuple[_T2]]:
    """Turn [collections.abc.Awaitable][]-returning function into a function
    expecting and returning [trcks.AwaitableTuple][]s
    of the same length.

    Args:
        f:
            The [collections.abc.Awaitable][]-returning function to be transformed
            into a function expecting and returning
            [trcks.AwaitableTuple][]s of the same length.

    Returns:
        The given function transformed into
            a function expecting and returning
            [trcks.AwaitableTuple][]s of the same length.

    Example:
        >>> import asyncio
        >>> from trcks import AwaitableTuple
        >>> from trcks.fp.composition import pipe
        >>> from trcks.fp.monads import awaitable_tuple as at
        >>> async def slowly_add_one(n: int) -> int:
        ...     await asyncio.sleep(0.001)
        ...     return n + 1
        >>> a_tpl: AwaitableTuple[int] = pipe(
        ...     (
        ...         at.construct_from_iterable((1, 2)),
        ...         at.map_to_awaitable(slowly_add_one),
        ...     )
        ... )
        >>> asyncio.run(at.to_coroutine_tuple(a_tpl))
        (2, 3)
    """
    return map_to_awaitable_iterable(compose2((f, construct_from_awaitable)))

map_to_awaitable_iterable(f)

Turn trcks.AwaitableIterable-returning function into a function expecting and returning trcks.AwaitableTuples of varying length.

Parameters:

Returns:

Example
>>> import asyncio
>>> from trcks import AwaitableTuple
>>> from trcks.fp.composition import pipe
>>> from trcks.fp.monads import awaitable_tuple as at
>>> async def slowly_duplicate_integer(n: int) -> tuple[int, int]:
...     await asyncio.sleep(0.001)
...     return n, n
>>> a_tpl: AwaitableTuple[int] = pipe(
...     (
...         at.construct_from_iterable((1, 2)),
...         at.map_to_awaitable_iterable(slowly_duplicate_integer),
...     )
... )
>>> asyncio.run(at.to_coroutine_tuple(a_tpl))
(1, 1, 2, 2)
Source code in src/trcks/fp/monads/awaitable_tuple.py
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
def map_to_awaitable_iterable(
    f: Callable[[_T1], AwaitableIterable[_T2]],
) -> Callable[[AwaitableTuple[_T1]], AwaitableTuple[_T2]]:
    """Turn [trcks.AwaitableIterable][]-returning function into a function
    expecting and returning [trcks.AwaitableTuple][]s
    of varying length.

    Args:
        f:
            The [trcks.AwaitableIterable][]-returning function to be transformed
            into a function expecting and returning
            [trcks.AwaitableTuple][]s of varying length.

    Returns:
        The given function transformed into
            a function expecting and returning
            [trcks.AwaitableTuple][]s of varying length.

    Example:
        >>> import asyncio
        >>> from trcks import AwaitableTuple
        >>> from trcks.fp.composition import pipe
        >>> from trcks.fp.monads import awaitable_tuple as at
        >>> async def slowly_duplicate_integer(n: int) -> tuple[int, int]:
        ...     await asyncio.sleep(0.001)
        ...     return n, n
        >>> a_tpl: AwaitableTuple[int] = pipe(
        ...     (
        ...         at.construct_from_iterable((1, 2)),
        ...         at.map_to_awaitable_iterable(slowly_duplicate_integer),
        ...     )
        ... )
        >>> asyncio.run(at.to_coroutine_tuple(a_tpl))
        (1, 1, 2, 2)
    """

    async def mapped_f(a_t1s: AwaitableTuple[_T1]) -> tuple[_T2, ...]:
        # `tuple` does not support asynchronous generators.
        # Therefore, we need to use a list comprehension and then convert it to a tuple:
        t2s = [t2 for t1 in await a_t1s for t2 in await f(t1)]
        return tuple(t2s)

    return mapped_f

map_to_awaitable_tuple(f)

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

Source code in src/trcks/fp/monads/awaitable_tuple.py
272
273
274
275
276
277
278
279
@deprecated("Use map_to_awaitable_iterable instead")
def map_to_awaitable_tuple(
    f: Callable[[_T1], AwaitableTuple[_T2]],
) -> Callable[[AwaitableTuple[_T1]], AwaitableTuple[_T2]]:
    """Deprecated alias for
    [trcks.fp.monads.awaitable_tuple.map_to_awaitable_iterable][].
    """
    return map_to_awaitable_iterable(f)  # pragma: no cover

map_to_iterable(f)

Turn collections.abc.Iterable-returning function into a function expecting and returning trcks.AwaitableTuples of varying length.

Parameters:

Returns:

Example
>>> import asyncio
>>> from trcks import AwaitableTuple
>>> from trcks.fp.composition import pipe
>>> from trcks.fp.monads import awaitable_tuple as at
>>> def add_negative(n: int) -> tuple[int, int]:
...     return n, -n
>>> a_tpl: AwaitableTuple[int] = pipe(
...     (
...         at.construct_from_iterable((1, 2)),
...         at.map_to_iterable(add_negative),
...     )
... )
>>> asyncio.run(at.to_coroutine_tuple(a_tpl))
(1, -1, 2, -2)
Source code in src/trcks/fp/monads/awaitable_tuple.py
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
def map_to_iterable(
    f: Callable[[_T1], Iterable[_T2]],
) -> Callable[[AwaitableTuple[_T1]], AwaitableTuple[_T2]]:
    """Turn [collections.abc.Iterable][]-returning function into a function
    expecting and returning [trcks.AwaitableTuple][]s
    of varying length.

    Args:
        f:
            The [collections.abc.Iterable][]-returning function to be transformed
            into a function expecting and returning
            [trcks.AwaitableTuple][]s of varying length.

    Returns:
        The given function transformed into
            a function expecting and returning
            [trcks.AwaitableTuple][]s of varying length.

    Example:
        >>> import asyncio
        >>> from trcks import AwaitableTuple
        >>> from trcks.fp.composition import pipe
        >>> from trcks.fp.monads import awaitable_tuple as at
        >>> def add_negative(n: int) -> tuple[int, int]:
        ...     return n, -n
        >>> a_tpl: AwaitableTuple[int] = pipe(
        ...     (
        ...         at.construct_from_iterable((1, 2)),
        ...         at.map_to_iterable(add_negative),
        ...     )
        ... )
        >>> asyncio.run(at.to_coroutine_tuple(a_tpl))
        (1, -1, 2, -2)
    """
    return a.map_(t.map_to_iterable(f))

map_to_tuple(f)

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

Source code in src/trcks/fp/monads/awaitable_tuple.py
319
320
321
322
323
324
@deprecated("Use map_to_iterable instead")
def map_to_tuple(
    f: Callable[[_T1], tuple[_T2, ...]],
) -> Callable[[AwaitableTuple[_T1]], AwaitableTuple[_T2]]:
    """Deprecated alias for [trcks.fp.monads.awaitable_tuple.map_to_iterable][]."""
    return map_to_iterable(f)  # pragma: no cover

tap(f)

Turn synchronous function into a function expecting a trcks.AwaitableTuple and returning the same trcks.AwaitableTuple.

Parameters:

Returns:

Example
>>> import asyncio
>>> from trcks import AwaitableTuple
>>> from trcks.fp.composition import pipe
>>> from trcks.fp.monads import awaitable_tuple as at
>>> def log_integer(n: int) -> None:
...     print(f"Received: {n}")
...
>>> a_tpl: AwaitableTuple[int] = pipe(
...     (
...         at.construct_from_iterable((1, 2)),
...         at.tap(log_integer),
...     )
... )
>>> tpl = asyncio.run(at.to_coroutine_tuple(a_tpl))
Received: 1
Received: 2
>>> tpl
(1, 2)
Source code in src/trcks/fp/monads/awaitable_tuple.py
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
def tap(
    f: Callable[[_T1], object],
) -> Callable[[AwaitableTuple[_T1]], AwaitableTuple[_T1]]:
    """Turn synchronous function into a function
    expecting a [trcks.AwaitableTuple][] and
    returning the same [trcks.AwaitableTuple][].

    Args:
        f:
            The synchronous function to be transformed into a function
            expecting a [trcks.AwaitableTuple][] and
            returning the same [trcks.AwaitableTuple][].

    Returns:
        The given function transformed into a function
            expecting a [trcks.AwaitableTuple][] and
            returning the same [trcks.AwaitableTuple][].

    Example:
        >>> import asyncio
        >>> from trcks import AwaitableTuple
        >>> from trcks.fp.composition import pipe
        >>> from trcks.fp.monads import awaitable_tuple as at
        >>> def log_integer(n: int) -> None:
        ...     print(f"Received: {n}")
        ...
        >>> a_tpl: AwaitableTuple[int] = pipe(
        ...     (
        ...         at.construct_from_iterable((1, 2)),
        ...         at.tap(log_integer),
        ...     )
        ... )
        >>> tpl = asyncio.run(at.to_coroutine_tuple(a_tpl))
        Received: 1
        Received: 2
        >>> tpl
        (1, 2)
    """
    return a.map_(t.tap(f))

tap_to_awaitable(f)

Turn collections.abc.Awaitable-returning function into a function expecting a trcks.AwaitableTuple and returning the same trcks.AwaitableTuple.

Parameters:

Returns:

Example
>>> import asyncio
>>> from trcks import AwaitableTuple
>>> from trcks.fp.composition import pipe
>>> from trcks.fp.monads import awaitable_tuple as at
>>> async def slowly_log_integer(n: int) -> None:
...     await asyncio.sleep(0.001)
...     print(f"Received: {n}")
>>> a_tpl: AwaitableTuple[int] = pipe(
...     (
...         at.construct_from_iterable((1, 2)),
...         at.tap_to_awaitable(slowly_log_integer),
...     )
... )
>>> tpl = asyncio.run(at.to_coroutine_tuple(a_tpl))
Received: 1
Received: 2
>>> tpl
(1, 2)
Source code in src/trcks/fp/monads/awaitable_tuple.py
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
def tap_to_awaitable(
    f: Callable[[_T1], Awaitable[object]],
) -> Callable[[AwaitableTuple[_T1]], AwaitableTuple[_T1]]:
    """Turn [collections.abc.Awaitable][]-returning function into a function
    expecting a [trcks.AwaitableTuple][] and
    returning the same [trcks.AwaitableTuple][].

    Args:
        f:
            The [collections.abc.Awaitable][]-returning function to be transformed
            into a function expecting a [trcks.AwaitableTuple][] and
            returning the same [trcks.AwaitableTuple][].

    Returns:
        The given function transformed into a function
            expecting a [trcks.AwaitableTuple][] and
            returning the same [trcks.AwaitableTuple][].

    Example:
        >>> import asyncio
        >>> from trcks import AwaitableTuple
        >>> from trcks.fp.composition import pipe
        >>> from trcks.fp.monads import awaitable_tuple as at
        >>> async def slowly_log_integer(n: int) -> None:
        ...     await asyncio.sleep(0.001)
        ...     print(f"Received: {n}")
        >>> a_tpl: AwaitableTuple[int] = pipe(
        ...     (
        ...         at.construct_from_iterable((1, 2)),
        ...         at.tap_to_awaitable(slowly_log_integer),
        ...     )
        ... )
        >>> tpl = asyncio.run(at.to_coroutine_tuple(a_tpl))
        Received: 1
        Received: 2
        >>> tpl
        (1, 2)
    """

    async def bypassed_f(t1: _T1) -> _T1:
        _ = await f(t1)
        return t1

    return map_to_awaitable(bypassed_f)

tap_to_awaitable_iterable(f)

Turn a trcks.AwaitableIterable-returning side effect into a function expecting a trcks.AwaitableTuple and returning a trcks.AwaitableTuple where each original element is repeated once per element returned by the side effect.

Parameters:

Returns:

Example
>>> import asyncio
>>> from trcks import AwaitableTuple
>>> from trcks.fp.composition import pipe
>>> from trcks.fp.monads import awaitable_tuple as at
>>> async def slowly_get_divisors(n: int) -> tuple[int, ...]:
...     await asyncio.sleep(0.001)
...     candidates = range(1, n + 1)
...     return tuple(c for c in candidates if n % c == 0)
>>> a_tpl: AwaitableTuple[int] = pipe(
...     (
...         at.construct_from_iterable((1, 2, 3, 4)),
...         at.tap_to_awaitable_iterable(slowly_get_divisors),
...     )
... )
>>> asyncio.run(at.to_coroutine_tuple(a_tpl))
(1, 2, 2, 3, 3, 4, 4, 4)
Source code in src/trcks/fp/monads/awaitable_tuple.py
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
def tap_to_awaitable_iterable(
    f: Callable[[_T1], AwaitableIterable[object]],
) -> Callable[[AwaitableTuple[_T1]], AwaitableTuple[_T1]]:
    """Turn a [trcks.AwaitableIterable][]-returning side effect into a function
    expecting a [trcks.AwaitableTuple][] and returning a [trcks.AwaitableTuple][]
    where each original element is repeated once per element returned by
    the side effect.

    Args:
        f:
            The [trcks.AwaitableIterable][]-returning function to be transformed
            into a function expecting a [trcks.AwaitableTuple][] and
            returning a [trcks.AwaitableTuple][] where each original element is
            repeated once per element returned by the side effect.

    Returns:
        The given function transformed into a function
            expecting a [trcks.AwaitableTuple][] and
            returning a [trcks.AwaitableTuple][] where each original element is
            repeated once per element returned by the side effect.

    Example:
        >>> import asyncio
        >>> from trcks import AwaitableTuple
        >>> from trcks.fp.composition import pipe
        >>> from trcks.fp.monads import awaitable_tuple as at
        >>> async def slowly_get_divisors(n: int) -> tuple[int, ...]:
        ...     await asyncio.sleep(0.001)
        ...     candidates = range(1, n + 1)
        ...     return tuple(c for c in candidates if n % c == 0)
        >>> a_tpl: AwaitableTuple[int] = pipe(
        ...     (
        ...         at.construct_from_iterable((1, 2, 3, 4)),
        ...         at.tap_to_awaitable_iterable(slowly_get_divisors),
        ...     )
        ... )
        >>> asyncio.run(at.to_coroutine_tuple(a_tpl))
        (1, 2, 2, 3, 3, 4, 4, 4)
    """

    async def bypassed_f(t1: _T1) -> tuple[_T1, ...]:
        objs = await f(t1)
        return tuple(t1 for _ in objs)

    return map_to_awaitable_iterable(bypassed_f)

tap_to_awaitable_tuple(f)

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

Source code in src/trcks/fp/monads/awaitable_tuple.py
461
462
463
464
465
466
467
468
@deprecated("Use tap_to_awaitable_iterable instead")
def tap_to_awaitable_tuple(
    f: Callable[[_T1], AwaitableTuple[object]],
) -> Callable[[AwaitableTuple[_T1]], AwaitableTuple[_T1]]:
    """Deprecated alias for
    [trcks.fp.monads.awaitable_tuple.tap_to_awaitable_iterable][].
    """
    return tap_to_awaitable_iterable(f)  # pragma: no cover

tap_to_iterable(f)

Turn a collections.abc.Iterable-returning side effect into a function expecting a trcks.AwaitableTuple and returning a trcks.AwaitableTuple where each original element is repeated once per element returned by the side effect.

Parameters:

Returns:

Example
>>> import asyncio
>>> from trcks import AwaitableTuple
>>> from trcks.fp.composition import pipe
>>> from trcks.fp.monads import awaitable_tuple as at
>>> def get_divisors(n: int) -> tuple[int, ...]:
...     candidates = range(1, n + 1)
...     return tuple(c for c in candidates if n % c == 0)
>>> a_tpl: AwaitableTuple[int] = pipe(
...     (
...         at.construct_from_iterable((1, 2, 3, 4)),
...         at.tap_to_iterable(get_divisors),
...     )
... )
>>> asyncio.run(at.to_coroutine_tuple(a_tpl))
(1, 2, 2, 3, 3, 4, 4, 4)
Source code in src/trcks/fp/monads/awaitable_tuple.py
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
def tap_to_iterable(
    f: Callable[[_T1], Iterable[object]],
) -> Callable[[AwaitableTuple[_T1]], AwaitableTuple[_T1]]:
    """Turn a [collections.abc.Iterable][]-returning side effect into a function
    expecting a [trcks.AwaitableTuple][] and returning a [trcks.AwaitableTuple][]
    where each original element is repeated once per element returned by
    the side effect.

    Args:
        f:
            The [collections.abc.Iterable][]-returning function to be transformed
            into a function expecting a [trcks.AwaitableTuple][] and
            returning a [trcks.AwaitableTuple][] where each original element is
            repeated once per element returned by the side effect.

    Returns:
        The given function transformed into a function
            expecting a [trcks.AwaitableTuple][] and
            returning a [trcks.AwaitableTuple][] where each original element is
            repeated once per element returned by the side effect.

    Example:
        >>> import asyncio
        >>> from trcks import AwaitableTuple
        >>> from trcks.fp.composition import pipe
        >>> from trcks.fp.monads import awaitable_tuple as at
        >>> def get_divisors(n: int) -> tuple[int, ...]:
        ...     candidates = range(1, n + 1)
        ...     return tuple(c for c in candidates if n % c == 0)
        >>> a_tpl: AwaitableTuple[int] = pipe(
        ...     (
        ...         at.construct_from_iterable((1, 2, 3, 4)),
        ...         at.tap_to_iterable(get_divisors),
        ...     )
        ... )
        >>> asyncio.run(at.to_coroutine_tuple(a_tpl))
        (1, 2, 2, 3, 3, 4, 4, 4)
    """
    return a.map_(t.tap_to_iterable(f))

tap_to_tuple(f)

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

Source code in src/trcks/fp/monads/awaitable_tuple.py
512
513
514
515
516
517
@deprecated("Use tap_to_iterable instead")
def tap_to_tuple(
    f: Callable[[_T1], tuple[object, ...]],
) -> Callable[[AwaitableTuple[_T1]], AwaitableTuple[_T1]]:
    """Deprecated alias for [trcks.fp.monads.awaitable_tuple.tap_to_iterable][]."""
    return tap_to_iterable(f)  # pragma: no cover

to_coroutine_tuple(a_tpl) async

Turn a trcks.AwaitableTuple into a coroutine.

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

Parameters:

Returns:

Note

The type trcks.AwaitableTuple is an alias of collections.abc.Awaitable over homogeneous tuple values.

Example
>>> import asyncio
>>> from trcks import AwaitableTuple
>>> from trcks.fp.monads import awaitable_tuple as at
>>> loop = asyncio.new_event_loop()
>>> future: asyncio.Future[tuple[int, ...]] = loop.create_future()
>>> future.set_result((3, 4))
>>> future
<Future finished result=(3, 4)>
>>> coro = at.to_coroutine_tuple(future)
>>> coro
<coroutine object to_coroutine_tuple at 0x...>
>>> loop.run_until_complete(coro)
(3, 4)
>>> loop.close()
Source code in src/trcks/fp/monads/awaitable_tuple.py
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
546
547
548
549
550
551
552
553
554
555
async def to_coroutine_tuple(a_tpl: AwaitableTuple[_T]) -> tuple[_T, ...]:
    """Turn a [trcks.AwaitableTuple][] into a coroutine.

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

    Args:
        a_tpl: The [trcks.AwaitableTuple][] to be transformed
            into a coroutine.

    Returns:
        The given [trcks.AwaitableTuple][] transformed
            into a coroutine.

    Note:
        The type [trcks.AwaitableTuple][] is
        an alias of [collections.abc.Awaitable][] over
        homogeneous [tuple][] values.

    Example:
        >>> import asyncio
        >>> from trcks import AwaitableTuple
        >>> from trcks.fp.monads import awaitable_tuple as at
        >>> loop = asyncio.new_event_loop()
        >>> future: asyncio.Future[tuple[int, ...]] = loop.create_future()
        >>> future.set_result((3, 4))
        >>> future
        <Future finished result=(3, 4)>
        >>> coro = at.to_coroutine_tuple(future)
        >>> coro
        <coroutine object to_coroutine_tuple at 0x...>
        >>> loop.run_until_complete(coro)
        (3, 4)
        >>> loop.close()
    """
    return await a_tpl