-
Notifications
You must be signed in to change notification settings - Fork 2
/
stepper.go
72 lines (54 loc) · 1.5 KB
/
stepper.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package bigo
import "errors"
var (
ErrInvalidStepSize = errors.New("stepSize must be greater than zero")
ErrUnorderedRange = errors.New("min must be smaller than max")
ErrEmptyStepsArray = errors.New("steps array must have at least one entry")
)
// Stepper implements the Next method. With every call it returned the next N and true.
// The boolean return parameter will be set to false when there are more steps to process.
type Stepper interface {
Next() (float64, bool)
}
// NewRangeStepper returns a Stepper that steps from min to max incremented by stepSize.
func NewRangeStepper(min, max, stepSize float64) *RangeStepper {
if stepSize <= 0 {
panic(ErrInvalidStepSize)
}
if min >= max {
panic(ErrUnorderedRange)
}
return &RangeStepper{max: max, stepSize: stepSize, current: min}
}
type RangeStepper struct {
max float64
stepSize float64
current float64
}
func (i *RangeStepper) Next() (float64, bool) {
if i.current > i.max {
return 0, false
}
value := i.current
i.current += i.stepSize
return value, true
}
// NewArrayStepper returns a Stepper that steps from the beginning to the end of the provided array.
func NewArrayStepper(steps []float64) *ArrayStepper {
if len(steps) == 0 {
panic(ErrEmptyStepsArray)
}
return &ArrayStepper{steps: steps}
}
type ArrayStepper struct {
steps []float64
current int
}
func (a *ArrayStepper) Next() (float64, bool) {
if a.current >= len(a.steps) {
return 0, false
}
var value = a.steps[a.current]
a.current++
return value, true
}