Skip to content

Instantly share code, notes, and snippets.

@Cifro
Last active September 30, 2015 01:18
Show Gist options
  • Save Cifro/1699265 to your computer and use it in GitHub Desktop.
Save Cifro/1699265 to your computer and use it in GitHub Desktop.
Nice feature of Lisp is also in JavaScript
; 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
@fprochazka
Copy link

And also in PHP :)

function generateCounter() {
    $memory = 1;
    return function () use (&$memory) {
        return $memory++;
    }
}

$gen = generateCounter();
echo $gen(); // 1
echo $gen(); // 2
echo $gen(); // 3

;)

@Cifro
Copy link
Author

Cifro commented Sep 17, 2012

Nice ^_^ 👍

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