Skip to content

Instantly share code, notes, and snippets.

@cwg999
Last active January 15, 2018 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cwg999/52e71d7379c05b6019616a2f1ad495ac to your computer and use it in GitHub Desktop.
Save cwg999/52e71d7379c05b6019616a2f1ad495ac to your computer and use it in GitHub Desktop.
Declarative vs Imperative
// Not sure this is accruate, but I was wondering about different styles.
(function(){
let sum = e=>e+1;
let sub = e=>e-1;
let mul = e=>e*e;
let div = e=>1/e;
let dataSet = [1,2,3];
let dataSet2 = [4,5,6];
// Imperative
// console.log(dataSet.map(sum)); // Do thing
// console.log(dataSet.map(sub)); // Do thing
// console.log(dataSet.map(mul)); // Do thing
// console.log(dataSet.map(div)); // Do thing
// Functional?
let actionProcessor = data => e=>console.log(data.map(e)); // Declare thing to do and what to do it on.
let applyFun = dataSet => [sum,sub,mul,div].forEach(actionProcessor(dataSet));
// [dataSet,dataSet2].forEach(applyFun); // Do thing
//Declarative
let actionObj = {
actions:[sum,sub,mul,div], // Default, Declare thing to do when set.
set runData(value){
this.actions.forEach(action=>console.log(value.map(action)))
}
};
actionObj.actions=[sum,sub,mul,div];
actionObj.runData=dataSet; // Set data.
actionObj.actions=[sum,sub];
actionObj.runData=dataSet2; // Set data.
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment