Skip to content
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

zip for arrays, iters, etc #994

Open
NKID00 opened this issue Sep 13, 2024 · 3 comments
Open

zip for arrays, iters, etc #994

NKID00 opened this issue Sep 13, 2024 · 3 comments
Assignees

Comments

@NKID00
Copy link

NKID00 commented Sep 13, 2024

Is seems zip is only implemented for @immut/list.T but not Array or Iter. There seems to be no way to iterate over multiple iterators, and zipping two iterators fits well.

I am willing to contribute this, please assign me if I may give it a try.

@Guest0x0
Copy link
Contributor

MoonBit's Iter and Iter2 are internal iterators, where the user of iterators pass a callback to the provider of data. This is in contrast to external iterators, where the iterator produce one element every time the user requests, and save internal state about the current progress of iteration.

In internal iterators, the provider of data (e.g. Array::iter) owns the control flow, while for external iterators, the user of the iterator (e.g. for .. in) owns the control flow. This difference is subtle, because only the one who owns the control flow can stop the iterator in the middle. As a consequence, it is impossible to implement zip for internal iterators unless you have control effect constructs such as call/cc. So sadly, the absence of Iter::zip is a theoretic limitation of the Iter type.

At this point, you may wonder why MoonBit chooses the less flexible internal style to implement iterators. This is because internal iterators are simpler to write and optimize, and is adequate for the most common scenario.

Of course there are many cases where it is handly or even necessary to zip two iterators, and a iterator type that support zipping (i.e. external iterator) would be a good addition to MoonBit's current Iter and Iter2 types. If you are interested in this task, you may consider writing a package for external iterators on https://mooncakes.io , which is also a highly encouraged and appreciated way to contribute to the MoonBit community.

@Guest0x0
Copy link
Contributor

There is no problem with `Array::iter`` though, it would be great if you could implement it

@hackwaly
Copy link
Contributor

hackwaly commented Oct 12, 2024

How about

fn Iter::zipWith[T1, T2](self: Iter[T1], seq: Unit -> Option[T2]) -> Iter[T1, T2]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants