github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/arrays/nth.go (about) 1 package arrays 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 // NTH returns the element of an array at a given position. 12 // It is the same as anyArray[position] for positive positions, but does not support negative positions. 13 // If position is negative or beyond the upper bound of the array, then NONE will be returned. 14 // @param {Any[]} array - An array with elements of arbitrary type. 15 // @param {Int} index - Position of desired element in array, positions start at 0. 16 // @return {Any} - The array element at the given position. 17 func Nth(_ context.Context, args ...core.Value) (core.Value, error) { 18 err := core.ValidateArgs(args, 2, 2) 19 20 if err != nil { 21 return values.None, err 22 } 23 24 err = core.ValidateType(args[0], types.Array) 25 26 if err != nil { 27 return values.None, err 28 } 29 30 err = core.ValidateType(args[1], types.Int) 31 32 if err != nil { 33 return values.None, err 34 } 35 36 arr := args[0].(*values.Array) 37 idx := args[1].(values.Int) 38 39 return arr.Get(idx), nil 40 }