Published

Stateful Loops and Swift Sequence Functions

Given the question:

“I’ve come across a situation a number of times where I write code that has to try something one or more times using a repeat…while loop and the condition relies upon variables that should be declared within the scope of the loop.”

“…creates a stateful repeat-while loop, which is more or less what I believe the writer was aiming for. It uses a Boolean test, hides the use of sequence(state:next:), and allows a trailing closure for the body of the loop.”