-
Notifications
You must be signed in to change notification settings - Fork 1
/
numeric_range.go
45 lines (36 loc) · 1.1 KB
/
numeric_range.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package enu
import "golang.org/x/exp/constraints"
func FromNumericRange[T constraints.Integer | constraints.Float](min, max T) *NumericEnumerable[T] {
return NewNumeric[T](NewNumericRange(min, max))
}
func NewNumericRange[T constraints.Integer | constraints.Float](min, max T) *RangeEnumerator[T, T] {
return NewNumericRangeWithStep(min, max, T(1))
}
func FromNumericRangeWithStep[T constraints.Integer | constraints.Float](min, max, step T) *NumericEnumerable[T] {
return NewNumeric[T](NewNumericRangeWithStep(min, max, step))
}
func NewNumericRangeWithStep[T constraints.Integer | constraints.Float](min, max, step T) *RangeEnumerator[T, T] {
return NewRange[T, T](
Numeric[T]{value: min},
Numeric[T]{value: max},
step,
)
}
type Numeric[T constraints.Integer | constraints.Float] struct {
value T
}
func (self Numeric[T]) Next(step T) RangeValuer[T, T] {
return Numeric[T]{value: self.value + T(step)}
}
func (self Numeric[T]) Value() T {
return T(self.value)
}
func (self Numeric[T]) Compare(other T) int {
if T(self.value) < other {
return -1
}
if T(self.value) > other {
return 1
}
return 0
}