Skip to content

Instantly share code, notes, and snippets.

@WillsonSmith
Created March 6, 2019 00:01
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 WillsonSmith/3ac47b4699eb389d2895aa24cd7bf695 to your computer and use it in GitHub Desktop.
Save WillsonSmith/3ac47b4699eb389d2895aa24cd7bf695 to your computer and use it in GitHub Desktop.
i honestly don't know why i did this. action functions with strings?
function sequence(...funcs) {
return function (value) {
return funcs.reverse().reduce(function(previous, current) {
return previous.call ? previous.call(this, current(value)) : previous;
});
};
};
function instructionSet(instructionSet) {
return (valueToWorkWith) => {
return (program) => {
const instructions = program[0].split('\n').filter((instruction) => instruction.length);
return sequence(
...instructions.reduce((functionsToRun, currentInstruction) => {
return [
...functionsToRun,
instructionSet.find((i) => i.match.includes(currentInstruction)).response,
]
}, [])
)(valueToWorkWith);
}
}
}
const mathInstructions = [
{
match: 'ADD',
response: (value) => {
return Number(value) + 1;
},
},
{
match: 'SUBTRACT',
response: (value) => {
return Number(value) - 1;
},
}
]
const math = instructionSet(mathInstructions)(0);
const result = math`ADD
ADD
SUBTRACT
ADD
ADD
`
console.log(result);
// 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment