This repository was archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrange.go
More file actions
43 lines (39 loc) · 1.24 KB
/
range.go
File metadata and controls
43 lines (39 loc) · 1.24 KB
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
package loop
// Range creates a function iterator to iterate between two given
// integer like values.
//
// The first argument is the starting value, which is included in the
// iteration. The second argument is the stop value, which is when the
// iteration is stopped. This value is not included.
//
// This function is basically loop.RangeWithStep(start, stop, 1)
func Range[Int intType](start Int, stop Int) func(func(Int) bool) {
return RangeWithStep(start, stop, 1)
}
// RangeWithStep creates a function iterator to iterate between two values
// with a given step incrementor.
//
// The first value is always returned (provided the stop value is value for
// the step amount)
// The stop value is not included.
// The step value can be either either greater than or less than 0. If the
// step is 0 then no iteration will take place.
func RangeWithStep[Int intType](start Int, stop Int, step Int) func(func(Int) bool) {
return func(yield func(Int) bool) {
if step == 0 {
return
}
delta := int64(stop) - int64(start)
steps := int64(delta) / int64(step)
rem := int64(delta) % int64(step)
if rem > 0 {
steps += 1
}
for i := int64(0); i < steps; i++ {
num := i*int64(step) + int64(start)
if !yield(Int(num)) {
return
}
}
}
}