Created
June 9, 2016 17:43
-
-
Save thurt/2d1142604bae80c3db030902be88f292 to your computer and use it in GitHub Desktop.
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
const deepSwitch = (input) => (cases) => { | |
const match_case = cases.find(c => c[0].every((el, i) => el === input[i])) | |
return (match_case !== undefined) | |
? match_case[1] | |
: false | |
} | |
// output values are determined solely by the current state | |
const MooreMachine = (transitionFn) => (outputFn) => (start_state) => (list) => { | |
var outlist = [] | |
var final_s = list.reduce((s, x, i) => { | |
s = transitionFn(s, x) | |
outlist[i] = outputFn(s) | |
return s | |
}, start_state) | |
return [final_s, outlist] | |
} | |
// output values are determined by the current state and the input value | |
const MealyMachine = (transitionFn) => (outputFn) => (start_state) => (list) => { | |
var outlist = [] | |
var final_s = list.reduce((s, x, i) => { | |
s = transitionFn(s, x) | |
outlist[i] = outputFn(s, x) | |
return s | |
}, start_state) | |
return [final_s, outlist] | |
} | |
var res = MooreMachine | |
((state, input) => deepSwitch([state, input])([ | |
[[0, 0], 2], | |
[[0, 1], 1], | |
[[1, 0], 0], | |
[[1, 1], 2], | |
[[2, 0], 1], | |
[[2, 1], 0]]))((new_state) => new_state)(0)([1,1,0,1]) | |
console.log(res) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment