1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-16 11:37:36 +02:00
ferret/pkg/stdlib/arrays/remove_values.go
2018-10-14 13:06:27 -04:00

59 lines
1.2 KiB
Go

package arrays
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
// RemoveValues 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
}