Generate All Partitions of a List
The new functions FoldPair and FoldPairList allow mapping simultaneously an output function and a state-update function.
Take a length-5 list to be partitioned.
In[1]:=
list = {a, b, c, d, e};
Use FoldPairList together with TakeDrop to generate a partition of the following lengths.
In[2]:=
lengths = {2, 2, 1};
FoldPairList[TakeDrop, list, lengths]
Out[2]=
To construct all partitions, find all possible decompositions of the number 5.
In[3]:=
lengthsAll = Flatten[Permutations /@ IntegerPartitions[5], 1]
Out[3]=
In[4]:=
FoldPairList[TakeDrop, list, #] & /@ lengthsAll // Column
Out[4]=