Skip to content

Instantly share code, notes, and snippets.

@appgurueu
Last active January 29, 2023 13:21
Show Gist options
  • Select an option

  • Save appgurueu/4712d5bcb063a7c6f288f3078fd0f242 to your computer and use it in GitHub Desktop.

Select an option

Save appgurueu/4712d5bcb063a7c6f288f3078fd0f242 to your computer and use it in GitHub Desktop.
Infinite Loops

Infinite Loops

In Lua.

Lawful

Good

A classic.

while true do
	-- body
end

Neutral

A different take on it.

repeat
	-- body
until false

Evil

Remember tail call optimizations.

local function loop()
	-- body
	return loop()
end
loop()

You don't even need the local variable given the powerful debug library:

(function() --[[body]] return debug.getinfo(1).func() end)()

Alternatively (who needs spaces?):

while-1do
	-- body
end

Chaotic

Good

Back to the future!

::loop::
-- body
goto loop

Neutral

A short infinite for iterator.

for _ in (function return true end) do
	-- body
end

Thanks to ywang for some golfing.

Evil

Numeric for plus some double magic.

for _ = -1e308, 1e308, 1e-308 do
	-- body
end

Shorter variant by ywang:

for _ = math.huge, math.huge do
	-- body
end

Instead of using math.huge, you can also divide something nonzero by zero.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment