mirror of
				https://github.com/chaisql/chai.git
				synced 2025-10-31 19:02:48 +08:00 
			
		
		
		
	Add random function (#514)
This commit is contained in:
		| @@ -18,6 +18,7 @@ var builtinDocs = functionDocs{ | |||||||
| 	"typeof":   "The typeof function returns the type of arg1.", | 	"typeof":   "The typeof function returns the type of arg1.", | ||||||
| 	"len":      "The len function returns length of the arg1 expression if arg1 evals to string, array or document, either returns NULL.", | 	"len":      "The len function returns length of the arg1 expression if arg1 evals to string, array or document, either returns NULL.", | ||||||
| 	"coalesce": "The coalesce function returns the first non-null argument. NULL is returned if all arguments are null.", | 	"coalesce": "The coalesce function returns the first non-null argument. NULL is returned if all arguments are null.", | ||||||
|  | 	"random":   "The random function returns a random number between math.MinInt64 and math.MaxInt64.", | ||||||
| } | } | ||||||
|  |  | ||||||
| var mathDocs = functionDocs{ | var mathDocs = functionDocs{ | ||||||
|   | |||||||
| @@ -3,6 +3,7 @@ package functions | |||||||
| import ( | import ( | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"math" | 	"math" | ||||||
|  | 	"math/rand" | ||||||
|  |  | ||||||
| 	"github.com/genjidb/genji/document" | 	"github.com/genjidb/genji/document" | ||||||
| 	"github.com/genjidb/genji/types" | 	"github.com/genjidb/genji/types" | ||||||
| @@ -22,6 +23,7 @@ var mathFunctions = Definitions{ | |||||||
| 	"asinh":  asinh, | 	"asinh":  asinh, | ||||||
| 	"atan":   atan, | 	"atan":   atan, | ||||||
| 	"atan2":  atan2, | 	"atan2":  atan2, | ||||||
|  | 	"random": random, | ||||||
| } | } | ||||||
|  |  | ||||||
| var floor = &ScalarDefinition{ | var floor = &ScalarDefinition{ | ||||||
| @@ -164,3 +166,12 @@ var atan2 = &ScalarDefinition{ | |||||||
| 		return types.NewDoubleValue(res), nil | 		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 | ||||||
|  | 	}, | ||||||
|  | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 agonist
					agonist