wasSharp – Diff between revs 29 and 32

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 29 Rev 32
Line 11... Line 11...
11 namespace wasSharp.Linq 11 namespace wasSharp.Linq
12 { 12 {
13 public static class Extensions 13 public static class Extensions
14 { 14 {
15 /////////////////////////////////////////////////////////////////////////// 15 ///////////////////////////////////////////////////////////////////////////
-   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>
-   28 public static void Switch<T>(this IEnumerable<T> query,
-   29 // default
-   30 Action<T> @default,
-   31 // case
-   32 // case
-   33 // ...
-   34 params Predicate<T>[] @case)
-   35 {
-   36 if (@case.Length % 2 != 0)
-   37 throw new ArgumentException("Pairs of predicates expected.");
-   38  
-   39 try
-   40 {
-   41 using (var iter = query.GetEnumerator())
-   42 {
-   43 while (iter.MoveNext())
-   44 {
-   45 for (var i = 0; i < @case.Length; i += 2)
-   46 {
-   47 if (@case[i].Invoke(iter.Current) && @case[i + 1].Invoke(iter.Current))
-   48 throw new Exception();
-   49 }
-   50 }
-   51 }
-   52 @default.Invoke(query.Last());
-   53 }
-   54 catch
-   55 {
-   56 }
-   57 }
-   58  
-   59 ///////////////////////////////////////////////////////////////////////////
-   60 // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 //
-   61 ///////////////////////////////////////////////////////////////////////////
-   62 /// <summary>
-   63 /// Invokes pf in case the predicate p resolves or qf in case the predicate q resolves, or ef otherwise.
-   64 /// </summary>
-   65 /// <typeparam name="T">the type of items in the query</typeparam>
-   66 /// <param name="query">the selector query</param>
-   67 /// <param name="p">the condition for invoking pf</param>
-   68 /// <param name="pf">the action to invoke in case p resolves</param>
-   69 /// <param name="q">the condition for invoking qf</param>
-   70 /// <param name="qf">the action to invoke in case q resolves</param>
-   71 /// <param name="ef">the action to invoke otherwise</param>
-   72 public static void ForAll<T>(this ParallelQuery<T> query, Predicate<T> p, Action<T> pf, Predicate<T> q, Action<T> qf, Action<T> ef)
-   73 {
-   74 query.ForAll(o =>
-   75 {
-   76 if (p.Invoke(o))
-   77 {
-   78 pf.Invoke(o);
-   79 }
-   80 else if (q.Invoke(o))
-   81 {
-   82 qf.Invoke(o);
-   83 }
-   84 else
-   85 {
-   86 ef.Invoke(o);
-   87 }
-   88 });
-   89 }
-   90  
-   91 ///////////////////////////////////////////////////////////////////////////
-   92 // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 //
-   93 ///////////////////////////////////////////////////////////////////////////
-   94 /// <summary>
-   95 /// Invokes pf in case the predicate p resolves or qf in case the predicate q resolves.
-   96 /// </summary>
-   97 /// <typeparam name="T">the type of items in the query</typeparam>
-   98 /// <param name="query">the selector query</param>
-   99 /// <param name="p">the condition for invoking pf</param>
-   100 /// <param name="pf">the action to invoke in case p resolves</param>
-   101 /// <param name="q">the condition for invoking qf</param>
-   102 /// <param name="qf">the action to invoke in case q resolves</param>
-   103 public static void ForAll<T>(this ParallelQuery<T> query, Predicate<T> p, Action<T> pf, Predicate<T> q, Action<T> qf)
-   104 {
-   105 query.ForAll(o =>
-   106 {
-   107 if (p.Invoke(o))
-   108 {
-   109 pf.Invoke(o);
-   110 return;
-   111 }
-   112  
-   113 if (q.Invoke(o))
-   114 {
-   115 qf.Invoke(o);
-   116 return;
-   117 }
-   118 });
-   119 }
-   120  
-   121 ///////////////////////////////////////////////////////////////////////////
-   122 // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 //
-   123 ///////////////////////////////////////////////////////////////////////////
-   124 /// <summary>
-   125 /// Invokes pass if and only if predicate resovles or fail otherwise.
-   126 /// </summary>
-   127 /// <typeparam name="T">the type of items in the query</typeparam>
-   128 /// <param name="query">the selector query</param>
-   129 /// <param name="condition">the condition for invoking pf</param>
-   130 /// <param name="pass">the function to invoke in case the predicate resolves</param>
-   131 /// <param name="fail">the function to invoke otherwise</param>
-   132 public static void ForAll<T>(this ParallelQuery<T> query, Predicate<T> condition, Action<T> pass, Action<T> fail)
-   133 {
-   134 query.ForAll(o =>
-   135 {
-   136 switch (condition.Invoke(o))
-   137 {
-   138 case true:
-   139 pass.Invoke(o);
-   140 return;
-   141  
-   142 default:
-   143 fail.Invoke(o);
-   144 return;
-   145 }
-   146 });
-   147 }
-   148  
-   149 ///////////////////////////////////////////////////////////////////////////
-   150 // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 //
-   151 ///////////////////////////////////////////////////////////////////////////
-   152 /// <summary>
-   153 /// Invokes pass if and only if condition holds or fail otherwise.
-   154 /// </summary>
-   155 /// <typeparam name="T">the return type of the pass and fail functions</typeparam>
-   156 /// <param name="condition">the branch condition</param>
-   157 /// <param name="pass">function with no parameters and return type T in case condition passes</param>
-   158 /// <param name="fail">function with no parameters and return type T in case condition fails</param>
-   159 /// <returns>the result of pass in case condition holds or the result of fail otherwise</returns>
-   160 public static T IfElse<T>(this bool condition, Func<T> pass, Func<T> fail)
-   161 {
-   162 return condition ? pass.Invoke() : fail.Invoke();
-   163 }
-   164  
-   165 ///////////////////////////////////////////////////////////////////////////
-   166 // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 //
-   167 ///////////////////////////////////////////////////////////////////////////
-   168 /// <summary>
-   169 /// Invokes pass if and only if condition holds or fail otherwise.
-   170 /// </summary>
-   171 /// <typeparam name="U">the type of the argument to pass and fail</typeparam>
-   172 /// <typeparam name="V">the return type of pass and fail</typeparam>
-   173 /// <param name="condition">the branch condition</param>
-   174 /// <param name="pass">function that takes argument arg and returns type V in case condition holds</param>
-   175 /// <param name="fail">function that takes argument arg and returns type V in case condition fails</param>
-   176 /// <param name="arg">the argument passed to pass or fail functions</param>
-   177 /// <returns>the result of pass in case condition holds or the result of fail otherwise</returns>
-   178 public static V IfElse<U, V>(this bool condition, Func<U, V> pass, Func<U, V> fail, U arg = default(U))
-   179 {
-   180 return condition ? pass.Invoke(arg) : fail.Invoke(arg);
-   181 }
-   182  
-   183 ///////////////////////////////////////////////////////////////////////////
-   184 // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 //
-   185 ///////////////////////////////////////////////////////////////////////////
-   186 /// <summary>
-   187 /// Invokes pass if and only if condition holds or fail otherwise.
-   188 /// </summary>
-   189 /// <typeparam name="T">the type of the argument to pass and fail</typeparam>
-   190 /// <param name="condition">the branch condition</param>
-   191 /// <param name="pass">function that takes argument arg and returns nothing in case condition holds</param>
-   192 /// <param name="fail">function that takes argument arg and returns nothing in case condition fails</param>
-   193 /// <param name="arg">the optional argument passed to pass or fail functions</param>
-   194 public static void IfElse<T>(this bool condition, Action<T> pass, Action<T> fail, T arg = default(T))
-   195 {
-   196 switch (condition)
-   197 {
-   198 case true:
-   199 pass.Invoke(arg);
-   200 return;
-   201  
-   202 default:
-   203 fail.Invoke(arg);
-   204 return;
-   205 }
-   206 }
-   207  
-   208 ///////////////////////////////////////////////////////////////////////////
-   209 // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 //
-   210 ///////////////////////////////////////////////////////////////////////////
-   211 /// <summary>
-   212 /// Invokes pass if and only if condition holds or fail otherwise.
-   213 /// </summary>
-   214 /// <typeparam name="U">the type of the first argument to the pass or fail functions</typeparam>
-   215 /// <typeparam name="V">the type of the second argument to the pass or fail functions</typeparam>
-   216 /// <param name="condition">the branch condition</param>
-   217 /// <param name="pass">function that takes argument arg and returns nothing in case condition holds</param>
-   218 /// <param name="fail">function that takes argument arg and returns nothing in case condition fails</param>
-   219 /// <param name="arga">first optional argument passed to pass or fail functions</param>
-   220 /// <param name="argb">second optional argument passed to pass or fail functions</param>
-   221 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))
-   222 {
-   223 switch (condition)
-   224 {
-   225 case true:
-   226 pass.Invoke(arga, argb);
-   227 return;
-   228  
-   229 default:
-   230 fail.Invoke(arga, argb);
-   231 return;
-   232 }
-   233 }
-   234  
-   235 ///////////////////////////////////////////////////////////////////////////
16 // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // 236 // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 //
17 /////////////////////////////////////////////////////////////////////////// 237 ///////////////////////////////////////////////////////////////////////////
18 /// <summary> 238 /// <summary>
19 /// Returns true of an enumerable contains more than one element. 239 /// Returns true of an enumerable contains more than one element.
20 /// </summary> 240 /// </summary>