1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-03-05 15:16:07 +02:00
ferret/pkg/stdlib/arrays/sorted_unique.go

63 lines
1.4 KiB
Go
Raw Normal View History

package arrays
import (
"context"
2018-10-14 20:06:27 +03:00
"github.com/MontFerret/ferret/pkg/runtime/collections"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
2018-10-14 20:06:27 +03:00
// SortedUnique sorts all elements in anyArray.
// The function will use the default comparison order for FQL value types.
// Additionally, the values in the result array will be made unique
// @param array (Array) - Target array.
// @returns (Array) - Sorted array.
func SortedUnique(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 1)
if err != nil {
return values.None, err
}
err = core.ValidateType(args[0], core.ArrayType)
if err != nil {
return values.None, err
}
arr := args[0].(*values.Array)
if arr.Length() == 0 {
return values.NewArray(0), nil
}
sorter, err := collections.NewSorter(func(first collections.DataSet, second collections.DataSet) (int, error) {
return first.Get(collections.DefaultValueVar).Compare(second.Get(collections.DefaultValueVar)), nil
}, collections.SortDirectionAsc)
if err != nil {
return values.None, err
}
uniqIterator, err := collections.NewUniqueIterator(
collections.NewDefaultIndexedIterator(arr),
collections.DefaultValueVar,
)
if err != nil {
return values.None, err
}
iterator, err := collections.NewSortIterator(
uniqIterator,
sorter,
)
if err != nil {
return values.None, err
}
return toArray(iterator)
}