1 #Region "Microsoft.VisualBasic::b61fb90525a79191455dc08f6f84ebf6, Microsoft.VisualBasic.Core\Extensions\Collection\Linq\NumericSequence.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 NumericSequence
35     
36     '         FunctionInRange, (+7 Overloads) Range
37     
38     
39     ' /********************************************************************************/
40
41 #End Region
42
43 Imports System.Runtime.CompilerServices
44 Imports Microsoft.VisualBasic.Language
45 Imports Microsoft.VisualBasic.Language.Default
46
47 Namespace Linq
48
49     Public Module NumericSequence
50
51         <Extension>
52         Public Function InRange(Of T As IComparable)(x As T, min As T, max As T) As Boolean
53             If x.LessThanOrEquals(max) AndAlso x.GreaterThanOrEquals(min) Then
54                 Return True
55             Else
56                 Return False
57             End If
58         End Function
59
60         Public Iterator Function Range(from%, to%, Optional step% = 1) As IEnumerable(Of Integer)
61             For i As Integer = from To [to] Step [step]
62                 Yield i
63             Next
64         End Function
65
66         Public Iterator Function Range(from#, to#, Optional step# = 1) As IEnumerable(Of Double)
67             For i As Double = from To [to] Step [step]
68                 Yield i
69             Next
70         End Function
71
72         Public Iterator Function Range(from!, to!, Optional step! = 1) As IEnumerable(Of Single)
73             For i As Single = from To [to] Step [step]
74                 Yield i
75             Next
76         End Function
77
78         Public Iterator Function Range(from&, to&, Optional step& = 1) As IEnumerable(Of Long)
79             For i As Long = from To [to] Step [step]
80                 Yield i
81             Next
82         End Function
83
84         Public Iterator Function Range(from@, to@, Optional step@ = 1) As IEnumerable(Of Decimal)
85             For i As Decimal = from To [to] Step [step]
86                 Yield i
87             Next
88         End Function
89
90         Public Iterator Function Range(from As Short, [to] As ShortOptional [step] As Short = 1) As IEnumerable(Of Short)
91             For i As Short = from To [to] Step [step]
92                 Yield i
93             Next
94         End Function
95
96         Public ReadOnly OneDay As DefaultValue(Of TimeSpan) = New TimeSpan(days:=1, hours:=0, minutes:=0, seconds:=0)
97         Public ReadOnly OneHour As DefaultValue(Of TimeSpan) = New TimeSpan(hours:=1, minutes:=0, seconds:=0)
98         Public ReadOnly OneMinute As DefaultValue(Of TimeSpan) = New TimeSpan(minutes:=1, seconds:=0, hours:=0)
99         Public ReadOnly OneSecond As DefaultValue(Of TimeSpan) = New TimeSpan(seconds:=1, hours:=0, minutes:=0)
100
101         ''' <summary>
102         ''' 
103         ''' </summary>
104         ''' <param name="from"></param>
105         ''' <param name="[to]"></param>
106         ''' <param name="step%">Milliseconds</param>
107         ''' <returns></returns>
108         Public Iterator Function Range(from As Date, [to] As DateOptional [step] As TimeSpan = NothingAs IEnumerable(Of Date)
109             Dim t As TimeSpan = [step] Or OneDay
110
111             ' 由于后面的Do循环之中from是放在加减法之后的,所以需要在这里把最开始的值返回,要不然第一个值会被丢失的。。。
112             Yield from
113
114             If from < [to] Then
115                 Do While from < [to]
116                     from = from + t
117                     Yield from
118                 Loop
119             Else
120                 Do While from > [to]
121                     from = from - t
122                     Yield from
123                 Loop
124             End If
125         End Function
126     End Module
127 End Namespace