core icon indicating copy to clipboard operation
core copied to clipboard

Proposal: peekable iterator and pattern matching on iterators

Open glyh opened this issue 1 year ago • 2 comments

For now there's no way to match on iteraters the same way we do with Lists, and I reckon it would be useful as it can drastically simplify the way people work with iterators.

So ideally it would be something like this:

match [1,2,3].iter() {
  1 ~> 2 ~> rest {
    // rest is a iterator here. 
  }
  End {
  }
}

glyh avatar Sep 13 '24 03:09 glyh

I am aware that this is a performance penalty as bascially we need to store what the underlying algorithm produces to make an iterator peekable. But still, I would argue this provides a unified iterface across data structures.

We can have some other data structures and retain the original iterator, so if user want a fast implementation, they choose Iter, and if they want something more fancy, they can choose PatIter

glyh avatar Sep 13 '24 03:09 glyh

Indeed an external iterator implementation would be very handy. I suggest starting with a prototype package on https://mooncakes.io, and if it turns out useful enough, we may consider moving it to moonbitlang/xor moonbitlang/core, and add pattern match support to it.

related: #994

Guest0x0 avatar Sep 14 '24 09:09 Guest0x0

Closing as this requires an implementation of external iterator

peter-jerry-ye avatar Apr 08 '25 03:04 peter-jerry-ye