Skip to content

Commit

Permalink
add spread stateful function
Browse files Browse the repository at this point in the history
  • Loading branch information
jsvisa committed Jun 1, 2016
1 parent 8ab770d commit b29af9d
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion tick/stateful/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,15 @@ func init() {

// Return set of built-in Funcs
func NewFunctions() Funcs {
funcs := make(Funcs, len(statelessFuncs)+2)
funcs := make(Funcs, len(statelessFuncs)+3)
for n, f := range statelessFuncs {
funcs[n] = f
}

// Statefull functions -- need new instance
funcs["sigma"] = &sigma{}
funcs["count"] = &count{}
funcs["spread"] = &spread{min: math.Inf(+1), max: math.Inf(-1)}

return funcs
}
Expand Down Expand Up @@ -447,6 +448,37 @@ func (s *sigma) Call(args ...interface{}) (interface{}, error) {
return math.Abs(x-s.mean) / math.Sqrt(s.variance), nil
}

type spread struct {
min float64
max float64
}

func (s *spread) Reset() {
s.min = math.Inf(+1)
s.max = math.Inf(-1)
}

// Computes the running range of all values
func (s *spread) Call(args ...interface{}) (interface{}, error) {
if len(args) != 1 {
return 0, errors.New("spread expects exactly one argument")
}
x, ok := args[0].(float64)
if !ok {
return nil, ErrNotFloat
}

if x < s.min {
s.min = x
}

if x > s.max {
s.max = x
}

return s.max - s.min, nil
}

type minute struct {
}

Expand Down

0 comments on commit b29af9d

Please sign in to comment.