Last active
December 19, 2015 09:45
-
-
Save mz1988/205f7933345f06411137 to your computer and use it in GitHub Desktop.
Reddit ranking algorithms by Go
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
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