Debian Codegraph @

external/rx/Ix/NET/System.Interactive/EnumerableEx.Imperative.cs

raw
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
7 namespace System.Linq
8 {
9 public static partial class EnumerableEx
10 {
11 /// <summary>
12 /// Generates an enumerable sequence by repeating a source sequence as long as the given loop condition holds.
13 /// </summary>
14 /// <typeparam name="TResult">Result sequence element type.</typeparam>
15 /// <param name="condition">Loop condition.</param>
16 /// <param name="source">Sequence to repeat while the condition evaluates true.</param>
17 /// <returns>Sequence generated by repeating the given sequence while the condition evaluates to true.</returns>
18 public static IEnumerable<TResult> While<TResult>(Func<bool> condition, IEnumerable<TResult> source)
19 {
20 if (condition == null)
21 throw new ArgumentNullException("condition");
22 if (source == null)
23 throw new ArgumentNullException("source");
24
25 return WhileCore(condition, source).Concat();
26 }
27
28 static IEnumerable<IEnumerable<TSource>> WhileCore<TSource>(Func<bool> condition, IEnumerable<TSource> source)
29 {
30 while (condition())
31 yield return source;
32 }
33
34 /// <summary>
35 /// Returns an enumerable sequence based on the evaluation result of the given condition.
36 /// </summary>
37 /// <typeparam name="TResult">Result sequence element type.</typeparam>
38 /// <param name="condition">Condition to evaluate.</param>
39 /// <param name="thenSource">Sequence to return in case the condition evaluates true.</param>
40 /// <param name="elseSource">Sequence to return in case the condition evaluates false.</param>
41 /// <returns>Either of the two input sequences based on the result of evaluating the condition.</returns>
42 public static IEnumerable<TResult> If<TResult>(Func<bool> condition, IEnumerable<TResult> thenSource, IEnumerable<TResult> elseSource)
43 {
44 if (condition == null)
45 throw new ArgumentNullException("condition");
46 if (thenSource == null)
47 throw new ArgumentNullException("thenSource");
48 if (elseSource == null)
49 throw new ArgumentNullException("elseSource");
50
51 return EnumerableEx.Defer(() => condition() ? thenSource : elseSource);
52 }
53
54 /// <summary>
55 /// Returns an enumerable sequence if the evaluation result of the given condition is true, otherwise returns an empty sequence.
56 /// </summary>
57 /// <typeparam name="TResult">Result sequence element type.</typeparam>
58 /// <param name="condition">Condition to evaluate.</param>
59 /// <param name="thenSource">Sequence to return in case the condition evaluates true.</param>
60 /// <returns>The given input sequence if the condition evaluates true; otherwise, an empty sequence.</returns>
61 public static IEnumerable<TResult> If<TResult>(Func<bool> condition, IEnumerable<TResult> thenSource)
62 {
63 if (condition == null)
64 throw new ArgumentNullException("condition");
65 if (thenSource == null)
66 throw new ArgumentNullException("thenSource");
67
68 return EnumerableEx.Defer(() => condition() ? thenSource : Enumerable.Empty<TResult>());
69 }
70
71 /// <summary>
72 /// Generates an enumerable sequence by repeating a source sequence as long as the given loop postcondition holds.
73 /// </summary>
74 /// <typeparam name="TResult">Result sequence element type.</typeparam>
75 /// <param name="source">Source sequence to repeat while the condition evaluates true.</param>
76 /// <param name="condition">Loop condition.</param>
77 /// <returns>Sequence generated by repeating the given sequence until the condition evaluates to false.</returns>
78 public static IEnumerable<TResult> DoWhile<TResult>(this IEnumerable<TResult> source, Func<bool> condition)
79 {
80 if (source == null)
81 throw new ArgumentNullException("source");
82 if (condition == null)
83 throw new ArgumentNullException("condition");
84
85 return source.Concat(While(condition, source));
86 }
87
88 /// <summary>
89 /// Returns a sequence from a dictionary based on the result of evaluating a selector function.
90 /// </summary>
91 /// <typeparam name="TValue">Type of the selector value.</typeparam>
92 /// <typeparam name="TResult">Result sequence element type.</typeparam>
93 /// <param name="selector">Selector function used to pick a sequence from the given sources.</param>
94 /// <param name="sources">Dictionary mapping selector values onto resulting sequences.</param>
95 /// <returns>The source sequence corresponding with the evaluated selector value; otherwise, an empty sequence.</returns>
96 public static IEnumerable<TResult> Case<TValue, TResult>(Func<TValue> selector, IDictionary<TValue, IEnumerable<TResult>> sources)
97 {
98 if (selector == null)
99 throw new ArgumentNullException("selector");
100 if (sources == null)
101 throw new ArgumentNullException("sources");
102
103 return Case(selector, sources, Enumerable.Empty<TResult>());
104 }
105
106 /// <summary>
107 /// Returns a sequence from a dictionary based on the result of evaluating a selector function, also specifying a default sequence.
108 /// </summary>
109 /// <typeparam name="TValue">Type of the selector value.</typeparam>
110 /// <typeparam name="TResult">Result sequence element type.</typeparam>
111 /// <param name="selector">Selector function used to pick a sequence from the given sources.</param>
112 /// <param name="sources">Dictionary mapping selector values onto resulting sequences.</param>
113 /// <param name="defaultSource">Default sequence to return in case there's no corresponding source for the computed selector value.</param>
114 /// <returns>The source sequence corresponding with the evaluated selector value; otherwise, the default source.</returns>
115 public static IEnumerable<TResult> Case<TValue, TResult>(Func<TValue> selector, IDictionary<TValue, IEnumerable<TResult>> sources, IEnumerable<TResult> defaultSource)
116 {
117 if (selector == null)
118 throw new ArgumentNullException("selector");
119 if (sources == null)
120 throw new ArgumentNullException("sources");
121 if (defaultSource == null)
122 throw new ArgumentNullException("defaultSource");
123
124 return EnumerableEx.Defer(() =>
125 {
126 IEnumerable<TResult> result;
127 if (!sources.TryGetValue(selector(), out result))
128 result = defaultSource;
129 return result;
130 });
131 }
132
133 /// <summary>
134 /// Generates a sequence by enumerating a source sequence, mapping its elements on result sequences, and concatenating those sequences.
135 /// </summary>
136 /// <typeparam name="TSource">Source sequence element type.</typeparam>
137 /// <typeparam name="TResult">Result sequence element type.</typeparam>
138 /// <param name="source">Source sequence.</param>
139 /// <param name="resultSelector">Result selector to evaluate for each iteration over the source.</param>
140 /// <returns>Sequence concatenating the inner sequences that result from evaluating the result selector on elements from the source.</returns>
141 public static IEnumerable<TResult> For<TSource, TResult>(IEnumerable<TSource> source, Func<TSource, IEnumerable<TResult>> resultSelector)
142 {
143 if (source == null)
144 throw new ArgumentNullException("source");
145 if (resultSelector == null)
146 throw new ArgumentNullException("resultSelector");
147
148 return ForCore(source, resultSelector).Concat();
149 }
150
151 static IEnumerable<IEnumerable<TResult>> ForCore<TSource, TResult>(IEnumerable<TSource> source, Func<TSource, IEnumerable<TResult>> resultSelector)
152 {
153 return source.Select(resultSelector);
154 }
155 }
156 }