Skip to content

Instantly share code, notes, and snippets.

@balintsera
Created September 11, 2017 17:54
Show Gist options
  • Save balintsera/7e5a83fa1890b68ecbf058e6ae9e9a20 to your computer and use it in GitHub Desktop.
Save balintsera/7e5a83fa1890b68ecbf058e6ae9e9a20 to your computer and use it in GitHub Desktop.
higher order function created by balintsera - https://repl.it/Kwbw/1
var snakify = function(text) {
return text.replace(/millenials/ig, "Snake People");
};
console.log(snakify("The Millenials are always up to something."));
var hippify = function(text) {
return text.replace(/baby boomers/ig, "Aging Hippies");
};
console.log(hippify("The Baby Boomers just look the other way."));
// Higher order: function returns function
var attitude = function(original, replacement) {
return function(source) {
return source.replace(original, replacement);
};
};
var snakify = attitude(/millenials/ig, "Snake People");
var hippify = attitude(/baby boomers/ig, "Aging Hippies");
console.log(snakify("The Millenials are always up to something."));
console.log(hippify("The Baby Boomers just look the other way."));
// original: https://www.sitepoint.com/higher-order-functions-javascript/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment