wasSharp

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 48  →  ?path2? @ 49
/Linq.cs
@@ -66,6 +66,50 @@
// Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 //
///////////////////////////////////////////////////////////////////////////
/// <summary>
/// A functional implementation of a switch clause.
/// </summary>
/// <typeparam name="T">the type of the item to 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>
/// <remarks>when used, the default action must be explicitly specified
/// due to language restrictions
/// </remarks>
public static void Switch<T>(this T query,
// default
Action<T> @default,
// case
// case
// ...
params Predicate<T>[] @case)
{
if (@case.Length % 2 != 0)
throw new ArgumentException("Pairs of predicates expected.");
 
var match = false;
for (var i = 0; i < @case.Length; i += 2)
{
if (!@case[i].Invoke(query))
continue;
 
if (@case[i + 1].Invoke(query))
return;
 
match = true;
}
 
if (!match)
@default.Invoke(query);
 
}
 
///////////////////////////////////////////////////////////////////////////
// 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>