core icon indicating copy to clipboard operation
core copied to clipboard

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

Open hackwaly opened this issue 1 year ago • 9 comments

hackwaly avatar Dec 11 '24 02:12 hackwaly

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)]

illusory0x0 avatar Jan 08 '25 18:01 illusory0x0

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 avatar Mar 11 '25 04:03 Yu-zh

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]

illusory0x0 avatar Mar 11 '25 05:03 illusory0x0

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

Yu-zh avatar Mar 11 '25 05:03 Yu-zh

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

  } 
}

illusory0x0 avatar Mar 11 '25 05:03 illusory0x0

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?

peter-jerry-ye avatar Apr 21 '25 02:04 peter-jerry-ye

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

illusory0x0 avatar Apr 21 '25 03:04 illusory0x0

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

peter-jerry-ye avatar Apr 21 '25 05:04 peter-jerry-ye

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

illusory0x0 avatar Apr 21 '25 09:04 illusory0x0