8000 feat(iter): implement last method for Iter to return the last element or None by bobzhang · Pull Request #2240 · moonbitlang/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat(iter): implement last method for Iter to return the last element or None #2240

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

Merged
merged 1 commit into from
Jun 10, 2025

Conversation

bobzhang
Copy link
Contributor

No description provided.

Copy link
peter-jerry-ye-code-review bot commented Jun 10, 2025
Improved implementation of Iter::last by removing unnecessary fold operation

Category
Performance
Code Snippet
pub fn[A] Iter::last(self : Iter[A]) -> A? {
let mut last = None
for a in self {
last = Some(a)
}
last
}
Recommendation
The new implementation is actually better. Keep as is.
Reasoning
The new implementation is more straightforward and likely more performant as it avoids the overhead of creating and calling a closure function for each element. The previous implementation using fold was doing the same work but with additional function call overhead.

Added #locals(f) directive to fold function

Category
Maintainability
Code Snippet
#intrinsic("%iter.reduce")
#locals(f)
pub fn[T, B] Iter::fold(self : Iter[T], init~ : B, f : (B, T) -> B) -> B
Recommendation
Consider adding a comment explaining the purpose of the #locals directive for future maintainers
Reasoning
The #locals directive appears to be a compiler hint or optimization directive, but its purpose is not immediately clear to readers. Documentation would help future maintainers understand its significance.

Both implementations of last maintain correct behavior

Category
Correctness
Code Snippet
// Old:
fn f(_a : A?, b : A) {
Some(b)
}
self.fold(init=None, f)

// New:
let mut last = None
for a in self {
last = Some(a)
}
Recommendation
The new implementation is correct. No changes needed.
Reasoning
Both implementations correctly return None for empty iterators and Some(last_element) for non-empty iterators. The new implementation is more explicit about its behavior and easier to verify for correctness.

@bobzhang bobzhang force-pushed the hongbo/last_tweak branch from 4a4faa9 to f523b83 Compare June 10, 2025 14:20
@bobzhang bobzhang enabled auto-merge (rebase) June 10, 2025 14:20
@bobzhang bobzhang merged commit 16a2c20 into main Jun 10, 2025
11 of 12 checks passed
@bobzhang bobzhang deleted the hongbo/last_tweak branch June 10, 2025 14:31
@coveralls
Copy link
Collaborator
coveralls commented Jun 10, 2025

Pull Request Test Coverage Report for Build 7187

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 1 of 1 (100.0%) changed or added relevant line in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.001%) to 93.105%

Totals Coverage Status
Change from base Build 7185: -0.001%
Covered Lines: 8574
Relevant Lines: 9209

💛 - Coveralls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0