1 #Region "Microsoft.VisualBasic::71ee759ed63d841ed4e808bcd1d165c7, Microsoft.VisualBasic.Core\Language\API.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     '     Module LanguageAPI
35     
36     '         Function: [ByRef], [Default], (+2 Overloads) [When], AsDefault, AsNumeric
37     '                   AsString, AsVector, DefaultValue, Empty, IsNothing
38     '                   Let, list, Self, TypeDef, TypeInfo
39     
40     
41     ' /********************************************************************************/
42
43 #End Region
44
45 Imports System.Runtime.CompilerServices
46 Imports Microsoft.VisualBasic.Language.Default
47 Imports Microsoft.VisualBasic.Language.Perl
48 Imports Microsoft.VisualBasic.Language.Vectorization
49 Imports Microsoft.VisualBasic.Scripting.Runtime
50
51 Namespace Language
52
53     ''' <summary>
54     ''' The VisualBasic language syntax helper API.
55     ''' </summary>
56     Public Module LanguageAPI
57
58         ''' <summary>
59         ''' The default value assertor. If target object assert result is nothing or empty, then this function will returns True.
60         ''' </summary>
61         Friend ReadOnly defaultAssert As New DefaultValue(Of Assert(Of Object)) With {
62             .Value = AddressOf ExceptionHandle.Default,
63             .assert = Function(assert)
64                           Return assert Is Nothing
65                       End Function
66         }
67
68         <MethodImpl(MethodImplOptions.AggressiveInlining)>
69         Public Function TypeDef(Of T)() As TypeSchema
70             Return New TypeSchema(GetType(T))
71         End Function
72
73         <MethodImpl(MethodImplOptions.AggressiveInlining)>
74         <Extension>
75         Public Function TypeInfo(Of T)(x As T) As TypeSchema
76             Return TypeDef(Of T)()
77         End Function
78
79         'Private Sub test()
80         '    If TypeDef(Of Integer)() Or {GetType(Integer), GetType(Double)} Then
81         '    End If
82         'End Sub
83
84         <MethodImpl(MethodImplOptions.AggressiveInlining)>
85         Public Function Empty(Of T)() As DefaultValue(Of T())
86             Return {}
87         End Function
88
89         ''' <summary>
90         ''' simulate the ``%||%`` operator in R language.
91         ''' 
92         ''' 模拟R语言之中的``%||%``操作符
93         ''' 
94         ''' ```R
95         ''' `%||%` &lt;- function(x, y) if (is.null(x)) y else x
96         ''' 
97         ''' NULL %||% 123
98         ''' # 123
99         ''' 
100         ''' 233 %||% 123
101         ''' # 233
102         ''' ```
103         ''' </summary>
104         ''' <typeparam name="T"></typeparam>
105         ''' <param name="x"></param>
106         ''' <param name="isNothing"></param>
107         ''' <returns></returns>
108         <MethodImpl(MethodImplOptions.AggressiveInlining)>
109         Public Function [Default](Of T)(x As T, Optional isNothing As Assert(Of Object) = NothingAs DefaultValue(Of T)
110             Return New DefaultValue(Of T) With {
111                 .Value = x,
112                 .assert = isNothing Or defaultAssert
113             }
114         End Function
115
116         ''' <summary>
117         ''' Using this value as the default value for this <typeparamref name="T"/> type.
118         ''' </summary>
119         ''' <typeparam name="T"></typeparam>
120         ''' <param name="x"></param>
121         ''' <param name="[If]"></param>
122         ''' <returns></returns>
123         <MethodImpl(MethodImplOptions.AggressiveInlining)>
124         <Extension>
125         Public Function AsDefault(Of T)(x As T, Optional [If] As Assert(Of Object) = NothingAs DefaultValue(Of T)
126             Return [Default](x, [If])
127         End Function
128
129         Public Function DefaultValue(Of T)(value As T) As DefaultValue(Of T)
130             Return New DefaultValue(Of T) With {
131                 .Value = value,
132                 .assert = defaultAssert
133             }
134         End Function
135
136         <MethodImpl(MethodImplOptions.AggressiveInlining)>
137         <Extension>
138         Public Function [When](Of T)([default] As T, expression As BooleanAs DefaultValue(Of T)
139             Return [default].AsDefault().When(expression)
140         End Function
141
142         <MethodImpl(MethodImplOptions.AggressiveInlining)>
143         <Extension>
144         Public Function [When](Of T)([default] As T, expression As Assert(Of T)) As DefaultValue(Of T)
145             Return [default].AsDefault().When(assert:=expression)
146         End Function
147
148         ''' <summary>
149         ''' Helper for update the value property of <see cref="Value(Of T)"/>
150         ''' 
151         ''' ```vbnet
152         ''' Call Let$(<see cref="Value(Of T)"/> = x)
153         ''' ```
154         ''' </summary>
155         ''' <typeparam name="T"></typeparam>
156         ''' <param name="value"></param>
157         ''' <returns></returns>
158         <MethodImpl(MethodImplOptions.AggressiveInlining)>
159         Public Function Let$(Of T)(value As T)
160             Try
161                 Return CStrSafe(value)
162             Catch ex As Exception
163                 ' 在这里知识进行帮助值的设置,所以这个错误无所谓,直接忽略掉
164                 Return Nothing
165             End Try
166         End Function
167
168 #Region "Helper for ``With``"
169
170         ''' <summary>
171         ''' My self: <see cref="LanguageAPI.ByRef(Of T)(T)"/>
172         ''' </summary>
173         ''' <typeparam name="T"></typeparam>
174         ''' <returns></returns>
175         <MethodImpl(MethodImplOptions.AggressiveInlining)>
176         Public Function Self(Of T)() As Func(Of T, T)
177             Return AddressOf [ByRef]
178         End Function
179
180         ''' <summary>
181         ''' Extension method for VisualBasic ``With`` anonymous variable syntax source reference helper
182         ''' </summary>
183         ''' <typeparam name="T"></typeparam>
184         ''' <param name="x"></param>
185         ''' <returns></returns>
186         ''' 
187         <MethodImpl(MethodImplOptions.AggressiveInlining)>
188         <Extension> Public Function [ByRef](Of T)(x As T) As T
189             Return x
190         End Function
191
192         ''' <summary>
193         ''' Extension method for VisualBasic ``With`` anonymous variable syntax for determine that source reference is nothing or not?
194         ''' </summary>
195         ''' <typeparam name="T"></typeparam>
196         ''' <param name="x"></param>
197         ''' <returns></returns>
198         <MethodImpl(MethodImplOptions.AggressiveInlining)>
199         <Extension>
200         Public Function IsNothing(Of T As Class)(x As T) As Boolean
201             Return x Is Nothing
202         End Function
203 #End Region
204
205         <MethodImpl(MethodImplOptions.AggressiveInlining)>
206         <Extension>
207         Public Function AsVector(strings As IEnumerable(Of String)) As StringVector
208             Return New StringVector(strings)
209         End Function
210
211         <MethodImpl(MethodImplOptions.AggressiveInlining)>
212         Public Function list(ParamArray args As ArgumentReference()) As Dictionary(Of StringObject)
213             Return args.ToDictionary(Function(a) a.name, Function(a) a.value)
214         End Function
215
216         <MethodImpl(MethodImplOptions.AggressiveInlining)>
217         <Extension>
218         Public Function AsNumeric(list As Dictionary(Of StringObject)) As Dictionary(Of StringDouble)
219             Return list.ToDictionary(Function(t) t.Key, Function(t) CDbl(t.Value))
220         End Function
221
222         <MethodImpl(MethodImplOptions.AggressiveInlining)>
223         <Extension>
224         Public Function AsString(list As Dictionary(Of StringObject)) As Dictionary(Of StringString)
225             Return list.ToDictionary(Function(t) t.Key, Function(t) Scripting.ToString(t.Value))
226         End Function
227     End Module
228 End Namespace