Friday Quiz

I just tried this example from the Squirrel manual as part of an article I’m writing on generator functions. It didn’t work: the output will not be as the book claims. I eventually figured out why. Can you?

`function geny(n)
for(local i=0;i<n;i+=1)
yield i;
return null;

local gtor=geny(10);
local x;
while(x=resume gtor) print(x+"

the output of this program will be


Note: this is just for fun - there are no prizes for posting the correct answer. Electric Imp employees, please don’t rush to provide the answer.

@coverdriven, you are correct, but were beaten to the punch by @mjkuwp94, who got it about half an hour after I posted the original quiz, but didn’t want to say so here in order to give everyone else a chance.

Hmm… there are a couple of things in there that I am not familiar with… but I’ve been loving your articles… eager to read this one.

Doesn’t x = resume gtor evaluate to 0 after the first yield? An expression that evaluates to 0 is interpreted as false according to the Squirrel documentation. Perhaps this would be better:
while ((x=resume gtor) != null) print(x+"\ ");
I’m looking forward to more documentation on generators, as the Reference Manual is pretty weak in this area. The example uses return null, but is the null superfluous?

I didn’t think to message you directly. Sorry, my bad. Next time.