-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
169 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# stats [![GoDoc](https://godoc.org/github.com/vadv/gopher-lua-libs/stats?status.svg)](https://godoc.org/github.com/vadv/gopher-lua-libs/stats) | ||
|
||
## Usage | ||
|
||
```lua | ||
local stats = require("stats") | ||
|
||
local result, _ = stats.median({0,0,10}) | ||
print(result) | ||
-- Output: 0 | ||
|
||
local result, _ = stats.percentile({0,0,10}, 100) | ||
print(result) | ||
-- Output: 10 | ||
``` | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Package strings implements golang package montanaflynn/stats functionality for lua. | ||
package stats | ||
|
||
import ( | ||
"fmt" | ||
|
||
gostats "github.com/montanaflynn/stats" | ||
lua "github.com/yuin/gopher-lua" | ||
) | ||
|
||
// get float slice from table | ||
func getFloatSliceFromTable(L *lua.LState, n int) ([]float64, error) { | ||
tbl := L.CheckTable(n) | ||
data := make([]float64, tbl.Len()) | ||
var err error | ||
tbl.ForEach(func(k lua.LValue, v lua.LValue) { | ||
value, ok := v.(lua.LNumber) | ||
if !ok { | ||
err = fmt.Errorf("only table of numbers is supported") | ||
return | ||
} | ||
data = append(data, float64(value)) | ||
}) | ||
return data, err | ||
} | ||
|
||
// Median lua stats.median(table): port of go montanaflynn/stats.Median() returns value and error | ||
func Median(L *lua.LState) int { | ||
data, err := getFloatSliceFromTable(L, 1) | ||
if err != nil { | ||
L.Push(lua.LNil) | ||
L.Push(lua.LString(err.Error())) | ||
return 2 | ||
} | ||
result, err := gostats.Median(data) | ||
if err != nil { | ||
L.Push(lua.LNil) | ||
L.Push(lua.LString(err.Error())) | ||
return 2 | ||
} | ||
L.Push(lua.LNumber(result)) | ||
return 1 | ||
} | ||
|
||
// Percentile lua stats.median(table, percentile): port of go montanaflynn/stats.Percentile() returns value and error | ||
func Percentile(L *lua.LState) int { | ||
data, err := getFloatSliceFromTable(L, 1) | ||
if err != nil { | ||
L.Push(lua.LNil) | ||
L.Push(lua.LString(err.Error())) | ||
return 2 | ||
} | ||
percentile := L.CheckNumber(2) | ||
result, err := gostats.Percentile(data, float64(percentile)) | ||
if err != nil { | ||
L.Push(lua.LNil) | ||
L.Push(lua.LString(err.Error())) | ||
return 2 | ||
} | ||
L.Push(lua.LNumber(result)) | ||
return 1 | ||
} | ||
|
||
// StandardDeviation lua stats.median(table, percentile): port of go montanaflynn/stats.StandardDeviation() returns value and error | ||
func StandardDeviation(L *lua.LState) int { | ||
data, err := getFloatSliceFromTable(L, 1) | ||
if err != nil { | ||
L.Push(lua.LNil) | ||
L.Push(lua.LString(err.Error())) | ||
return 2 | ||
} | ||
result, err := gostats.StandardDeviation(data) | ||
if err != nil { | ||
L.Push(lua.LNil) | ||
L.Push(lua.LString(err.Error())) | ||
return 2 | ||
} | ||
L.Push(lua.LNumber(result)) | ||
return 1 | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package stats | ||
|
||
import ( | ||
"testing" | ||
|
||
lua "github.com/yuin/gopher-lua" | ||
) | ||
|
||
func TestApi(t *testing.T) { | ||
state := lua.NewState() | ||
Preload(state) | ||
if err := state.DoFile("./test/test_api.lua"); err != nil { | ||
t.Fatalf("execute test: %s\n", err.Error()) | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package stats | ||
|
||
import ( | ||
lua "github.com/yuin/gopher-lua" | ||
) | ||
|
||
// Preload adds stats to the given Lua state's package.preload table. After it | ||
// has been preloaded, it can be loaded using require: | ||
// | ||
// local stats = require("stats") | ||
func Preload(L *lua.LState) { | ||
L.PreloadModule("stats", Loader) | ||
} | ||
|
||
// Loader is the module loader function. | ||
func Loader(L *lua.LState) int { | ||
t := L.NewTable() | ||
L.SetFuncs(t, api) | ||
L.Push(t) | ||
return 1 | ||
} | ||
|
||
var api = map[string]lua.LGFunction{ | ||
"median": Median, | ||
"percentile": Percentile, | ||
"standard_deviation": StandardDeviation, | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
local stats = require("stats") | ||
|
||
local result, err = stats.median({0,0,10}) | ||
if err then error(err) end | ||
if not(result == 0) then error("median get: "..tostring(result)) end | ||
|
||
local result, err = stats.percentile({0,0,10}, 100) | ||
if err then error(err) end | ||
if not(result == 10) then error("percentile get: "..tostring(result)) end | ||
|
||
local result, err = stats.percentile({0,0,10}, 60) | ||
if err then error(err) end | ||
if not(result == 0) then error("percentile get: "..tostring(result)) end | ||
|
||
local result, err = stats.standard_deviation({1,1,1,1}) | ||
if err then error(err) end | ||
if not(result == 0.5) then error("standard_deviation get: "..tostring(result)) end |