Allows to make declarative loops for any ``ApplicativeN`` subtypes. Quick example: .. code:: python >>> from typing import Callable >>> from returns.maybe import Some >>> from returns.iterables import Fold >>> def sum_two(first: in
(
cls,
iterable: Iterable[
KindN[_ApplicativeKind, _FirstType, _SecondType, _ThirdType],
],
acc: KindN[_ApplicativeKind, _UpdatedType, _SecondType, _ThirdType],
function: Callable[
[_FirstType],
Callable[[_UpdatedType], _UpdatedType],
],
)
| 49 | @kinded |
| 50 | @classmethod |
| 51 | def loop( |
| 52 | cls, |
| 53 | iterable: Iterable[ |
| 54 | KindN[_ApplicativeKind, _FirstType, _SecondType, _ThirdType], |
| 55 | ], |
| 56 | acc: KindN[_ApplicativeKind, _UpdatedType, _SecondType, _ThirdType], |
| 57 | function: Callable[ |
| 58 | [_FirstType], |
| 59 | Callable[[_UpdatedType], _UpdatedType], |
| 60 | ], |
| 61 | ) -> KindN[_ApplicativeKind, _UpdatedType, _SecondType, _ThirdType]: |
| 62 | """ |
| 63 | Allows to make declarative loops for any ``ApplicativeN`` subtypes. |
| 64 | |
| 65 | Quick example: |
| 66 | |
| 67 | .. code:: python |
| 68 | |
| 69 | >>> from typing import Callable |
| 70 | >>> from returns.maybe import Some |
| 71 | >>> from returns.iterables import Fold |
| 72 | |
| 73 | >>> def sum_two(first: int) -> Callable[[int], int]: |
| 74 | ... return lambda second: first + second |
| 75 | |
| 76 | >>> assert Fold.loop( |
| 77 | ... [Some(1), Some(2), Some(3)], |
| 78 | ... Some(10), |
| 79 | ... sum_two, |
| 80 | ... ) == Some(16) |
| 81 | |
| 82 | Looks like ``foldl`` in some other languages with some more specifics. |
| 83 | See: https://philipschwarz.dev/fpilluminated/?page_id=348#bwg3/137 |
| 84 | |
| 85 | .. image:: https://i.imgur.com/Tza1isS.jpg |
| 86 | |
| 87 | Is also quite similar to ``reduce``. |
| 88 | |
| 89 | Public interface for ``_loop`` method. Cannot be modified directly. |
| 90 | """ |
| 91 | return cls._loop(iterable, acc, function, _concat_applicative) |
| 92 | |
| 93 | @final |
| 94 | @kinded |