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

Use of counter variable #19

Open
svarnypetr opened this issue Nov 24, 2020 · 2 comments
Open

Use of counter variable #19

svarnypetr opened this issue Nov 24, 2020 · 2 comments

Comments

@svarnypetr
Copy link

svarnypetr commented Nov 24, 2020

If we also need to have an iteration counter (starting from zero), we can achieve that by using for , in : syntax. This is very practical if you want to iterate through one iterable, and simultaneously access another iterable at the same offset.

However, as far as I know, this works only with strings, it does not work for iterators in general (for example countTo fails). However, it does work on containers. At least in my Nim 1.4.0.

So I suggest changing the text to:

If we also need to have an iteration counter (starting from zero), we can achieve that by using for , in : syntax. This is very practical if you want to iterate through one container, and simultaneously access another container at the same offset. Containers are explained just a little bit later.

@narimiran
Copy link
Owner

However, as far as I know, this works only with strings

It works for all containers (even custom ones) which have pairs iterator. https://nim-lang.github.io/Nim/manual.html#iterators-and-the-for-statement-implicit-itemsslashpairs-invocations

@svarnypetr
Copy link
Author

Ah, so a more specific type of containers than just general containers. However, as soon as I read iterators in the tutorial, I tried countTo and failed, got confused and then tried string and some containers. But I saw already other Nim tutorials and program in Python.

As the text follows directly examples of iterators, some caution for new users would, I think, be helpful.

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

2 participants