-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiter.go
59 lines (56 loc) · 1.04 KB
/
iter.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
// Package iter provides functionality like Python's range function
// to iterate over numbers and letters
package iter
// N mimic the python range function.
// N(10) -> generate a sequence from 0 to 9.
// N(3, 10) -> generate a sequence from 3 to 9.
// N(3, 10, 2) -> generate a sequence from 3 to 9 by incrementing value by 2.
func N(in ...int) <-chan int {
ch := make(chan int)
var start, end int
step := 1
switch len(in) {
case 0:
close(ch)
return ch
case 1:
if in[0] > 0 {
end = in[0]
}
case 2:
if in[0] > 0 {
start = in[0]
}
if in[0] > 0 {
end = in[1]
}
case 3:
if in[0] > 0 {
start = in[0]
}
if in[1] > 0 {
end = in[1]
}
if in[2] > 0 {
step = in[2]
}
}
go func(ch1 chan<- int) {
for i := start; i < end; i += step {
ch1 <- i
}
close(ch1)
}(ch)
return ch
}
// L generate a range of letters, such as: a-z or A-Z.
func L(start, end byte) <-chan byte {
ch := make(chan byte)
go func(ch1 chan<- byte) {
for i := start; i <= end; i++ {
ch1 <- i
}
close(ch1)
}(ch)
return ch
}