Skip to content

Instantly share code, notes, and snippets.

@tasugim
Last active January 1, 2016 08:41
Show Gist options
  • Select an option

  • Save tasugim/6120ef91f73b7e0b999d to your computer and use it in GitHub Desktop.

Select an option

Save tasugim/6120ef91f73b7e0b999d to your computer and use it in GitHub Desktop.
いろんな言語でカウンタを書いてみる
counter = function() {
var count = 0;
return function() {
return count += 1;
}
}
c1 = counter()
c1() //=> 1
c1() //=> 2
def counter():
count = [0]
def f():
count[0] += 1
return count[0]
return f
c1 = counter()
c1() #=> 1
c1() #=> 2
def counter() = {
var count = 0
val f = () => { count += 1; count }
f
}
c1 = counter()
c1() //=> 1
c1() //=> 2
func counter() -> () -> Int {
var count = 0
func f() -> Int {
count += 1
return n
}
return f
}
let c1 = counter()
c1() //=> 1
c1() //=> 2
def counter
count = 0
lambda { count += 1; return count }
end
c1 = counter
c1.call #=> 1
c1.call #=> 2
def counter
count = 0
Proc.new { count += 1 }
end
c1 = counter
c1.call #=> 1
c1.call #=> 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment