-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsequence.go
More file actions
122 lines (97 loc) · 3.04 KB
/
sequence.go
File metadata and controls
122 lines (97 loc) · 3.04 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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package sqrt
import (
"context"
"iter"
"strings"
)
// Sequence represents a sequence of digits of either finite or infinite
// length within the mantissa of a real number. Although they can start
// and optionally end anywhere within a mantissa, Sequences must be
// contiguous. That is they can have no gaps in the middle.
type Sequence interface {
// All returns the 0 based position and value of each digit in this
// Sequence from beginning to end.
All() iter.Seq2[int, int]
// AllInRange returns the 0 based position and value of each digit in
// this sequence from position start up to but not including position
// end.
AllInRange(start, end int) iter.Seq2[int, int]
// Values returns the value of each digit in this Sequence from
// beginning to end.
Values() iter.Seq[int]
// WithStart returns a view of this Sequence that only has digits with
// zero based positions greater than or equal to start.
WithStart(start int) Sequence
// WithEnd returns a view of this Sequence that only has digits with
// zero based positions less than end.
WithEnd(end int) FiniteSequence
// PrimeToStart performs any necessary computations up front to ensure
// that this sequence can be iterated over without any initial lag.
PrimeToStart(ctx context.Context) error
private()
}
// FiniteSequence represents a Sequence of finite length.
type FiniteSequence interface {
Sequence
// Backward returns the 0 based position and value of each digit in this
// FiniteSequence from end to beginning.
Backward() iter.Seq2[int, int]
// FiniteWithStart works like WithStart except that it returns a
// FiniteSequence.
FiniteWithStart(start int) FiniteSequence
// PrimeToEnd performs any necessary computations up front to ensure
// that this sequence can be iterated over with Backward without any
// initial lag.
PrimeToEnd(ctx context.Context) error
}
// AsString returns all the digits in s as a string.
func AsString(s FiniteSequence) string {
var sb strings.Builder
for digit := range s.Values() {
sb.WriteByte('0' + byte(digit))
}
return sb.String()
}
type sequence struct {
sequencePart
}
func (s *sequence) WithStart(start int) Sequence {
result := s.withStart(start)
if result == s.sequencePart {
return s
}
return &sequence{result}
}
func (s *sequence) WithEnd(end int) FiniteSequence {
return &finiteSequence{s.withEnd(end)}
}
func (s *sequence) private() {
}
type finiteSequence struct {
sequencePart
}
func (f *finiteSequence) WithStart(start int) Sequence {
return f.FiniteWithStart(start)
}
func (f *finiteSequence) FiniteWithStart(start int) FiniteSequence {
result := f.withStart(start)
if result == f.sequencePart {
return f
}
return &finiteSequence{result}
}
func (f *finiteSequence) WithEnd(end int) FiniteSequence {
result := f.withEnd(end)
if result == f.sequencePart {
return f
}
return &finiteSequence{result}
}
func (f *finiteSequence) Backward() iter.Seq2[int, int] {
return f.backward()
}
func (f *finiteSequence) PrimeToEnd(ctx context.Context) error {
return f.primeToEnd(ctx)
}
func (f *finiteSequence) private() {
}