Skip to content

Instantly share code, notes, and snippets.

@adamretter
Created July 27, 2022 12:16
Show Gist options
  • Save adamretter/14b4c3850f1f1f17a290d5def18047e9 to your computer and use it in GitHub Desktop.
Save adamretter/14b4c3850f1f1f17a290d5def18047e9 to your computer and use it in GitHub Desktop.
permutations.xqm
declare function local:permutations($list as array(*)) as array(array(*)) {
if (array:size($list) eq 0)
then
array {}
else if (array:size($list) eq 1)
then
array { $list }
else
array:fold-left($list, array {}, function($a as array(array(*)), $x) {
array:join(($a, array {
array:for-each(local:permutations(array:filter($list, function($y){ $x ne $y })), function($p as array(*)) {
array:insert-before($p, 1, $x)
})?*
}))
})
};
local:permutations(["a","b","c"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment