I have a list of variable size that I'd like to get all possible combinations for. Given a List with {"A","B","C"} I'm looking for the following output (in any order):
A
AB
ABC
AC
B
BC
C
just like this js answer
I can get all permutations easily for predefined number, as shown below:
rule "Permutation of 3 List" extends "My List"
when
$obj1: MyClass() from $myList
$obj2: MyClass() from $myList
$obj3: MyClass() from $myList
then
List<MyClass> list = new ArrayList<MyClass>();
list.add($obj1);
list.add($obj2);
list.add($obj3);
insert(list);
end
I'm not sure how I can get permutations for a variable number, and it may not help as I need the combinations, but if I could get all permutations for n elements of size k, I could run all permutations for n elements size 1 up to size n, then take the distinct values from those permutations. I would also need this to be relatively efficient, however, and not have a list with 20 elements crash the server.
edit: fixed js answer link
edit: removed added answer and posted as answer