Created
March 6, 2019 00:01
-
-
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?
This file contains 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
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