Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,4 @@ Contributors
- Gabrieljr42, `Gabrieljr42@github <https://github.com/Gabrieljr42>`_
- Luxe7, `Luxe7@github <https://github.com/Luxe7>`_
- Shaowei Chen, `123qeew@github <https://github.com/123qeew>`_
- jipeng6036-del, `jipeng6036-del@github <https://github.com/jipeng6036-del>`_
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Changelog
Unreleased
----------

- Preserve original sequence indexes in ``reduce_right`` and ``reductions_right``
callbacks instead of numbering the reversed values again.
- Preserve repeated argument indexes in ``rearg`` instead of silently dropping duplicates.
- Preserve numeric return types, including Decimal, in median and its chain wrapper.
- Apply clone customizations to object attributes without requiring item assignment.
Expand Down
8 changes: 3 additions & 5 deletions src/pydash/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -1644,6 +1644,8 @@ def reduce_right(collection, iteratee=None, accumulator=None):

>>> reduce_right([1, 2, 3, 4], lambda total, x: total**x)
4096
>>> reduce_right([10, 20, 30], lambda total, x, index: total + x * (index + 1), 0)
140

.. versionadded:: 1.0.0

Expand All @@ -1653,11 +1655,7 @@ def reduce_right(collection, iteratee=None, accumulator=None):
.. versionchanged:: 4.0.0
Removed alias ``foldr``.
"""
if isinstance(collection, Mapping):
collection = dict(reversed(list(collection.items())))
else:
collection = list(collection)[::-1]

collection = dict(reversed(list(iterator(collection))))
return reduce_(collection, iteratee, accumulator)


Expand Down
27 changes: 27 additions & 0 deletions tests/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,33 @@
parametrize = pytest.mark.parametrize


@parametrize("factory", [list, tuple, iter])
def test_right_reductions_keep_original_sequence_indexes(factory):
values = [10, 20, 30]

def collect(accumulator, value, index):
return accumulator + [(index, value)]

expected = [(2, 30), (1, 20), (0, 10)]
assert _.reduce_right(factory(values), collect, []) == expected
assert _.reductions_right(factory(values), collect, []) == [
expected[:1],
expected[:2],
expected,
]
assert _.reductions(factory(values), collect, [], from_right=True)[-1] == expected
assert values == [10, 20, 30]


@parametrize("factory", [list, tuple, iter])
def test_right_reductions_keep_indexes_without_an_initial_accumulator(factory):
def weighted_total(accumulator, value, index):
return accumulator + value * (index + 1)

assert _.reduce_right(factory([10, 20, 30]), weighted_total) == 80
assert _.reductions_right(factory([10, 20, 30]), weighted_total) == [70, 80]


@parametrize("factory", [dict, MappingProxyType])
def test_reduce_right_mapping_order(factory):
collection = factory({"a": "A", "b": "B", "c": "C"})
Expand Down