Skip to content

Instantly share code, notes, and snippets.

@CrossEye
Last active October 23, 2018 04:26
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save CrossEye/f7c2f77f7db7a94af209 to your computer and use it in GitHub Desktop.
Save CrossEye/f7c2f77f7db7a94af209 to your computer and use it in GitHub Desktop.
Find permutations of a list of (distinct) values. Uses Ramda
const R = require('ramda');
const permutations = (tokens, subperms = [[]]) =>
R.isEmpty(tokens) ?
subperms :
R.addIndex(R.chain)((token, idx) => permutations(
R.remove(idx, 1, tokens),
R.map(R.append(token), subperms)
), tokens);
R.map(R.join(''), permutations(['A', 'B', 'C']));
//=> ["ABC", "ACB", "BAC", "BCA", "CAB", "CBA"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment