Skip to content

Instantly share code, notes, and snippets.

@todashuta
Last active August 29, 2015 14:01
Show Gist options
  • Select an option

  • Save todashuta/5bab5aa08a67b41dadcd to your computer and use it in GitHub Desktop.

Select an option

Save todashuta/5bab5aa08a67b41dadcd to your computer and use it in GitHub Desktop.
luaのクロージャを使ったカウンタ
function createCounter()
local n = 0
return function()
n = n + 1
return n
end
end
counter = createCounter()
print(counter())
print(counter())
print(counter())
print(counter())
print(counter())
print(counter())
print(counter())
print(counter())
print(counter())
print(counter())
@todashuta
Copy link
Copy Markdown
Author

出力

1
2
3
4
5
6
7
8
9
10

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