Skip to content

Instantly share code, notes, and snippets.

@xaprb
Created October 1, 2013 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xaprb/6783585 to your computer and use it in GitHub Desktop.
Save xaprb/6783585 to your computer and use it in GitHub Desktop.
How to define a type as a rebranded native type. Downside: lots of casting. Good or bad?
type SimpleEWMA float64
// Value returns the current value of the moving average.
func (e *SimpleEWMA) Value() float64 {
return float64(*e)
}
// Add adds a value to the series and updates the moving average.
func (e *SimpleEWMA) Add(value float64) {
if *e == 0 { // this is a proxy for "uninitialized"
*e = SimpleEWMA(value)
} else {
*e = (SimpleEWMA(value) * SimpleEWMA(DECAY)) + (*e * (1 - SimpleEWMA(DECAY)))
}
}
@DanielHeath
Copy link

Could you use *e = SimpleEWMA(value * DECAY) + (*e * (1 - DECAY))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment