Free cookie consent management tool by TermsFeed Policy Generator

Changeset 11370


Ignore:
Timestamp:
09/16/14 15:53:25 (10 years ago)
Author:
mkommend
Message:

#2220: Merged r11309 & r11347 into stable.

Location:
stable
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Common/3.3/EnumerableExtensions.cs

    r11170 r11370  
    8787      return result;
    8888    }
     89
     90    /// <summary>
     91    /// Compute the n-ary cartesian product of arbitrarily many sequences: http://blogs.msdn.com/b/ericlippert/archive/2010/06/28/computing-a-cartesian-product-with-linq.aspx
     92    /// </summary>
     93    /// <typeparam name="T">The type of the elements inside each sequence</typeparam>
     94    /// <param name="sequences">The collection of sequences</param>
     95    /// <returns>An enumerable sequence of all the possible combinations of elements</returns>
     96    public static IEnumerable<IEnumerable<T>> CartesianProduct<T>(this IEnumerable<IEnumerable<T>> sequences) {
     97      IEnumerable<IEnumerable<T>> result = new[] { Enumerable.Empty<T>() };
     98      return sequences.Where(s => s.Any()).Aggregate(result, (current, s) => (from seq in current from item in s select seq.Concat(new[] { item })));
     99    }
    89100  }
    90101}
Note: See TracChangeset for help on using the changeset viewer.