Code/Util/Permutations.cs
namespace Nodebox.Util;

public static class Permutations {
    public static IEnumerable<T[]> Calculate<T>(List<T>[] sources) {
        if (sources == null) {
            throw new ArgumentNullException(nameof(sources));
        }

        var current = new T[sources.Length];

        return Recurse(0);

        IEnumerable<T[]> Recurse(int index) {
            if (index == sources.Length) {
                var copy = current.ToArray();
                yield return copy;
                yield break;
            }

            foreach (var item in sources[index]) {
                current[index] = item;

                foreach (var permutation in Recurse(index + 1)) {
                    yield return permutation;
                }
            }
        }
    }
}