EnumerableEx.Multiple.cs 4.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading;
  8. namespace System.Linq
  9. {
  10. public static partial class EnumerableEx
  11. {
  12. /// <summary>
  13. /// Concatenates the input sequences.
  14. /// </summary>
  15. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  16. /// <param name="sources">Source sequences.</param>
  17. /// <returns>Sequence with the elements of the source sequences concatenated.</returns>
  18. public static IEnumerable<TSource> Concat<TSource>(this IEnumerable<IEnumerable<TSource>> sources)
  19. {
  20. if (sources == null)
  21. throw new ArgumentNullException(nameof(sources));
  22. return sources.Concat_();
  23. }
  24. /// <summary>
  25. /// Concatenates the input sequences.
  26. /// </summary>
  27. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  28. /// <param name="sources">Source sequences.</param>
  29. /// <returns>Sequence with the elements of the source sequences concatenated.</returns>
  30. public static IEnumerable<TSource> Concat<TSource>(params IEnumerable<TSource>[] sources)
  31. {
  32. if (sources == null)
  33. throw new ArgumentNullException(nameof(sources));
  34. return sources.Concat_();
  35. }
  36. private static IEnumerable<TSource> Concat_<TSource>(this IEnumerable<IEnumerable<TSource>> sources)
  37. {
  38. foreach (var source in sources)
  39. foreach (var item in source)
  40. yield return item;
  41. }
  42. /// <summary>
  43. /// Projects each element of a sequence to an given sequence and flattens the resulting sequences into one sequence.
  44. /// </summary>
  45. /// <typeparam name="TSource">First source sequence element type.</typeparam>
  46. /// <typeparam name="TOther">Second source sequence element type.</typeparam>
  47. /// <param name="source">A sequence of values to project.</param>
  48. /// <param name="other">Inner sequence each source sequenec element is projected onto.</param>
  49. /// <returns>Sequence flattening the sequences that result from projecting elements in the source sequence.</returns>
  50. public static IEnumerable<TOther> SelectMany<TSource, TOther>(this IEnumerable<TSource> source, IEnumerable<TOther> other)
  51. {
  52. if (source == null)
  53. throw new ArgumentNullException(nameof(source));
  54. if (other == null)
  55. throw new ArgumentNullException(nameof(other));
  56. return source.SelectMany(_ => other);
  57. }
  58. #if NO_ZIP
  59. /// <summary>
  60. /// Merges two sequences by applying the specified selector function on index-based corresponding element pairs from both sequences.
  61. /// </summary>
  62. /// <typeparam name="TFirst">The type of the elements of the first input sequence.</typeparam>
  63. /// <typeparam name="TSecond">The type of the elements of the second input sequence.</typeparam>
  64. /// <typeparam name="TResult">The type of the elements of the result sequence.</typeparam>
  65. /// <param name="first">The first sequence to merge.</param>
  66. /// <param name="second">The second sequence to merge.</param>
  67. /// <param name="resultSelector">Function to apply to each pair of elements from both sequences.</param>
  68. /// <returns>Sequence consisting of the result of pairwise application of the selector function over pairs of elements from the source sequences.</returns>
  69. public static IEnumerable<TResult> Zip<TFirst, TSecond, TResult>(this IEnumerable<TFirst> first, IEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> resultSelector)
  70. {
  71. if (first == null)
  72. throw new ArgumentNullException("first");
  73. if (second == null)
  74. throw new ArgumentNullException("second");
  75. if (resultSelector == null)
  76. throw new ArgumentNullException("resultSelector");
  77. return Zip_(first, second, resultSelector);
  78. }
  79. private static IEnumerable<TResult> Zip_<TFirst, TSecond, TResult>(this IEnumerable<TFirst> first, IEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> resultSelector)
  80. {
  81. using (var e1 = first.GetEnumerator())
  82. using (var e2 = second.GetEnumerator())
  83. while (e1.MoveNext() && e2.MoveNext())
  84. yield return resultSelector(e1.Current, e2.Current);
  85. }
  86. #endif
  87. }
  88. }