Created
October 1, 2013 19:15
-
-
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?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could you use
*e = SimpleEWMA(value * DECAY) + (*e * (1 - DECAY))