Transforms an iterable of containers into a single container. Quick example for regular containers: .. code:: python >>> from returns.io import IO >>> from returns.iterables import Fold >>> items = [IO(1), IO(2)] >>> assert Fold.co
(
cls,
iterable: Iterable[
KindN[_ApplicativeKind, _FirstType, _SecondType, _ThirdType],
],
acc: KindN[
_ApplicativeKind,
'tuple[_FirstType, ...]',
_SecondType,
_ThirdType,
],
)
| 94 | @kinded |
| 95 | @classmethod |
| 96 | def collect( |
| 97 | cls, |
| 98 | iterable: Iterable[ |
| 99 | KindN[_ApplicativeKind, _FirstType, _SecondType, _ThirdType], |
| 100 | ], |
| 101 | acc: KindN[ |
| 102 | _ApplicativeKind, |
| 103 | 'tuple[_FirstType, ...]', |
| 104 | _SecondType, |
| 105 | _ThirdType, |
| 106 | ], |
| 107 | ) -> KindN[ |
| 108 | _ApplicativeKind, |
| 109 | 'tuple[_FirstType, ...]', |
| 110 | _SecondType, |
| 111 | _ThirdType, |
| 112 | ]: |
| 113 | """ |
| 114 | Transforms an iterable of containers into a single container. |
| 115 | |
| 116 | Quick example for regular containers: |
| 117 | |
| 118 | .. code:: python |
| 119 | |
| 120 | >>> from returns.io import IO |
| 121 | >>> from returns.iterables import Fold |
| 122 | |
| 123 | >>> items = [IO(1), IO(2)] |
| 124 | >>> assert Fold.collect(items, IO(())) == IO((1, 2)) |
| 125 | |
| 126 | If container can have failed values, |
| 127 | then this strategy fails on any existing failed like type. |
| 128 | |
| 129 | It is enough to have even a single failed value in iterable |
| 130 | for this type to convert the whole operation result to be a failure. |
| 131 | Let's see how it works: |
| 132 | |
| 133 | .. code:: python |
| 134 | |
| 135 | >>> from returns.result import Success, Failure |
| 136 | >>> from returns.iterables import Fold |
| 137 | |
| 138 | >>> empty = [] |
| 139 | >>> all_success = [Success(1), Success(2), Success(3)] |
| 140 | >>> has_failure = [Success(1), Failure('a'), Success(3)] |
| 141 | >>> all_failures = [Failure('a'), Failure('b')] |
| 142 | |
| 143 | >>> acc = Success(()) # empty tuple |
| 144 | |
| 145 | >>> assert Fold.collect(empty, acc) == Success(()) |
| 146 | >>> assert Fold.collect(all_success, acc) == Success((1, 2, 3)) |
| 147 | >>> assert Fold.collect(has_failure, acc) == Failure('a') |
| 148 | >>> assert Fold.collect(all_failures, acc) == Failure('a') |
| 149 | |
| 150 | If that's now what you need, check out |
| 151 | :meth:`~AbstractFold.collect_all` |
| 152 | to force collect all non-failed values. |
| 153 |