Created
July 27, 2022 12:16
-
-
Save adamretter/14b4c3850f1f1f17a290d5def18047e9 to your computer and use it in GitHub Desktop.
permutations.xqm
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
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