wasSharp – Diff between revs 47 and 49
?pathlinks?
Rev 47 | Rev 49 | |||
---|---|---|---|---|
Line 64... | Line 64... | |||
64 | |
64 | |
|
65 | /////////////////////////////////////////////////////////////////////////// |
65 | /////////////////////////////////////////////////////////////////////////// |
|
66 | // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // |
66 | // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // |
|
67 | /////////////////////////////////////////////////////////////////////////// |
67 | /////////////////////////////////////////////////////////////////////////// |
|
- | 68 | /// <summary> |
||
- | 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 | /////////////////////////////////////////////////////////////////////////// |
||
68 | /// <summary> |
112 | /// <summary> |
|
69 | /// Invokes pf in case the predicate p resolves or qf in case the predicate q resolves, or ef otherwise. |
113 | /// Invokes pf in case the predicate p resolves or qf in case the predicate q resolves, or ef otherwise. |
|
70 | /// </summary> |
114 | /// </summary> |
|
71 | /// <typeparam name="T">the type of items in the query</typeparam> |
115 | /// <typeparam name="T">the type of items in the query</typeparam> |
|
72 | /// <param name="query">the selector query</param> |
116 | /// <param name="query">the selector query</param> |