Last active
January 1, 2016 08:41
-
-
Save tasugim/6120ef91f73b7e0b999d to your computer and use it in GitHub Desktop.
いろんな言語でカウンタを書いてみる
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| counter = function() { | |
| var count = 0; | |
| return function() { | |
| return count += 1; | |
| } | |
| } | |
| c1 = counter() | |
| c1() //=> 1 | |
| c1() //=> 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def counter(): | |
| count = [0] | |
| def f(): | |
| count[0] += 1 | |
| return count[0] | |
| return f | |
| c1 = counter() | |
| c1() #=> 1 | |
| c1() #=> 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def counter() = { | |
| var count = 0 | |
| val f = () => { count += 1; count } | |
| f | |
| } | |
| c1 = counter() | |
| c1() //=> 1 | |
| c1() //=> 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func counter() -> () -> Int { | |
| var count = 0 | |
| func f() -> Int { | |
| count += 1 | |
| return n | |
| } | |
| return f | |
| } | |
| let c1 = counter() | |
| c1() //=> 1 | |
| c1() //=> 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def counter | |
| count = 0 | |
| lambda { count += 1; return count } | |
| end | |
| c1 = counter | |
| c1.call #=> 1 | |
| c1.call #=> 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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