1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-07-03 00:46:51 +02:00
Files
ferret/pkg/stdlib/arrays/remove_values.go
Tim Voronov ec2d6a659b Feature/#9 array functions ()
*  Added 'APPEND' function

*  Added 'FIRST' function

*  Added 'FLATTEN' function

*  Added 'INTERSECTION' function

*  Added 'LAST' function

*  Added 'MINUS' function

*  Added 'NTH' function

*  Added 'OUTERSECTION' function

*  Added 'POP' function

*  Added 'POSITION' function

*  Added 'PUSH' function

* Fixed nil pointer exception in value parser

*  Added 'REMOVE_NTH' function

*  Added 'REMOVE_VALUE' function

*  Added 'REMOVE_VALUES' function

*  Added 'REVERSE' function

*  Added 'SHIFT' function

*  Added 'SLICE' function

* Removed meme

*  Added 'SORTED' function

*  Added SORTED_UNIQUE function

*  Added 'UNION' function

*  Added 'UNION_DISTINCT' function

*  Added 'UNIQUE' function

*  Added 'UNSHIFT' function

*  Made more strict optional arg validation

*  Fixed linting errors
2018-10-05 21:27:34 -04:00

60 lines
1.2 KiB
Go

package arrays
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
/*
* Returns a new array with removed all occurrences of values in a given array.
* @param array (Array) - Source array.
* @param values (Array) - Target values.
* @returns (Array) - A new array with removed all occurrences of values in a given array.
*/
func RemoveValues(_ 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.ArrayType)
if err != nil {
return values.None, err
}
arr := args[0].(*values.Array)
vals := args[1].(*values.Array)
result := values.NewArray(int(arr.Length()))
lookupTable := make(map[uint64]bool)
vals.ForEach(func(value core.Value, idx int) bool {
lookupTable[value.Hash()] = true
return true
})
arr.ForEach(func(value core.Value, idx int) bool {
h := value.Hash()
_, exists := lookupTable[h]
if !exists {
result.Push(value)
}
return true
})
return result, nil
}