Add random function (#514)

This commit is contained in:
agonist
2023-11-25 00:26:03 +08:00
committed by GitHub
parent 79af2d01b1
commit 71477da66c
2 changed files with 20 additions and 8 deletions

View File

@@ -3,6 +3,7 @@ package functions
import (
"fmt"
"math"
"math/rand"
"github.com/genjidb/genji/document"
"github.com/genjidb/genji/types"
@@ -14,14 +15,15 @@ func MathFunctions() Definitions {
}
var mathFunctions = Definitions{
"floor": floor,
"abs": abs,
"acos": acos,
"acosh": acosh,
"asin": asin,
"asinh": asinh,
"atan": atan,
"atan2": atan2,
"floor": floor,
"abs": abs,
"acos": acos,
"acosh": acosh,
"asin": asin,
"asinh": asinh,
"atan": atan,
"atan2": atan2,
"random": random,
}
var floor = &ScalarDefinition{
@@ -164,3 +166,12 @@ var atan2 = &ScalarDefinition{
return types.NewDoubleValue(res), nil
},
}
var random = &ScalarDefinition{
name: "random",
arity: 0,
callFn: func(args ...types.Value) (types.Value, error) {
randomNum := rand.Int63()
return types.NewIntegerValue(randomNum), nil
},
}