Last active
November 18, 2016 23:16
-
-
Save rajaraodv/315a339d0f1382470a22dc75e0a26ee6 to your computer and use it in GitHub Desktop.
The below gist shows how to take care of global variables and also make funcs chainable
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
| //The below gist shows how to take care of global variables and also make funcs chainable | |
| //Global indexURLs map for different languages | |
| let indexURLs = { | |
| 'en': 'http://mysite.com/en', //English | |
| 'sp': 'http://mysite.com/sp', //Spanish | |
| 'jp': 'http://mysite.com/jp' //Japanese | |
| } | |
| //Imperative | |
| const getUrl = (language) => allUrls[language]; //Simple, but error prone and impure (accesses global variable) | |
| //Functional Programming | |
| //Before currying: | |
| const getUrl = (allUrls, language) => { | |
| return Maybe(allUrls[language]); | |
| } | |
| //After currying: | |
| const getUrl = R.curry(function(allUrls, language) {//curry to convert this to a single arg func | |
| return Maybe(allUrls[language]); | |
| }); | |
| const maybeGetUrl = getUrl(indexURLs) //Store global value in the 'curried' function. | |
| //From this point, maybeGetUrl needs only one argument(language). So we can now chain this like: | |
| maybe(user).chain(maybeGetUrl).bla.bla | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment