Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SergiuB/3316b92ac6b440c1b149 to your computer and use it in GitHub Desktop.
Save SergiuB/3316b92ac6b440c1b149 to your computer and use it in GitHub Desktop.
Backtracking engine using ES6 generators
function backtracking(opts) {
let {isSolution, notValid, nextStates, initialState} = opts
function* solutionsFromState(state) {
if (notValid(state))
return
if (isSolution(state)) {
yield state
} else {
for (let next of nextStates(state)) {
yield* solutionsFromState(next)
}
}
}
return solutionsFromState.bind(null, initialState)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/6.6.1/polyfill.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.3.4/mocha.min.css" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment