Skip to content

Instantly share code, notes, and snippets.

@thurt
Created June 9, 2016 17:43
Show Gist options
  • Save thurt/2d1142604bae80c3db030902be88f292 to your computer and use it in GitHub Desktop.
Save thurt/2d1142604bae80c3db030902be88f292 to your computer and use it in GitHub Desktop.
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