A Pen by Sergiu Buciuc on CodePen.
Last active
March 5, 2016 16:28
-
-
Save SergiuB/3316b92ac6b440c1b149 to your computer and use it in GitHub Desktop.
Backtracking engine using ES6 generators
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 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) | |
} |
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
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/6.6.1/polyfill.min.js"></script> |
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
<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