Hi Ryan! My guess is my response will start to answer, but won't answer completely. If it doesn't no problem with any follow-ups!
input = queue.shift()(input);
This is like
var function_from_q = queue.shift();
input = function_from_q(input);
How does this not loop indefinitely?
The above may answer, but please explain why you thought it'd loop sideways- -ly. There's no inherent or hidden loop in the above statements, and each time
shift is called it'll remove one element from the array, guaranteeing if your loop is right that the containing loop'll stop.
How does it now which input to use when it is being reset?
May not quite be following, but I think the above breakdown also answers.
The stuff on the right-hand side of the
= is evaluated first. So, the value of
input prior to
input = queue.shift()(input); is used as the argument to the function returned from
shift. That function call's return value is then assigned to
input on the left-hand side of the
=, and that is the new value of
input = queue.shift()(input); is executed.
This is a bit confusing, and although my breakdown is more readable, I do find in the real world statements like
input = queue.shift()(input); or more complex are used, so understanding it is important.