|
| 1 | +from typing import Any, Callable, Iterable, Optional, TypeVar, overload |
| 2 | + |
| 3 | +_ValueType = TypeVar('_ValueType') |
| 4 | +_NewValueType = TypeVar('_NewValueType') |
| 5 | + |
| 6 | +_AccValueType = TypeVar('_AccValueType') |
| 7 | + |
| 8 | + |
| 9 | +class Reduced(object): |
| 10 | + """ |
| 11 | + Sentinel for early termination inside transducer. |
| 12 | +
|
| 13 | + .. code:: python |
| 14 | +
|
| 15 | + >>> from returns.transducers import tmap, transduce, Reduced |
| 16 | +
|
| 17 | + >>> def add_one(number: int) -> int: |
| 18 | + ... return number + 1 |
| 19 | +
|
| 20 | + >>> def add(acc: int, number: int) -> int: |
| 21 | + ... if acc == 3: |
| 22 | + ... return Reduced(acc) |
| 23 | + ... return acc + number |
| 24 | +
|
| 25 | + >>> my_list = [0, 1, 2] |
| 26 | + >>> assert transduce(tmap(add_one), add, 0, my_list) == 3 |
| 27 | +
|
| 28 | + """ |
| 29 | + |
| 30 | + _inner_value: Any |
| 31 | + |
| 32 | + def __init__(self, inner_value: Any) -> None: |
| 33 | + self._inner_value = inner_value |
| 34 | + |
| 35 | + @property |
| 36 | + def value(self) -> Any: |
| 37 | + return self._inner_value |
| 38 | + |
| 39 | + |
| 40 | +class _Missing(object): |
| 41 | + """Represents a missing value for reducers.""" |
| 42 | + |
| 43 | + _instance: Optional['_Missing'] = None |
| 44 | + |
| 45 | + def __new__(cls, *args: Any, **kwargs: Any) -> '_Missing': |
| 46 | + if cls._instance is None: |
| 47 | + cls._instance = object.__new__(cls) # noqa: WPS609 |
| 48 | + return cls._instance |
| 49 | + |
| 50 | + |
| 51 | +#: A singleton representing any missing value |
| 52 | +Missing = _Missing() |
| 53 | + |
| 54 | + |
| 55 | +def transduce( |
| 56 | + xform: Callable[ |
| 57 | + [Callable[[_AccValueType, _ValueType], _AccValueType]], |
| 58 | + Callable[[_AccValueType, _ValueType], _AccValueType], |
| 59 | + ], |
| 60 | + reducing_function: Callable[[_AccValueType, _ValueType], _AccValueType], |
| 61 | + initial: _AccValueType, |
| 62 | + iterable: Iterable[_ValueType], |
| 63 | +) -> _AccValueType: |
| 64 | + """ |
| 65 | + Process information with transducers. |
| 66 | +
|
| 67 | + .. code:: python |
| 68 | +
|
| 69 | + >>> from returns.transducers import tmap, transduce |
| 70 | +
|
| 71 | + >>> def add_one(number: int) -> int: |
| 72 | + ... return number + 1 |
| 73 | +
|
| 74 | + >>> def add(acc: int, number: int) -> int: |
| 75 | + ... return acc + number |
| 76 | +
|
| 77 | + >>> my_list = [0, 1, 2] |
| 78 | + >>> assert transduce(tmap(add_one), add, 0, my_list) == 6 |
| 79 | + """ |
| 80 | + reducer = xform(reducing_function) |
| 81 | + return reduce(reducer, iterable, initial) |
| 82 | + |
| 83 | + |
| 84 | +@overload |
| 85 | +def reduce( |
| 86 | + function: Callable[[_ValueType, _ValueType], _ValueType], |
| 87 | + iterable: Iterable[_ValueType], |
| 88 | + initial: _Missing = Missing, |
| 89 | +) -> _ValueType: |
| 90 | + """Reduce without an initial value.""" |
| 91 | + |
| 92 | + |
| 93 | +@overload |
| 94 | +def reduce( |
| 95 | + function: Callable[[_AccValueType, _ValueType], _AccValueType], |
| 96 | + iterable: Iterable[_ValueType], |
| 97 | + initial: _AccValueType, |
| 98 | +) -> _AccValueType: |
| 99 | + """Reduce with an initial value.""" |
| 100 | + |
| 101 | + |
| 102 | +def reduce(function, iterable, initial=Missing): |
| 103 | + """ |
| 104 | + A rewritten version of :func:`reduce <functools.reduce>`. |
| 105 | +
|
| 106 | + This version considers some features borrowed from Clojure: |
| 107 | +
|
| 108 | + - Early termination |
| 109 | + - Function initializer [TODO] |
| 110 | +
|
| 111 | + You can use it as a normal reduce if you want: |
| 112 | +
|
| 113 | + .. code:: python |
| 114 | +
|
| 115 | + >>> from returns.transducers import reduce |
| 116 | +
|
| 117 | + >>> def add(acc: int, value: int) -> int: |
| 118 | + ... return acc + value |
| 119 | +
|
| 120 | + >>> assert reduce(add, [1, 2, 3]) == 6 |
| 121 | +
|
| 122 | + """ |
| 123 | + it = iter(iterable) |
| 124 | + |
| 125 | + if initial is Missing: |
| 126 | + try: |
| 127 | + acc_value = next(it) |
| 128 | + except StopIteration: |
| 129 | + raise TypeError( |
| 130 | + 'reduce() of empty iterable with no initial value', |
| 131 | + ) from None |
| 132 | + else: |
| 133 | + acc_value = initial |
| 134 | + |
| 135 | + for value in it: # noqa: WPS110 |
| 136 | + acc_value = function(acc_value, value) |
| 137 | + if isinstance(acc_value, Reduced): |
| 138 | + return acc_value.value |
| 139 | + return acc_value |
0 commit comments