Skip to content

feat(iter): add iteri and 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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions builtin/builtin.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Copy link
Contributor

@bobzhang bobzhang Apr 28, 2025

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?

Copy link
Collaborator Author

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() {
}

Copy link
Collaborator Author

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]

join(Self[String], String) -> String
just_run[T](Self[T], (T) -> IterResult) -> Unit
last[A](Self[A]) -> A?
Expand Down
20 changes: 20 additions & 0 deletions builtin/iter.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -968,3 +968,23 @@ pub fn Iter::minimum[T : Compare](self : Iter[T]) -> T? {
}
res
}

///|
/// Returns an iterator that iterates over the index and the element of the iterator.
pub fn Iter::iteri[T](self : Iter[T], offset~ : Int = 0) -> Iter2[Int, T] {
fn {
yield_ => {
let mut value = offset - 1
self.run(fn(i) {
value += 1
yield_(value, i)
})
}
}
}

///|
/// Returns an iterator that iterates over the key value pair.
pub fn Iter::iter2[K, V](self : Iter[(K, V)]) -> Iter2[K, V] {
fn { yield_ => self.run(fn { (k, v) => yield_(k, v) }) }
}
28 changes: 28 additions & 0 deletions builtin/iter_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -748,3 +748,31 @@ test "Float::until negative step" {
let result = 10.0.until(0.0, step=-2.0).collect()
inspect!(result, content="[10, 8, 6, 4, 2]")
}

///|
test "iteri" {
let iter = [].iter()
for _, _ in iter.iteri() {
assert_true!(false)
}
let iter = [0, 1, 2].iter()
for i, x in iter.iteri() {
assert_eq!(i, x)
}
let iter = [0, 1, 2].iter()
for i, x in iter.iteri(offset=10) {
assert_eq!(i, x + 10)
}
}

///|
test "iter2" {
let iter = [].iter()
for _, _ in iter.iter2() {
assert_true!(false)
}
let iter = [(0, 1), (2, 3), (4, 5)].iter()
for k, v in iter.iter2() {
assert_eq!(k + 1, v)
}
}