wasSharp – Blame information for rev 55
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
11 | office | 1 | /////////////////////////////////////////////////////////////////////////// |
2 | // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
||
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
||
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
||
5 | /////////////////////////////////////////////////////////////////////////// |
||
6 | |||
7 | using System; |
||
8 | using System.Collections.Generic; |
||
9 | using System.Linq; |
||
10 | |||
53 | office | 11 | namespace wasSharp.Lambda |
11 | office | 12 | { |
13 | public static class Extensions |
||
14 | { |
||
15 | /////////////////////////////////////////////////////////////////////////// |
||
32 | office | 16 | // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // |
17 | /////////////////////////////////////////////////////////////////////////// |
||
18 | /// <summary> |
||
19 | /// A functional implementation of a switch clause. |
||
20 | /// </summary> |
||
21 | /// <typeparam name="T">the type of items in the query</typeparam> |
||
22 | /// <param name="query">the selector query</param> |
||
23 | /// <param name="default">the function to execute when no case matches</param> |
||
24 | /// <param name="case">a list of predicates representing the switch cases, |
||
25 | /// where each predicate has to return True or False indicating whether |
||
26 | /// fallthrough should occur. |
||
27 | /// </param> |
||
47 | office | 28 | /// <remarks>when used, the default action must be explicitly specified |
29 | /// due to language restrictions |
||
30 | /// </remarks> |
||
32 | office | 31 | public static void Switch<T>(this IEnumerable<T> query, |
32 | // default |
||
33 | Action<T> @default, |
||
34 | // case |
||
35 | // case |
||
36 | // ... |
||
37 | params Predicate<T>[] @case) |
||
38 | { |
||
39 | if (@case.Length % 2 != 0) |
||
40 | throw new ArgumentException("Pairs of predicates expected."); |
||
41 | |||
46 | office | 42 | var enumerable = query as IList<T> ?? query.ToList(); |
43 | using (var iter = enumerable.GetEnumerator()) |
||
32 | office | 44 | { |
46 | office | 45 | while (iter.MoveNext()) |
32 | office | 46 | { |
46 | office | 47 | var match = false; |
48 | for (var i = 0; i < @case.Length; i += 2) |
||
32 | office | 49 | { |
46 | office | 50 | if (!@case[i].Invoke(iter.Current)) |
51 | continue; |
||
52 | |||
53 | if (@case[i + 1].Invoke(iter.Current)) |
||
54 | return; |
||
55 | |||
56 | match = true; |
||
32 | office | 57 | } |
46 | office | 58 | |
59 | if (!match) |
||
60 | @default.Invoke(iter.Current); |
||
32 | office | 61 | } |
62 | } |
||
63 | } |
||
64 | |||
65 | /////////////////////////////////////////////////////////////////////////// |
||
66 | // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // |
||
67 | /////////////////////////////////////////////////////////////////////////// |
||
68 | /// <summary> |
||
49 | office | 69 | /// A functional implementation of a switch clause. |
70 | /// </summary> |
||
71 | /// <typeparam name="T">the type of the item to query</typeparam> |
||
72 | /// <param name="query">the selector query</param> |
||
73 | /// <param name="default">the function to execute when no case matches</param> |
||
74 | /// <param name="case">a list of predicates representing the switch cases, |
||
75 | /// where each predicate has to return True or False indicating whether |
||
76 | /// fallthrough should occur. |
||
77 | /// </param> |
||
78 | /// <remarks>when used, the default action must be explicitly specified |
||
79 | /// due to language restrictions |
||
80 | /// </remarks> |
||
81 | public static void Switch<T>(this T query, |
||
82 | // default |
||
83 | Action<T> @default, |
||
84 | // case |
||
85 | // case |
||
86 | // ... |
||
87 | params Predicate<T>[] @case) |
||
88 | { |
||
89 | if (@case.Length % 2 != 0) |
||
90 | throw new ArgumentException("Pairs of predicates expected."); |
||
91 | |||
92 | var match = false; |
||
93 | for (var i = 0; i < @case.Length; i += 2) |
||
94 | { |
||
95 | if (!@case[i].Invoke(query)) |
||
96 | continue; |
||
97 | |||
98 | if (@case[i + 1].Invoke(query)) |
||
99 | return; |
||
100 | |||
101 | match = true; |
||
102 | } |
||
103 | |||
104 | if (!match) |
||
105 | @default.Invoke(query); |
||
106 | |||
107 | } |
||
108 | |||
109 | /////////////////////////////////////////////////////////////////////////// |
||
110 | // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // |
||
111 | /////////////////////////////////////////////////////////////////////////// |
||
112 | /// <summary> |
||
32 | office | 113 | /// Invokes pf in case the predicate p resolves or qf in case the predicate q resolves, or ef otherwise. |
114 | /// </summary> |
||
115 | /// <typeparam name="T">the type of items in the query</typeparam> |
||
116 | /// <param name="query">the selector query</param> |
||
117 | /// <param name="p">the condition for invoking pf</param> |
||
118 | /// <param name="pf">the action to invoke in case p resolves</param> |
||
119 | /// <param name="q">the condition for invoking qf</param> |
||
120 | /// <param name="qf">the action to invoke in case q resolves</param> |
||
121 | /// <param name="ef">the action to invoke otherwise</param> |
||
122 | public static void ForAll<T>(this ParallelQuery<T> query, Predicate<T> p, Action<T> pf, Predicate<T> q, Action<T> qf, Action<T> ef) |
||
123 | { |
||
124 | query.ForAll(o => |
||
125 | { |
||
126 | if (p.Invoke(o)) |
||
127 | { |
||
128 | pf.Invoke(o); |
||
129 | } |
||
130 | else if (q.Invoke(o)) |
||
131 | { |
||
132 | qf.Invoke(o); |
||
133 | } |
||
134 | else |
||
135 | { |
||
136 | ef.Invoke(o); |
||
137 | } |
||
138 | }); |
||
139 | } |
||
140 | |||
141 | /////////////////////////////////////////////////////////////////////////// |
||
142 | // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // |
||
143 | /////////////////////////////////////////////////////////////////////////// |
||
144 | /// <summary> |
||
145 | /// Invokes pf in case the predicate p resolves or qf in case the predicate q resolves. |
||
146 | /// </summary> |
||
147 | /// <typeparam name="T">the type of items in the query</typeparam> |
||
148 | /// <param name="query">the selector query</param> |
||
149 | /// <param name="p">the condition for invoking pf</param> |
||
150 | /// <param name="pf">the action to invoke in case p resolves</param> |
||
151 | /// <param name="q">the condition for invoking qf</param> |
||
152 | /// <param name="qf">the action to invoke in case q resolves</param> |
||
153 | public static void ForAll<T>(this ParallelQuery<T> query, Predicate<T> p, Action<T> pf, Predicate<T> q, Action<T> qf) |
||
154 | { |
||
155 | query.ForAll(o => |
||
156 | { |
||
157 | if (p.Invoke(o)) |
||
158 | { |
||
159 | pf.Invoke(o); |
||
160 | return; |
||
161 | } |
||
162 | |||
163 | if (q.Invoke(o)) |
||
164 | { |
||
165 | qf.Invoke(o); |
||
166 | return; |
||
167 | } |
||
168 | }); |
||
169 | } |
||
170 | |||
171 | /////////////////////////////////////////////////////////////////////////// |
||
172 | // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // |
||
173 | /////////////////////////////////////////////////////////////////////////// |
||
174 | /// <summary> |
||
175 | /// Invokes pass if and only if predicate resovles or fail otherwise. |
||
176 | /// </summary> |
||
177 | /// <typeparam name="T">the type of items in the query</typeparam> |
||
178 | /// <param name="query">the selector query</param> |
||
179 | /// <param name="condition">the condition for invoking pf</param> |
||
180 | /// <param name="pass">the function to invoke in case the predicate resolves</param> |
||
181 | /// <param name="fail">the function to invoke otherwise</param> |
||
182 | public static void ForAll<T>(this ParallelQuery<T> query, Predicate<T> condition, Action<T> pass, Action<T> fail) |
||
183 | { |
||
184 | query.ForAll(o => |
||
185 | { |
||
186 | switch (condition.Invoke(o)) |
||
187 | { |
||
188 | case true: |
||
189 | pass.Invoke(o); |
||
190 | return; |
||
191 | |||
192 | default: |
||
193 | fail.Invoke(o); |
||
194 | return; |
||
195 | } |
||
196 | }); |
||
197 | } |
||
198 | |||
199 | /////////////////////////////////////////////////////////////////////////// |
||
200 | // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // |
||
201 | /////////////////////////////////////////////////////////////////////////// |
||
202 | /// <summary> |
||
203 | /// Invokes pass if and only if condition holds or fail otherwise. |
||
204 | /// </summary> |
||
205 | /// <typeparam name="T">the return type of the pass and fail functions</typeparam> |
||
206 | /// <param name="condition">the branch condition</param> |
||
207 | /// <param name="pass">function with no parameters and return type T in case condition passes</param> |
||
208 | /// <param name="fail">function with no parameters and return type T in case condition fails</param> |
||
209 | /// <returns>the result of pass in case condition holds or the result of fail otherwise</returns> |
||
55 | office | 210 | public static V IfElse<T, V>(this T arg, Func<T, bool> condition, Func<T, V> pass, Func<T, V> fail) |
211 | { |
||
212 | return condition.Invoke(arg) ? pass.Invoke(arg) : fail.Invoke(arg); |
||
213 | } |
||
214 | |||
215 | /////////////////////////////////////////////////////////////////////////// |
||
216 | // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // |
||
217 | /////////////////////////////////////////////////////////////////////////// |
||
218 | /// <summary> |
||
219 | /// Invokes pass if and only if condition holds or fail otherwise. |
||
220 | /// </summary> |
||
221 | /// <typeparam name="T">the return type of the pass and fail functions</typeparam> |
||
222 | /// <param name="condition">the branch condition</param> |
||
223 | /// <param name="pass">function with no parameters and return type T in case condition passes</param> |
||
224 | /// <param name="fail">function with no parameters and return type T in case condition fails</param> |
||
225 | /// <returns>the result of pass in case condition holds or the result of fail otherwise</returns> |
||
32 | office | 226 | public static T IfElse<T>(this bool condition, Func<T> pass, Func<T> fail) |
227 | { |
||
228 | return condition ? pass.Invoke() : fail.Invoke(); |
||
229 | } |
||
230 | |||
231 | /////////////////////////////////////////////////////////////////////////// |
||
232 | // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // |
||
233 | /////////////////////////////////////////////////////////////////////////// |
||
234 | /// <summary> |
||
235 | /// Invokes pass if and only if condition holds or fail otherwise. |
||
236 | /// </summary> |
||
237 | /// <typeparam name="U">the type of the argument to pass and fail</typeparam> |
||
238 | /// <typeparam name="V">the return type of pass and fail</typeparam> |
||
239 | /// <param name="condition">the branch condition</param> |
||
240 | /// <param name="pass">function that takes argument arg and returns type V in case condition holds</param> |
||
241 | /// <param name="fail">function that takes argument arg and returns type V in case condition fails</param> |
||
242 | /// <param name="arg">the argument passed to pass or fail functions</param> |
||
243 | /// <returns>the result of pass in case condition holds or the result of fail otherwise</returns> |
||
244 | public static V IfElse<U, V>(this bool condition, Func<U, V> pass, Func<U, V> fail, U arg = default(U)) |
||
245 | { |
||
246 | return condition ? pass.Invoke(arg) : fail.Invoke(arg); |
||
247 | } |
||
248 | |||
249 | /////////////////////////////////////////////////////////////////////////// |
||
250 | // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // |
||
251 | /////////////////////////////////////////////////////////////////////////// |
||
252 | /// <summary> |
||
253 | /// Invokes pass if and only if condition holds or fail otherwise. |
||
254 | /// </summary> |
||
255 | /// <typeparam name="T">the type of the argument to pass and fail</typeparam> |
||
256 | /// <param name="condition">the branch condition</param> |
||
257 | /// <param name="pass">function that takes argument arg and returns nothing in case condition holds</param> |
||
258 | /// <param name="fail">function that takes argument arg and returns nothing in case condition fails</param> |
||
259 | /// <param name="arg">the optional argument passed to pass or fail functions</param> |
||
260 | public static void IfElse<T>(this bool condition, Action<T> pass, Action<T> fail, T arg = default(T)) |
||
261 | { |
||
262 | switch (condition) |
||
263 | { |
||
264 | case true: |
||
265 | pass.Invoke(arg); |
||
266 | return; |
||
267 | |||
268 | default: |
||
269 | fail.Invoke(arg); |
||
270 | return; |
||
271 | } |
||
272 | } |
||
273 | |||
274 | /////////////////////////////////////////////////////////////////////////// |
||
275 | // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // |
||
276 | /////////////////////////////////////////////////////////////////////////// |
||
277 | /// <summary> |
||
278 | /// Invokes pass if and only if condition holds or fail otherwise. |
||
279 | /// </summary> |
||
280 | /// <typeparam name="U">the type of the first argument to the pass or fail functions</typeparam> |
||
281 | /// <typeparam name="V">the type of the second argument to the pass or fail functions</typeparam> |
||
282 | /// <param name="condition">the branch condition</param> |
||
283 | /// <param name="pass">function that takes argument arg and returns nothing in case condition holds</param> |
||
284 | /// <param name="fail">function that takes argument arg and returns nothing in case condition fails</param> |
||
285 | /// <param name="arga">first optional argument passed to pass or fail functions</param> |
||
286 | /// <param name="argb">second optional argument passed to pass or fail functions</param> |
||
287 | public static void IfElse<U, V>(this bool condition, Action<U, V> pass, Action<U, V> fail, U arga = default(U), V argb = default(V)) |
||
288 | { |
||
289 | switch (condition) |
||
290 | { |
||
291 | case true: |
||
292 | pass.Invoke(arga, argb); |
||
293 | return; |
||
294 | |||
295 | default: |
||
296 | fail.Invoke(arga, argb); |
||
297 | return; |
||
298 | } |
||
299 | } |
||
300 | |||
301 | /////////////////////////////////////////////////////////////////////////// |
||
11 | office | 302 | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
303 | /////////////////////////////////////////////////////////////////////////// |
||
304 | /// <summary> |
||
305 | /// Returns true of an enumerable contains more than one element. |
||
306 | /// </summary> |
||
307 | /// <typeparam name="T">the type of the enumeration</typeparam> |
||
308 | /// <param name="e">the enumeration</param> |
||
309 | /// <returns>true if enumeration contains more than one element</returns> |
||
310 | /// <remarks>O(2) worst case</remarks> |
||
311 | public static bool Some<T>(this IEnumerable<T> e) |
||
312 | { |
||
313 | var i = 0; |
||
314 | using (var iter = e.GetEnumerator()) |
||
315 | { |
||
316 | while (iter.MoveNext()) |
||
317 | { |
||
318 | if (++i > 1) |
||
319 | return true; |
||
320 | } |
||
321 | return false; |
||
322 | } |
||
323 | } |
||
324 | } |
||
27 | office | 325 | } |