EnumerableEx.Multiple.cs 4.6 KB

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