Skip to content

Instantly share code, notes, and snippets.

@mz1988
Last active December 19, 2015 09:45
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mz1988/205f7933345f06411137 to your computer and use it in GitHub Desktop.
Save mz1988/205f7933345f06411137 to your computer and use it in GitHub Desktop.
Reddit ranking algorithms by Go
package main
import (
"fmt"
"math"
"time"
)
func epoch_seconds(date time.Time) (sec float64) {
/* Returns the number of seconds from the epoch to date. */
epoch := time.Date(1970, 1, 1, 0, 0, 0, 0, time.Local).Unix()
td := date.Unix() - epoch
sec = float64(td)
return
}
func score(ups float64, downs float64) (score float64) {
return ups - downs
}
func hot(ups float64, downs float64, date time.Time) (finalScore float64) {
/* The hot formula. Should match the equivalent function in postgres. */
s := score(ups, downs)
order := math.Log10(math.Max(math.Abs(s), 1))
var sign float64
if s < 0 {
sign = -1
} else if s > 0 {
sign = 1
} else {
sign = 0
}
seconds := epoch_seconds(date) - 1134028003
finalScore = sign*order + seconds/45000
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment