[Feature Request] impl Iter2 for Iter[(A, B)]
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)]
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]
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]
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
Then the problem would be when you write
for a, b in items, the compiler does not know whether it should useiterioriter2
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() {
}
}
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?
Do you think that for
Iter[(K, V)], for eachK, there can be at most one corresponding value ofV?
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
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
Thus for
Iter[(K, V)] -> Iter2[K, V], we may need to find another name such asiter_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