Created
December 23, 2013 04:24
-
-
Save zealoushacker/8091614 to your computer and use it in GitHub Desktop.
A closure containing a pure function in js
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
| /* | |
| Functions containing no free/unbound variables are called pure functions. | |
| Functions containing one or more free variables are called closures. | |
| Adapted from the quick exercise in https://leanpub.com/javascript-allonge/read#closures | |
| If pure functions can contain closures, can a closure contain a pure function? | |
| Attempt to compose a closure that contains a pure function. | |
| Many thanks to @raganwald! | |
| */ | |
| ( | |
| function (x) { | |
| return function (y) { | |
| (function (n) { | |
| return n; | |
| }); | |
| return x; | |
| }; | |
| } | |
| )("foo")("bar"); | |
| //=> 'foo' |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@raganwald What do you think about this as an example of pure functions contained in closures? :)