// ------------------------------------------------------------------------------------------------- // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. // ------------------------------------------------------------------------------------------------- using System.Collections.Generic; using System.Linq; using EnsureThat; namespace Microsoft.Health.Core.Extensions; public static class EnumerableExtensions { /// /// Creates the cartesian product. /// /// The type of the elements of the input sequences. /// The input sequence. /// An that contains the cartesian product of the input sequences. public static IEnumerable> CartesianProduct(this IEnumerable> sequences) { EnsureArg.IsNotNull(sequences, nameof(sequences)); IEnumerable> emptyProduct = new[] { Enumerable.Empty() }; return sequences.Aggregate( emptyProduct, (accumulator, sequence) => { return accumulator.SelectMany(a => sequence.Select(s => a.Concat(Enumerable.Repeat(s, 1)))); }); } /// /// Generates a sequence that contains one value. /// /// The element type. /// The element to return. /// An that contains one value. public static IEnumerable AsEnumerable(this TResult element) { yield return element; } }