Skip to content

[Feature Request] impl Iter2 for Iter[(A, B)] #1319

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
hackwaly opened this issue Dec 11, 2024 · 9 comments · May be fixed by #2012
Open

[Feature Request] impl Iter2 for Iter[(A, B)] #1319

hackwaly opened this issue Dec 11, 2024 · 9 comments · May be fixed by #2012

Comments

@hackwaly
Copy link
Contributor

No description provided.

@illusory0x0
Copy link
Contributor

https://github.com/moonbitlang/core/blob/main/builtin/iter2.mbt#L67

we can use Iter2::iter() to transform Iter2[A,B] into Iter[(A,b)]

@Yu-zh
Copy link
Collaborator

Yu-zh commented Mar 11, 2025

We need to think about which one is more preferable because we cannot have both under the same name.

fn Iter::iter2[T](self : Iter[T]) -> Iter2[Int, T]
fn Iter::iter2[A, B](self : Iter[(A, B)]) -> Iter2[A, B]

@illusory0x0
Copy link
Contributor

Rename iter2 to iteri by convention, e.g. make and makei

fn Iter::iteri[T](self : Iter[T]) -> Iter2[Int, T]

We need to think about which one is more preferable because we cannot have both under the same name.

fn Iter::iter2[T](self : Iter[T]) -> Iter2[Int, T]
fn Iter::iter2[A, B](self : Iter[(A, B)]) -> Iter2[A, B]

@Yu-zh
Copy link
Collaborator

Yu-zh commented Mar 11, 2025

Then the problem would be when you write for a, b in items, the compiler does not know whether it should use iteri or iter2

@illusory0x0
Copy link
Contributor

Then the problem would be when you write for a, b in items, the compiler does not know whether it should use iteri or iter2

The behavior of T::iter2 and T::op_get should be consistent.

add iteri as helper function like this

type AssocList[K,V] @immut/list.T[(K,V)]

fn AssocList::iteri[K,V](self : AssocList[K,V]) -> Iter2[Int,V] {
    Iter2::new(fn(yield_) {
    loop 0,self._ {
      _,Nil => IterContinue
      i,Cons(head, tail) => {
        if yield_(i,head.1) == IterEnd {
          break IterEnd
        }
        continue i+1,tail
      }
    }
  })
}

test {
  let assoc : AssocList[String,String] = Nil
  for i,v in assoc.iteri() {

  } 
}

@peter-jerry-ye
Copy link
Collaborator

The behavior of T::iter2 and T::op_get should be consistent.

What should be the behavior for:

let array = [(1, "a"), (1, "b"), (1, "c")] 
for k, v in array.iter() {
  // should k be 0, 1, 2 or 1, 1, 1?
}

Do you think that for Iter[(K, V)], for each K, there can be at most one corresponding value of V?

@illusory0x0
Copy link
Contributor

illusory0x0 commented Apr 21, 2025

Do you think that for Iter[(K, V)], for each K, there can be at most one corresponding value of V?

K in [0, 1, 2], V in [(1, "a"), (1, "b"), (1, "c")] , we can treat Array[T] like Map[Int,T].

If we just iter two element, Iter[(A,B)] is enough, maybe IterWithIndex wound be better name

@peter-jerry-ye
Copy link
Collaborator

K in [0, 1, 2], V in [(1, "a"), (1, "b"), (1, "c")] , we can treat Array[T] like Map[Int,T].

Then by convention, when implementing iter2 for data structures in Core, the first element should serve as a key element.

Thus for Iter[(K, V)] -> Iter2[K, V], we may need to find another name such as iter_as_pair

@illusory0x0
Copy link
Contributor

Thus for Iter[(K, V)] -> Iter2[K, V], we may need to find another name such as iter_as_pair

typo ´s/iter_as_pair/iter_as_pairs´

we also can add more documents for ´iter_as_pairs´, iterate sequence like associated list

@peter-jerry-ye peter-jerry-ye linked a pull request Apr 27, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants