trcks.fp.monads.awaitable_tuple
Monadic functions for trcks.AwaitableTuple.
Provides utilities for functional composition of asynchronous homogeneous-tuple-returning functions.
Examples:
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:
-
value(_T) –The value to create the trcks.AwaitableTuple from.
Returns:
-
AwaitableTuple[_T]–The trcks.AwaitableTuple created from the value.
Examples:
>>> 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
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | |
construct_from_awaitable(awtbl)
Create a trcks.AwaitableTuple from an awaitable value.
Parameters:
-
awtbl(Awaitable[_T]) –The awaitable value to create the trcks.AwaitableTuple from.
Returns:
-
AwaitableTuple[_T]–The trcks.AwaitableTuple created from the awaitable value.
Examples:
>>> 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
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | |
construct_from_awaitable_iterable(a_it)
Create a trcks.AwaitableTuple from a trcks.AwaitableIterable.
Parameters:
-
a_it(AwaitableIterable[_T]) –The trcks.AwaitableIterable to create the trcks.AwaitableTuple from.
Returns:
-
AwaitableTuple[_T]–The trcks.AwaitableTuple created from the trcks.AwaitableIterable.
Examples:
>>> import asyncio
>>> from trcks import AwaitableIterable, AwaitableTuple
>>> from trcks.fp.monads import awaitable as a
>>> from trcks.fp.monads import awaitable_tuple as at
>>> a_it: AwaitableIterable[int] = a.construct([1, 2])
>>> a_tpl: AwaitableTuple[int] = at.construct_from_awaitable_iterable(a_it)
>>> asyncio.run(at.to_coroutine_tuple(a_tpl))
(1, 2)
Source code in src/trcks/fp/_monads/awaitable_tuple.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
construct_from_awaitable_tuple(a_tpl)
Deprecated alias for trcks.fp.monads.awaitable_tuple.construct_from_awaitable_iterable.
Source code in src/trcks/fp/monads/awaitable_tuple.py
130 131 132 133 134 135 136 137 138 | |
construct_from_iterable(it)
Create a trcks.AwaitableTuple from an iterable.
Parameters:
-
it(Iterable[_T]) –The iterable to create the trcks.AwaitableTuple from.
Returns:
-
AwaitableTuple[_T]–The trcks.AwaitableTuple created from the iterable.
Examples:
>>> 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
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | |
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
141 142 143 144 145 146 | |
map_(callable_, /, *args, **kwargs)
Turn synchronous function into a function expecting and returning trcks.AwaitableTuples of the same length.
Parameters:
-
callable_(Callable[Concatenate[_T1, _P], _T2]) –The synchronous function to be transformed into a function expecting and returning trcks.AwaitableTuples of the same length.
-
*args(args, default:()) –Positional arguments to be passed to
callable_. -
**kwargs(kwargs, default:{}) –Keyword arguments to be passed to
callable_.
Returns:
-
Callable[[AwaitableTuple[_T1]], AwaitableTuple[_T2]]–The given function transformed into a function expecting and returning trcks.AwaitableTuples of the same length.
Note
The underscore in the function name helps to avoid collisions with the built-in function map.
Examples:
>>> 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
113 114 115 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 152 153 154 155 156 | |
map_to_awaitable(callable_, /, *args, **kwargs)
Turn collections.abc.Awaitable-returning function into a function expecting and returning trcks.AwaitableTuples of the same length.
Parameters:
-
callable_(Callable[Concatenate[_T1, _P], Awaitable[_T2]]) –The collections.abc.Awaitable-returning function to be transformed into a function expecting and returning trcks.AwaitableTuples of the same length.
-
*args(args, default:()) –Positional arguments to be passed to
callable_. -
**kwargs(kwargs, default:{}) –Keyword arguments to be passed to
callable_.
Returns:
-
Callable[[AwaitableTuple[_T1]], AwaitableTuple[_T2]]–The given function transformed into a function expecting and returning trcks.AwaitableTuples of the same length.
Examples:
>>> 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
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | |
map_to_awaitable_iterable(callable_, /, *args, **kwargs)
Turn trcks.AwaitableIterable-returning function into a function expecting and returning trcks.AwaitableTuples of varying length.
Parameters:
-
callable_(Callable[Concatenate[_T1, _P], AwaitableIterable[_T2]]) –The trcks.AwaitableIterable-returning function to be transformed into a function expecting and returning trcks.AwaitableTuples of varying length.
-
*args(args, default:()) –Positional arguments to be passed to
callable_. -
**kwargs(kwargs, default:{}) –Keyword arguments to be passed to
callable_.
Returns:
-
Callable[[AwaitableTuple[_T1]], AwaitableTuple[_T2]]–The given function transformed into a function expecting and returning trcks.AwaitableTuples of varying length.
Examples:
>>> 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
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | |
map_to_awaitable_result(callable_, /, *args, **kwargs)
Create function that maps each element of a trcks.AwaitableTuple 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:
-
Callable[[AwaitableTuple[_T1]], AwaitableResultTuple[_F, _S]]–Maps trcks.AwaitableTuples to trcks.AwaitableResultTuples 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 awaitable_tuple as at
>>> 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 = (
... at.map_to_awaitable_result(slowly_double_if_positive)
... )
>>> a_r_tpl_1 = double_if_positive(at.construct_from_iterable((1, 2)))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
('success', (2, 4))
>>> a_r_tpl_2 = double_if_positive(at.construct_from_iterable((1, -1, 2)))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
('failure', 'negative')
Source code in src/trcks/fp/monads/awaitable_tuple.py
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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | |
map_to_awaitable_result_iterable(callable_, /, *args, **kwargs)
Create function that maps each element of a trcks.AwaitableTuple to a trcks.AwaitableResultIterable and flattens the result.
Parameters:
-
callable_(Callable[Concatenate[_T1, _P], AwaitableResultIterable[_F, _S]]) –Asynchronous function to apply to each element, returning a trcks.AwaitableResultIterable.
-
*args(args, default:()) –Positional arguments to be passed to
callable_. -
**kwargs(kwargs, default:{}) –Keyword arguments to be passed to
callable_.
Returns:
-
Callable[[AwaitableTuple[_T1]], AwaitableResultTuple[_F, _S]]–Maps trcks.AwaitableTuples to trcks.AwaitableResultTuples 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 awaitable_tuple as at
>>> 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 = (
... at.map_to_awaitable_result_iterable(slowly_expand_if_positive)
... )
>>> a_r_tpl_1 = expand_if_positive(at.construct_from_iterable((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(at.construct_from_iterable((1, -1, 2)))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
('failure', 'negative')
Source code in src/trcks/fp/monads/awaitable_tuple.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | |
map_to_awaitable_result_tuple(callable_, /, *args, **kwargs)
Deprecated alias for trcks.fp.monads.awaitable_tuple.map_to_awaitable_result_iterable.
Source code in src/trcks/fp/monads/awaitable_tuple.py
261 262 263 264 265 266 267 268 269 270 271 272 273 | |
map_to_awaitable_tuple(callable_, /, *args, **kwargs)
Deprecated alias for trcks.fp.monads.awaitable_tuple.map_to_awaitable_iterable.
Source code in src/trcks/fp/monads/awaitable_tuple.py
276 277 278 279 280 281 282 283 284 285 286 | |
map_to_iterable(callable_, /, *args, **kwargs)
Turn collections.abc.Iterable-returning function into a function expecting and returning trcks.AwaitableTuples of varying length.
Parameters:
-
callable_(Callable[Concatenate[_T1, _P], Iterable[_T2]]) –The collections.abc.Iterable-returning function to be transformed into a function expecting and returning trcks.AwaitableTuples of varying length.
-
*args(args, default:()) –Positional arguments to be passed to
callable_. -
**kwargs(kwargs, default:{}) –Keyword arguments to be passed to
callable_.
Returns:
-
Callable[[AwaitableTuple[_T1]], AwaitableTuple[_T2]]–The given function transformed into a function expecting and returning trcks.AwaitableTuples of varying length.
Examples:
>>> 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
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 | |
map_to_result(callable_, /, *args, **kwargs)
Create function that maps each element of a trcks.AwaitableTuple to a trcks.Result value.
Parameters:
-
callable_(Callable[Concatenate[_T1, _P], Result[_F, _S]]) –Synchronous function 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:
-
Callable[[AwaitableTuple[_T1]], AwaitableResultTuple[_F, _S]]–Maps trcks.AwaitableTuples to trcks.AwaitableResultTuples 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:
>>> import asyncio
>>> from trcks import Result
>>> from trcks.fp.monads import awaitable_result_tuple as art
>>> from trcks.fp.monads import awaitable_tuple as at
>>> def double_if_positive(n: int) -> Result[str, int]:
... if n > 0:
... return "success", n * 2
... return "failure", "negative"
...
>>> double_result = at.map_to_result(double_if_positive)
>>> a_r_tpl_1 = double_result(at.construct_from_iterable((1, 2)))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
('success', (2, 4))
>>> a_r_tpl_2 = double_result(at.construct_from_iterable((1, -1, 2)))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
('failure', 'negative')
Source code in src/trcks/fp/monads/awaitable_tuple.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 | |
map_to_result_iterable(callable_, /, *args, **kwargs)
Create function that maps each element of a trcks.AwaitableTuple 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:
-
Callable[[AwaitableTuple[_T1]], AwaitableResultTuple[_F, _S]]–Maps trcks.AwaitableTuples to trcks.AwaitableResultTuples 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 awaitable_tuple as at
>>> def expand_if_positive(n: int) -> ResultTuple[str, int]:
... if n > 0:
... return "success", (n, -n)
... return "failure", "negative"
...
>>> expand_result = at.map_to_result_iterable(expand_if_positive)
>>> a_r_tpl_1 = expand_result(at.construct_from_iterable((1, 2)))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
('success', (1, -1, 2, -2))
>>> a_r_tpl_2 = expand_result(at.construct_from_iterable((1, -1, 2)))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
('failure', 'negative')
Source code in src/trcks/fp/monads/awaitable_tuple.py
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 | |
map_to_result_tuple(callable_, /, *args, **kwargs)
Deprecated alias for trcks.fp.monads.awaitable_tuple.map_to_result_iterable.
Source code in src/trcks/fp/monads/awaitable_tuple.py
395 396 397 398 399 400 401 402 403 404 405 | |
map_to_tuple(callable_, /, *args, **kwargs)
Deprecated alias for trcks.fp.monads.awaitable_tuple.map_to_iterable.
Source code in src/trcks/fp/monads/awaitable_tuple.py
408 409 410 411 412 413 414 415 416 | |
tap(callable_, /, *args, **kwargs)
Turn synchronous function into a function expecting a trcks.AwaitableTuple and returning the same trcks.AwaitableTuple.
Parameters:
-
callable_(Callable[Concatenate[_T1, _P], object]) –The synchronous function to be transformed into a function expecting a trcks.AwaitableTuple and returning the same trcks.AwaitableTuple.
-
*args(args, default:()) –Positional arguments to be passed to
callable_. -
**kwargs(kwargs, default:{}) –Keyword arguments to be passed to
callable_.
Returns:
-
Callable[[AwaitableTuple[_T1]], AwaitableTuple[_T1]]–The given function transformed into a function expecting a trcks.AwaitableTuple and returning the same trcks.AwaitableTuple.
Examples:
>>> 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
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 | |
tap_to_awaitable(callable_, /, *args, **kwargs)
Turn collections.abc.Awaitable-returning function into a function expecting a trcks.AwaitableTuple and returning the same trcks.AwaitableTuple.
Parameters:
-
callable_(Callable[Concatenate[_T1, _P], Awaitable[object]]) –The collections.abc.Awaitable-returning function to be transformed into a function expecting a trcks.AwaitableTuple and returning the same trcks.AwaitableTuple.
-
*args(args, default:()) –Positional arguments to be passed to
callable_. -
**kwargs(kwargs, default:{}) –Keyword arguments to be passed to
callable_.
Returns:
-
Callable[[AwaitableTuple[_T1]], AwaitableTuple[_T1]]–The given function transformed into a function expecting a trcks.AwaitableTuple and returning the same trcks.AwaitableTuple.
Examples:
>>> 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
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 | |
tap_to_awaitable_iterable(callable_, /, *args, **kwargs)
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:
-
callable_(Callable[Concatenate[_T1, _P], AwaitableIterable[object]]) –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.
-
*args(args, default:()) –Positional arguments to be passed to
callable_. -
**kwargs(kwargs, default:{}) –Keyword arguments to be passed to
callable_.
Returns:
-
Callable[[AwaitableTuple[_T1]], AwaitableTuple[_T1]]–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.
Examples:
>>> 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
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 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 | |
tap_to_awaitable_result(callable_, /, *args, **kwargs)
Create function that applies an asynchronous side effect with return type trcks.AwaitableResult to each element of a trcks.AwaitableTuple.
Parameters:
-
callable_(Callable[Concatenate[_T1, _P], AwaitableResult[_F, object]]) –Asynchronous side effect 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:
-
Callable[[AwaitableTuple[_T1]], AwaitableResultTuple[_F, _T1]]–Maps trcks.AwaitableTuples to trcks.AwaitableResultTuples with
- the returned trcks.Failure if the applied side effect returns a trcks.Failure for an element, or
- the original elements 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 awaitable_tuple as at
>>> 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 = at.tap_to_awaitable_result(slowly_check_if_positive)
>>> a_r_tpl_1 = check_if_positive(at.construct_from_iterable((1, 2)))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
('success', (1, 2))
>>> a_r_tpl_2 = check_if_positive(at.construct_from_iterable((1, -1, 2)))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
('failure', 'negative')
Source code in src/trcks/fp/monads/awaitable_tuple.py
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 459 460 461 462 463 464 465 466 467 468 469 470 471 472 | |
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 trcks.AwaitableTuple.
Parameters:
-
callable_(Callable[Concatenate[_T1, _P], AwaitableResultIterable[_F, object]]) –Asynchronous side effect to apply to each element, returning a trcks.AwaitableResultIterable.
-
*args(args, default:()) –Positional arguments to be passed to
callable_. -
**kwargs(kwargs, default:{}) –Keyword arguments to be passed to
callable_.
Returns:
-
Callable[[AwaitableTuple[_T1]], AwaitableResultTuple[_F, _T1]]–Maps trcks.AwaitableTuples to trcks.AwaitableResultTuples with
- the returned trcks.Failure if the applied side effect returns a trcks.Failure for an element, or
- the original elements 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 awaitable_tuple as at
>>> async def slowly_audit(n: int) -> ResultTuple[str, None]:
... await asyncio.sleep(0.001)
... if n > 0:
... return "success", (None, None)
... return "failure", "negative"
...
>>> audit_successes = at.tap_to_awaitable_result_iterable(slowly_audit)
>>> a_r_tpl_1 = audit_successes(at.construct_from_iterable((1, 2)))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
('success', (1, 1, 2, 2))
>>> a_r_tpl_2 = audit_successes(at.construct_from_iterable((1, -1, 2)))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
('failure', 'negative')
Source code in src/trcks/fp/monads/awaitable_tuple.py
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 | |
tap_to_awaitable_result_tuple(callable_, /, *args, **kwargs)
Deprecated alias for trcks.fp.monads.awaitable_tuple.tap_to_awaitable_result_iterable.
Source code in src/trcks/fp/monads/awaitable_tuple.py
532 533 534 535 536 537 538 539 540 541 542 543 544 | |
tap_to_awaitable_tuple(callable_, /, *args, **kwargs)
Deprecated alias for trcks.fp.monads.awaitable_tuple.tap_to_awaitable_iterable.
Source code in src/trcks/fp/monads/awaitable_tuple.py
547 548 549 550 551 552 553 554 555 556 557 | |
tap_to_iterable(callable_, /, *args, **kwargs)
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:
-
callable_(Callable[Concatenate[_T1, _P], Iterable[object]]) –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.
-
*args(args, default:()) –Positional arguments to be passed to
callable_. -
**kwargs(kwargs, default:{}) –Keyword arguments to be passed to
callable_.
Returns:
-
Callable[[AwaitableTuple[_T1]], AwaitableTuple[_T1]]–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.
Examples:
>>> 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
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 | |
tap_to_result(callable_, /, *args, **kwargs)
Create function that applies a synchronous side effect with return type trcks.Result to each element of a trcks.AwaitableTuple.
Parameters:
-
callable_(Callable[Concatenate[_T1, _P], Result[_F, object]]) –Synchronous 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:
-
Callable[[AwaitableTuple[_T1]], AwaitableResultTuple[_F, _T1]]–Maps trcks.AwaitableTuples to trcks.AwaitableResultTuples with
- the returned trcks.Failure if the applied side effect returns a trcks.Failure for an element, or
- the original elements 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 awaitable_tuple as at
>>> def check_if_positive(n: int) -> Result[str, None]:
... if n > 0:
... return "success", None
... return "failure", "negative"
...
>>> check_if_positive_sync = at.tap_to_result(check_if_positive)
>>> a_r_tpl_1 = check_if_positive_sync(at.construct_from_iterable((1, 2)))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
('success', (1, 2))
>>> a_r_tpl_2 = check_if_positive_sync(at.construct_from_iterable((1, -1, 2)))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
('failure', 'negative')
Source code in src/trcks/fp/monads/awaitable_tuple.py
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 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 | |
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 trcks.AwaitableTuple.
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:
-
Callable[[AwaitableTuple[_T1]], AwaitableResultTuple[_F, _T1]]–Maps trcks.AwaitableTuples to trcks.AwaitableResultTuples with
- the returned trcks.Failure if the applied side effect returns a trcks.Failure for an element, or
- the original elements 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 awaitable_tuple as at
>>> def audit(n: int) -> ResultTuple[str, None]:
... if n > 0:
... return "success", (None, None)
... return "failure", "negative"
...
>>> audit_successes_sync = at.tap_to_result_iterable(audit)
>>> a_r_tpl_1 = audit_successes_sync(at.construct_from_iterable((1, 2)))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_1))
('success', (1, 1, 2, 2))
>>> a_r_tpl_2 = audit_successes_sync(at.construct_from_iterable((1, -1, 2)))
>>> asyncio.run(art.to_coroutine_result_tuple(a_r_tpl_2))
('failure', 'negative')
Source code in src/trcks/fp/monads/awaitable_tuple.py
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 | |
tap_to_result_tuple(callable_, /, *args, **kwargs)
Deprecated alias for trcks.fp.monads.awaitable_tuple.tap_to_result_iterable.
Source code in src/trcks/fp/monads/awaitable_tuple.py
671 672 673 674 675 676 677 678 679 680 681 | |
tap_to_tuple(callable_, /, *args, **kwargs)
Deprecated alias for trcks.fp.monads.awaitable_tuple.tap_to_iterable.
Source code in src/trcks/fp/monads/awaitable_tuple.py
684 685 686 687 688 689 690 691 692 | |
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:
-
a_tpl(AwaitableTuple[_T]) –The trcks.AwaitableTuple to be transformed into a coroutine.
Returns:
-
tuple[_T, ...]–The given trcks.AwaitableTuple transformed into a coroutine.
Note
The type trcks.AwaitableTuple is an alias of collections.abc.Awaitable over homogeneous tuple values.
Examples:
>>> 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
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 | |