github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/math/range.go (about) 1 package math 2 3 import ( 4 "context" 5 6 "github.com/MontFerret/ferret/pkg/runtime/core" 7 "github.com/MontFerret/ferret/pkg/runtime/values" 8 "github.com/MontFerret/ferret/pkg/runtime/values/types" 9 ) 10 11 // RANGE returns an array of numbers in the specified range, optionally with increments other than 1. 12 // @param {Int | Float} start - The value to start the range at (inclusive). 13 // @param {Int | Float} end - The value to end the range with (inclusive). 14 // @param {Int | Float} [step=1.0] - How much to increment in every step. 15 // @return {Int[] | Float[]} - Array of numbers in the specified range, optionally with increments other than 1. 16 func Range(_ context.Context, args ...core.Value) (core.Value, error) { 17 err := core.ValidateArgs(args, 2, 3) 18 19 if err != nil { 20 return values.None, err 21 } 22 23 err = core.ValidateType(args[0], types.Int, types.Float) 24 25 if err != nil { 26 return values.None, err 27 } 28 29 err = core.ValidateType(args[1], types.Int, types.Float) 30 31 if err != nil { 32 return values.None, err 33 } 34 35 var step float64 = 1 36 37 if len(args) > 2 { 38 err = core.ValidateType(args[2], types.Int, types.Float) 39 40 if err != nil { 41 return values.None, err 42 } 43 44 step = toFloat(args[2]) 45 } 46 47 start := toFloat(args[0]) 48 end := toFloat(args[1]) 49 50 arr := values.NewArray(int(end)) 51 52 for i := start; i <= end; i += step { 53 arr.Push(values.NewFloat(i)) 54 } 55 56 return arr, nil 57 }