File tree 1 file changed +21
-4
lines changed
1 file changed +21
-4
lines changed Original file line number Diff line number Diff line change @@ -73,7 +73,10 @@ func (s Slice) Add(b Slice) (c Slice) {
73
73
}
74
74
75
75
func (s Slice ) Sum () (sum float64 ) {
76
- return floats .Sum (s )
76
+ for _ , v := range s {
77
+ sum += v
78
+ }
79
+ return sum
77
80
}
78
81
79
82
func (s Slice ) Mean () (mean float64 ) {
@@ -97,6 +100,18 @@ func (s Slice) Tail(size int) Slice {
97
100
return win
98
101
}
99
102
103
+ func (s Slice ) Average () float64 {
104
+ if len (s ) == 0 {
105
+ return 0.0
106
+ }
107
+
108
+ total := 0.0
109
+ for _ , value := range s {
110
+ total += value
111
+ }
112
+ return total / float64 (len (s ))
113
+ }
114
+
100
115
func (s Slice ) Diff () (values Slice ) {
101
116
for i , v := range s {
102
117
if i == 0 {
@@ -171,17 +186,19 @@ func (s Slice) Addr() *Slice {
171
186
func (s Slice ) Last () float64 {
172
187
length := len (s )
173
188
if length > 0 {
174
- return ( s ) [length - 1 ]
189
+ return s [length - 1 ]
175
190
}
176
191
return 0.0
177
192
}
178
193
194
+ // Index fetches the element from the end of the slice
195
+ // WARNING: it does not start from 0!!!
179
196
func (s Slice ) Index (i int ) float64 {
180
197
length := len (s )
181
- if length - i <= 0 || i < 0 {
198
+ if i < 0 || length - 1 - i < 0 {
182
199
return 0.0
183
200
}
184
- return ( s ) [length - i - 1 ]
201
+ return s [length - 1 - i ]
185
202
}
186
203
187
204
func (s Slice ) Length () int {
You can’t perform that action at this time.
0 commit comments