/Linq.cs |
@@ -13,6 +13,226 @@ |
public static class Extensions |
{ |
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // |
/////////////////////////////////////////////////////////////////////////// |
/// <summary> |
/// A functional implementation of a switch clause. |
/// </summary> |
/// <typeparam name="T">the type of items in the query</typeparam> |
/// <param name="query">the selector query</param> |
/// <param name="default">the function to execute when no case matches</param> |
/// <param name="case">a list of predicates representing the switch cases, |
/// where each predicate has to return True or False indicating whether |
/// fallthrough should occur. |
/// </param> |
public static void Switch<T>(this IEnumerable<T> query, |
// default |
Action<T> @default, |
// case |
// case |
// ... |
params Predicate<T>[] @case) |
{ |
if (@case.Length % 2 != 0) |
throw new ArgumentException("Pairs of predicates expected."); |
|
try |
{ |
using (var iter = query.GetEnumerator()) |
{ |
while (iter.MoveNext()) |
{ |
for (var i = 0; i < @case.Length; i += 2) |
{ |
if (@case[i].Invoke(iter.Current) && @case[i + 1].Invoke(iter.Current)) |
throw new Exception(); |
} |
} |
} |
@default.Invoke(query.Last()); |
} |
catch |
{ |
} |
} |
|
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // |
/////////////////////////////////////////////////////////////////////////// |
/// <summary> |
/// Invokes pf in case the predicate p resolves or qf in case the predicate q resolves, or ef otherwise. |
/// </summary> |
/// <typeparam name="T">the type of items in the query</typeparam> |
/// <param name="query">the selector query</param> |
/// <param name="p">the condition for invoking pf</param> |
/// <param name="pf">the action to invoke in case p resolves</param> |
/// <param name="q">the condition for invoking qf</param> |
/// <param name="qf">the action to invoke in case q resolves</param> |
/// <param name="ef">the action to invoke otherwise</param> |
public static void ForAll<T>(this ParallelQuery<T> query, Predicate<T> p, Action<T> pf, Predicate<T> q, Action<T> qf, Action<T> ef) |
{ |
query.ForAll(o => |
{ |
if (p.Invoke(o)) |
{ |
pf.Invoke(o); |
} |
else if (q.Invoke(o)) |
{ |
qf.Invoke(o); |
} |
else |
{ |
ef.Invoke(o); |
} |
}); |
} |
|
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // |
/////////////////////////////////////////////////////////////////////////// |
/// <summary> |
/// Invokes pf in case the predicate p resolves or qf in case the predicate q resolves. |
/// </summary> |
/// <typeparam name="T">the type of items in the query</typeparam> |
/// <param name="query">the selector query</param> |
/// <param name="p">the condition for invoking pf</param> |
/// <param name="pf">the action to invoke in case p resolves</param> |
/// <param name="q">the condition for invoking qf</param> |
/// <param name="qf">the action to invoke in case q resolves</param> |
public static void ForAll<T>(this ParallelQuery<T> query, Predicate<T> p, Action<T> pf, Predicate<T> q, Action<T> qf) |
{ |
query.ForAll(o => |
{ |
if (p.Invoke(o)) |
{ |
pf.Invoke(o); |
return; |
} |
|
if (q.Invoke(o)) |
{ |
qf.Invoke(o); |
return; |
} |
}); |
} |
|
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // |
/////////////////////////////////////////////////////////////////////////// |
/// <summary> |
/// Invokes pass if and only if predicate resovles or fail otherwise. |
/// </summary> |
/// <typeparam name="T">the type of items in the query</typeparam> |
/// <param name="query">the selector query</param> |
/// <param name="condition">the condition for invoking pf</param> |
/// <param name="pass">the function to invoke in case the predicate resolves</param> |
/// <param name="fail">the function to invoke otherwise</param> |
public static void ForAll<T>(this ParallelQuery<T> query, Predicate<T> condition, Action<T> pass, Action<T> fail) |
{ |
query.ForAll(o => |
{ |
switch (condition.Invoke(o)) |
{ |
case true: |
pass.Invoke(o); |
return; |
|
default: |
fail.Invoke(o); |
return; |
} |
}); |
} |
|
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // |
/////////////////////////////////////////////////////////////////////////// |
/// <summary> |
/// Invokes pass if and only if condition holds or fail otherwise. |
/// </summary> |
/// <typeparam name="T">the return type of the pass and fail functions</typeparam> |
/// <param name="condition">the branch condition</param> |
/// <param name="pass">function with no parameters and return type T in case condition passes</param> |
/// <param name="fail">function with no parameters and return type T in case condition fails</param> |
/// <returns>the result of pass in case condition holds or the result of fail otherwise</returns> |
public static T IfElse<T>(this bool condition, Func<T> pass, Func<T> fail) |
{ |
return condition ? pass.Invoke() : fail.Invoke(); |
} |
|
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // |
/////////////////////////////////////////////////////////////////////////// |
/// <summary> |
/// Invokes pass if and only if condition holds or fail otherwise. |
/// </summary> |
/// <typeparam name="U">the type of the argument to pass and fail</typeparam> |
/// <typeparam name="V">the return type of pass and fail</typeparam> |
/// <param name="condition">the branch condition</param> |
/// <param name="pass">function that takes argument arg and returns type V in case condition holds</param> |
/// <param name="fail">function that takes argument arg and returns type V in case condition fails</param> |
/// <param name="arg">the argument passed to pass or fail functions</param> |
/// <returns>the result of pass in case condition holds or the result of fail otherwise</returns> |
public static V IfElse<U, V>(this bool condition, Func<U, V> pass, Func<U, V> fail, U arg = default(U)) |
{ |
return condition ? pass.Invoke(arg) : fail.Invoke(arg); |
} |
|
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // |
/////////////////////////////////////////////////////////////////////////// |
/// <summary> |
/// Invokes pass if and only if condition holds or fail otherwise. |
/// </summary> |
/// <typeparam name="T">the type of the argument to pass and fail</typeparam> |
/// <param name="condition">the branch condition</param> |
/// <param name="pass">function that takes argument arg and returns nothing in case condition holds</param> |
/// <param name="fail">function that takes argument arg and returns nothing in case condition fails</param> |
/// <param name="arg">the optional argument passed to pass or fail functions</param> |
public static void IfElse<T>(this bool condition, Action<T> pass, Action<T> fail, T arg = default(T)) |
{ |
switch (condition) |
{ |
case true: |
pass.Invoke(arg); |
return; |
|
default: |
fail.Invoke(arg); |
return; |
} |
} |
|
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // |
/////////////////////////////////////////////////////////////////////////// |
/// <summary> |
/// Invokes pass if and only if condition holds or fail otherwise. |
/// </summary> |
/// <typeparam name="U">the type of the first argument to the pass or fail functions</typeparam> |
/// <typeparam name="V">the type of the second argument to the pass or fail functions</typeparam> |
/// <param name="condition">the branch condition</param> |
/// <param name="pass">function that takes argument arg and returns nothing in case condition holds</param> |
/// <param name="fail">function that takes argument arg and returns nothing in case condition fails</param> |
/// <param name="arga">first optional argument passed to pass or fail functions</param> |
/// <param name="argb">second optional argument passed to pass or fail functions</param> |
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)) |
{ |
switch (condition) |
{ |
case true: |
pass.Invoke(arga, argb); |
return; |
|
default: |
fail.Invoke(arga, argb); |
return; |
} |
} |
|
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
/////////////////////////////////////////////////////////////////////////// |
/// <summary> |