-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.go
46 lines (35 loc) · 809 Bytes
/
reader.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
package equation
import (
"regexp"
)
func splitter(str string) []string {
reg := regexp.MustCompile(`\d+\.\d+|\W|\w+`)
return reg.FindAllString(str, -1)
}
func nextElement(arr []string, index int) string {
if index+1 >= len(arr) || index+1 < 0 {
return ""
}
return arr[index+1]
}
func prevElement(arr []string, index int) string {
if index-1 < 0 || index-1 >= len(arr) {
return ""
}
return arr[index-1]
}
type Reader func(step int, peek bool) (string, int)
func createReader(str []string) Reader {
current := -1
return func(step int, peek bool) (string, int) {
if peek == false {
defer func() {
current = current + step
}()
}
if step >= 0 {
return nextElement(str, current+step-1), current + step
}
return prevElement(str, current+step+1), current + step
}
}