mirror of
https://github.com/MontFerret/ferret.git
synced 2025-07-15 01:25:00 +02:00
Feature/#9 array functions (#57)
* #9 Added 'APPEND' function * #9 Added 'FIRST' function * #9 Added 'FLATTEN' function * #9 Added 'INTERSECTION' function * #9 Added 'LAST' function * #9 Added 'MINUS' function * #9 Added 'NTH' function * #9 Added 'OUTERSECTION' function * #9 Added 'POP' function * #9 Added 'POSITION' function * #9 Added 'PUSH' function * Fixed nil pointer exception in value parser * #9 Added 'REMOVE_NTH' function * #9 Added 'REMOVE_VALUE' function * #9 Added 'REMOVE_VALUES' function * #9 Added 'REVERSE' function * #9 Added 'SHIFT' function * #9 Added 'SLICE' function * Removed meme * #9 Added 'SORTED' function * #9 Added SORTED_UNIQUE function * #9 Added 'UNION' function * #9 Added 'UNION_DISTINCT' function * #9 Added 'UNIQUE' function * #9 Added 'UNSHIFT' function * #9 Made more strict optional arg validation * #9 Fixed linting errors
This commit is contained in:
40
pkg/stdlib/arrays/nth.go
Normal file
40
pkg/stdlib/arrays/nth.go
Normal file
@ -0,0 +1,40 @@
|
||||
package arrays
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/MontFerret/ferret/pkg/runtime/core"
|
||||
"github.com/MontFerret/ferret/pkg/runtime/values"
|
||||
)
|
||||
|
||||
/*
|
||||
* Returns the element of an array at a given position.
|
||||
* It is the same as anyArray[position] for positive positions, but does not support negative positions.
|
||||
* @param array (Array) - An array with elements of arbitrary type.
|
||||
* @param index (Int) - Position of desired element in array, positions start at 0.
|
||||
* @returns (Value) - The array element at the given position.
|
||||
* If position is negative or beyond the upper bound of the array, then NONE will be returned.
|
||||
*/
|
||||
func Nth(_ context.Context, args ...core.Value) (core.Value, error) {
|
||||
err := core.ValidateArgs(args, 2, 2)
|
||||
|
||||
if err != nil {
|
||||
return values.None, err
|
||||
}
|
||||
|
||||
err = core.ValidateType(args[0], core.ArrayType)
|
||||
|
||||
if err != nil {
|
||||
return values.None, err
|
||||
}
|
||||
|
||||
err = core.ValidateType(args[1], core.IntType)
|
||||
|
||||
if err != nil {
|
||||
return values.None, err
|
||||
}
|
||||
|
||||
arr := args[0].(*values.Array)
|
||||
idx := args[1].(values.Int)
|
||||
|
||||
return arr.Get(idx), nil
|
||||
}
|
Reference in New Issue
Block a user