Last active
September 30, 2015 01:18
-
-
Save Cifro/1699265 to your computer and use it in GitHub Desktop.
Nice feature of Lisp is also in JavaScript
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
; Lisp | |
(defun generate-counter () | |
(let ((memory 0)) | |
(lambda () (incf memory)))) | |
CG-USER(1): (setq fn (generate-counter)) | |
CG-USER(2): (funcall fn) | |
1 | |
CG-USER(3): (funcall fn) | |
2 | |
CG-USER(4): (funcall fn) | |
3 | |
// JavaScript | |
function generateCounter(){ | |
var memory = 1; | |
return function(){ | |
return memory++; | |
} | |
} | |
var fn = generateCounter(); | |
> fn() | |
1 | |
> fn() | |
2 | |
> fn() | |
3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And also in PHP :)
;)