-
Notifications
You must be signed in to change notification settings - Fork 126
feat(iter): add iter2 #2012
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
feat(iter): add iter2 #2012
Conversation
Initial index value could be confusing for empty iteratorsCategory let mut index = 0
self.run(fn(i) {
yield_(index, i)
index += 1
})```
**Reasoning**
Starting with -1 and incrementing before use is less intuitive than starting with 0 and incrementing after use. Both approaches work but the recommended version is more straightforward to reason about, especially when debugging.
</details>
<details>
<summary> Test coverage could be more comprehensive </summary>
**Category**
Maintainability
**Code Snippet**
test "iter2" {
let iter : Iter[Int] = [].iter()
for _, _ in iter.iter2() {
assert_true(false)
}
let iter = [0, 1, 2].iter()
for i, x in iter.iter2() {
assert_eq(i, x)
}
}
**Recommendation**
Add tests for non-numeric types and check actual index values:
```moonbit
test "iter2_strings" {
let iter = ["a", "b", "c"].iter()
let mut expected_idx = 0
for i, _ in iter.iter2() {
assert_eq(i, expected_idx)
expected_idx += 1
}
}```
**Reasoning**
Current tests only cover numeric arrays where indices happen to match values. Testing with non-numeric types would ensure the indexing works correctly regardless of element type.
</details>
<details>
<summary> Documentation could be more detailed </summary>
**Category**
Maintainability
**Code Snippet**
///|
/// Returns an iterator that iterates over the index and the element of the iterator.
**Recommendation**
Add more detailed documentation with examples:
```moonbit
///|
/// Returns an iterator that yields pairs of indices and elements.
/// The indices start from 0 and increment by 1 for each element.
///
/// Example:
/// ```
/// let arr = ["a", "b", "c"].iter()
/// for i, v in arr.iter2() {
/// // i will be 0, 1, 2
/// // v will be "a", "b", "c"
/// }
/// ```
**Reasoning**
More detailed documentation with examples helps users understand the behavior and usage patterns more quickly, reducing potential misuse.
</details> |
Pull Request Test Coverage Report for Build 6460Details
💛 - Coveralls |
builtin/builtin.mbti
Outdated
@@ -236,6 +236,8 @@ impl Iter { | |||
head[A](Self[A]) -> A? | |||
intersperse[A](Self[A], A) -> Self[A] | |||
iter[T](Self[T]) -> Self[T] | |||
iter2[K, V](Self[(K, V)]) -> Iter2[K, V] | |||
iteri[T](Self[T], offset~ : Int = ..) -> Iter2[Int, T] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
iteri
looks reasonable.
What's the benefit of having iter2
, it doe not have perf benefit either?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't have pattern matching for for .. in
, so one would have to write:
let map = [("a", "b"), ("c", "d")].iter()
for v in map {
let (k, v) = v
}
With iter2
, one can at least write:
for k, v in map.iter2() {
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will use iter2 -> Iter2[Int, T]
9941c9f
to
a60e18f
Compare
@Guest0x0 we need to let |
a60e18f
to
156d45c
Compare
Closes #1319
The naming is chosen as such because:
mapi
eachi
where thei
signifies the fact that an index is added as parameterfor i, v in [].iter()
anyway, so there's no ambiguity.Update: we will use
iter2
to returnIter2[Int, T]
to align with existing behaviors. As ofIter[(K, V)]
, users can just use a pattern matchinglet (k, v) = elem
.