Correct exclusive reductions docs: the initial value is not emitted#423
Merged
FranzBusch merged 1 commit intoMay 27, 2026
Conversation
…alue The doc comments for the exclusive reductions overloads stated that the returned sequence is "the initial value followed by the reduced elements". That does not match the behavior: the iterator pulls a base element before emitting anything, so the first emitted element is the result of combining the initial value with the first base element, and the initial value is never emitted. For an empty base sequence the result is empty rather than a single-element sequence containing the initial value. This is the behavior the existing tests already pin down, and changing it would be source-breaking, so this corrects the documentation to match. Updates all four exclusive overloads (throwing and non-throwing, value and into:) and adds empty-base tests covering the now-documented edge case. The inclusive reductions and the DocC Reductions guide already describe the actual behavior and are left unchanged.
FranzBusch
approved these changes
May 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The documentation for the exclusive
reductionsoverloads says the returned sequence is "the initial value followed by the reduced elements", but that is not what happens.AsyncExclusiveReductionsSequence.Iterator.next()pulls a base element before emitting anything, so the first emitted element is the result of combining the initial value with the first base element, and the initial value itself is never emitted. For an empty base sequence the result is empty rather than a single element containing the initial value.This is the behavior the existing tests already encode (for example
[1, 2, 3, 4].async.reductions("") { ... }is expected to produce["1", "12", "123", "1234"], not["", "1", "12", "123", "1234"]), and changing it would be source-breaking. This corrects the documentation to match the implementation instead, as suggested in the issue.Changes:
Returnsline for all four exclusive overloads (throwing and non-throwing, the value-returning andinto:forms) to state that each emitted element is an accumulated result starting from the initial value, that the initial value is not emitted, and that an empty base produces an empty sequence.test_reductions_emptyandtest_throwing_reductions_emptycovering the empty-base case, which was previously untested and is the most surprising consequence of the corrected wording.The inclusive reductions doc comments and the DocC Reductions guide already describe the actual behavior, so they are left unchanged.
Testing:
swift buildandswift testboth pass locally (358 tests, including the two new cases).swift format lint --strictis clean on the changed files.Resolves #386.