github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/arrays/remove_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 // REMOVE_NTH returns a new array without an element by a given position. 12 // @param {Any[]} array - Source array. 13 // @param {Int} position - Target element position. 14 // @return {Any[]} - A new array without an element by a given position. 15 func RemoveNth(_ context.Context, args ...core.Value) (core.Value, error) { 16 err := core.ValidateArgs(args, 2, 2) 17 18 if err != nil { 19 return values.None, err 20 } 21 22 err = core.ValidateType(args[0], types.Array) 23 24 if err != nil { 25 return values.None, err 26 } 27 28 err = core.ValidateType(args[1], types.Int) 29 30 if err != nil { 31 return values.None, err 32 } 33 34 arr := args[0].(*values.Array) 35 index := int(args[1].(values.Int)) 36 result := values.NewArray(int(arr.Length() - 1)) 37 38 arr.ForEach(func(value core.Value, idx int) bool { 39 if idx != index { 40 result.Push(value) 41 } 42 43 return true 44 }) 45 46 return result, nil 47 }