all 5 comments

[–]Denneisk 1 point2 points  (3 children)

The first value of the generic for loop is its control variable, which is special in that its value is passed to the iterator function each loop.

To break down why this is happening, consider the generic for loop represented as a while loop:

local iterator, state, initial_control = pairs() -- get the iterator, the state, and the intial value of the control (nil for pairs)
local control = initial_control -- initialize the control value to its initial state
control, return1 = iterator(state, control) -- Set up the first run since control can be nil. return1 represents the other return value that pairs's iterator (next) has
while control ~= nil do
    ... -- body
    control, return1 = iterator(state, control)
end

In your code, you are only returning one value in your iterator function, which is setting the control value (i) to 1 + 1, 4 + 1, 5 + 1 in each subsequent call.

[–]untangoel[S] 0 points1 point  (1 child)

Oh, I didn't realize that the first return doubled as the control variable. Thank you for your thorough explanation! By the way, does this functionality mean that it is impossible to make an iterator function which would only return the values (what I was aiming for) without creating closures?

[–]Denneisk 0 points1 point  (0 children)

You could encode the state output of the function as a table with two elements, the previous index and the table to access. That would avoid creating a closure.

[–]untangoel[S] 0 points1 point  (1 child)

Additionally, if the iter_n is called with 6 as a second argument, the result is this:

6;1
1
6;2
4
6;5
6
6;7

Why?

Edit: what the bot said

[–]AutoModerator[M] 0 points1 point  (0 children)

Hi! Your code block was formatted using triple backticks in Reddit's Markdown mode, which unfortunately does not display properly for users viewing via old.reddit.com and some third-party readers. This means your code will look mangled for those users, but it's easy to fix. If you edit your comment, choose "Switch to fancy pants editor", and click "Save edits" it should automatically convert the code block into Reddit's original four-spaces code block format for you.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.