1 | #Region "Microsoft.VisualBasic::811bb9408a27e609223757a9f9def80b, Microsoft.VisualBasic.Core\Scripting\Runtime\Where.vb" |
2 | |
3 | ' Author: |
4 | ' |
5 | ' asuka (amethyst.asuka@gcmodeller.org) |
6 | ' xie (genetics@smrucc.org) |
7 | ' xieguigang (xie.guigang@live.com) |
8 | ' |
9 | ' Copyright (c) 2018 GPL3 Licensed |
10 | ' |
11 | ' |
12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) |
13 | ' |
14 | ' |
15 | ' This program is free software: you can redistribute it and/or modify |
16 | ' it under the terms of the GNU General Public License as published by |
17 | ' the Free Software Foundation, either version 3 of the License, or |
18 | ' (at your option) any later version. |
19 | ' |
20 | ' This program is distributed in the hope that it will be useful, |
21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of |
22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23 | ' GNU General Public License for more details. |
24 | ' |
25 | ' You should have received a copy of the GNU General Public License |
26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. |
27 | |
28 | |
29 | |
30 | ' /********************************************************************************/ |
31 | |
32 | ' Summaries: |
33 | |
34 | ' Enum Logics |
35 | ' |
36 | ' [And], [AndAlso], [Not], [Or], [OrElse] |
37 | ' [XOr] |
38 | ' |
39 | ' |
40 | ' |
41 | ' |
42 | ' |
43 | ' Module [Where] |
44 | ' |
45 | ' Function: __and, __andAlso, __or, __orElse, BuildAll |
46 | ' |
47 | ' |
48 | ' /********************************************************************************/ |
49 | |
50 | #End Region |
51 | |
52 | Imports System.Runtime.CompilerServices |
53 | |
54 | Namespace Scripting.Runtime |
55 | |
56 | Public Enum Logics |
57 | |
58 | ''' <summary> |
59 | ''' 两边的逻辑表达式都会被计算 |
60 | ''' </summary> |
61 | [And] |
62 | ''' <summary> |
63 | ''' 两边的逻辑表达式都会被计算 |
64 | ''' </summary> |
65 | [Or] |
66 | ''' <summary> |
67 | ''' 相比于<see cref="Logics.And"/>,这个逻辑运算是会短路的 |
68 | ''' </summary> |
69 | [AndAlso] |
70 | ''' <summary> |
71 | ''' 相比于<see cref="Logics.Or"/>,这个逻辑运算是会短路的 |
72 | ''' </summary> |
73 | [OrElse] |
74 | [Not] |
75 | [XOr] |
76 | |
77 | End Enum |
78 | |
79 | ''' <summary> |
80 | ''' Merge the logical lambda into one lambda function by a specifc logical operation in <see cref="Logics"/>. |
81 | ''' </summary> |
82 | Public Module [Where] |
83 | |
84 | ''' <summary> |
85 | ''' Not support <see cref="Logics.Not"/> |
86 | ''' </summary> |
87 | ''' <typeparam name="T"></typeparam> |
88 | ''' <param name="logic"></param> |
89 | ''' <param name="tests"></param> |
90 | ''' <returns></returns> |
91 | Public Function BuildAll(Of T)(logic As Logics, ParamArray tests As Func(Of T, Boolean)()) As Func(Of T, Boolean) |
92 | Select Case logic |
93 | Case Logics.And |
94 | Return Function(x) x.__and(tests) |
95 | Case Logics.AndAlso |
96 | Return Function(x) x.__andAlso(tests) |
97 | Case Logics.Or |
98 | Return Function(x) x.__or(tests) |
99 | Case Logics.OrElse |
100 | Return Function(x) x.__orElse(tests) |
101 | Case Logics.XOr |
102 | Return Function(x) Not x.__orElse(tests) |
103 | Case Else |
104 | Throw New NotSupportedException(logic.ToString) |
105 | End Select |
106 | End Function |
107 | |
108 | <Extension> |
109 | Private Function __orElse(Of T)(x As T, tests As Func(Of T, Boolean)()) As Boolean |
110 | For Each test As Func(Of T, Boolean) In tests |
111 | If test(x) = True Then |
112 | Return True |
113 | End If |
114 | Next |
115 | |
116 | Return False |
117 | End Function |
118 | |
119 | <Extension> |
120 | Private Function __andAlso(Of T)(x As T, tests As Func(Of T, Boolean)()) As Boolean |
121 | For Each test As Func(Of T, Boolean) In tests |
122 | If test(x) = False Then |
123 | Return False |
124 | End If |
125 | Next |
126 | |
127 | Return True |
128 | End Function |
129 | |
130 | ''' <summary> |
131 | ''' <see cref="Logics.Or"/>, 所有的表达式都会被计算 |
132 | ''' </summary> |
133 | ''' <typeparam name="T"></typeparam> |
134 | ''' <param name="x"></param> |
135 | ''' <param name="tests"></param> |
136 | ''' <returns></returns> |
137 | <Extension> |
138 | Private Function __or(Of T)(x As T, tests As Func(Of T, Boolean)()) As Boolean |
139 | Dim [true] As Boolean = False |
140 | |
141 | For Each test As Func(Of T, Boolean) In tests |
142 | If test(x) = True Then |
143 | [true] = True |
144 | End If |
145 | Next |
146 | |
147 | Return [true] |
148 | End Function |
149 | |
150 | ''' <summary> |
151 | ''' <see cref="Logics.And"/>, 所有的表达式都会被计算 |
152 | ''' </summary> |
153 | ''' <param name="tests"></param> |
154 | ''' <returns></returns> |
155 | <Extension> |
156 | Private Function __and(Of T)(x As T, tests As Func(Of T, Boolean)()) As Boolean |
157 | Dim [false] As Boolean = False |
158 | |
159 | For Each test As Func(Of T, Boolean) In tests |
160 | If test(x) = False Then |
161 | [false] = True |
162 | End If |
163 | Next |
164 | |
165 | If [false] Then |
166 | Return False |
167 | Else |
168 | Return True |
169 | End If |
170 | End Function |
171 | End Module |
172 | End Namespace |