-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[SR-11841] Lazy filter runs in unexpected order #54246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Comment by Diego Torres (JIRA) It is indeed wrong. Thanks @natecook1000 |
Comment by Diego Torres (JIRA) And a PR to fix it #28462 |
An interesting example that came up discussing this bug, just to demonstrate how insidious it can be: var valuesEncountered = Set<String>()
var result = ["bangle", "bannister", "banjo"]
.filter({ valuesEncountered.insert($0).inserted })
.filter({ valuesEncountered.insert(String($0.prefix(3))).inserted })
print(valuesEncountered) // ["bangle", "bannister", "banjo", "ban”] …as expected. But make it lazy, which should give the same results: var valuesEncountered = Set<String>()
var result = ["bangle", "bannister", "banjo"]
.lazy // go lazy
.filter({ valuesEncountered.insert($0).inserted })
.filter({ valuesEncountered.insert(String($0.prefix(3))).inserted })
Array(result) // force evaluation
print(valuesEncountered) // ["ban", "bangle”] ← oops! But then if we trigger lazy evaluation with a contextual type instead of an explicit Array(…) call: var valuesEncountered = Set<String>()
// Now [String] type forces evaluation instead
var result: [String] = ["bangle", "bannister", "banjo"]
.lazy // go lazy
.filter({ valuesEncountered.insert($0).inserted })
.filter({ valuesEncountered.insert(String($0.prefix(3))).inserted })
print(valuesEncountered) // ["bangle", "ban", "banjo", "bannister”] |
@swift-ci create |
Just as a note, that last example isn't using lazy evaluation — providing the type of the result as |
Fixed in #28462 |
Environment
Xcode 11.2.1
Additional Detail from JIRA
md5: 281a0f54ddfb46f82336abd5abaa9562
Issue Description:
For the following code:
Actual Results:
Expected Results:
I would expect a cascade behaviour of A filtering before B*. However, this prints:
*Not to confuse with non lazy behaviour of AAABBB which is indeed correct
The text was updated successfully, but these errors were encountered: