Slippage and more statistics

This commit is contained in:
Luke I. Wilson
2023-05-18 20:17:29 -05:00
parent 565aa6c9fd
commit 5a0a4d0c33
5 changed files with 99 additions and 16 deletions

View File

@@ -46,6 +46,7 @@ type Series interface {
Filter(f func(i int, val any) bool) Series // Where returns a new Series with only the values that return true for the given function.
Map(f func(i int, val any) any) Series // Map returns a new Series with the values modified by the given function.
MapReverse(f func(i int, val any) any) Series // MapReverse is the same as Map but it starts from the last item and works backwards.
ForEach(f func(i int, val any)) Series // ForEach calls f for each item in the Series.
// Statistical functions.
@@ -128,6 +129,11 @@ func (s *AppliedSeries) MapReverse(f func(i int, val any) any) Series {
return NewAppliedSeries(s.Series.MapReverse(f), s.apply)
}
func (s *AppliedSeries) ForEach(f func(i int, val any)) Series {
_ = s.Series.ForEach(f)
return s
}
func (s *AppliedSeries) WithValueFunc(value func(i int) any) Series {
return &AppliedSeries{Series: s.Series.WithValueFunc(value), apply: s.apply}
}
@@ -189,6 +195,11 @@ func (s *RollingSeries) MapReverse(f func(i int, val any) any) Series {
return NewRollingSeries(s.Series.MapReverse(f), s.period)
}
func (s *RollingSeries) ForEach(f func(i int, val any)) Series {
_ = s.Series.ForEach(f)
return s
}
// Average is an alias for Mean.
func (s *RollingSeries) Average() *AppliedSeries {
return s.Mean()
@@ -531,6 +542,13 @@ func (s *DataSeries) MapReverse(f func(i int, val any) any) Series {
return series
}
func (s *DataSeries) ForEach(f func(i int, val any)) Series {
for i := 0; i < s.Len(); i++ {
f(i, s.value(i))
}
return s
}
func (s *DataSeries) Rolling(period int) *RollingSeries {
return NewRollingSeries(s, period)
}