github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/arrays/lib.go (about)

     1  package arrays
     2  
     3  import (
     4  	"github.com/MontFerret/ferret/pkg/runtime/core"
     5  	"github.com/MontFerret/ferret/pkg/runtime/values"
     6  )
     7  
     8  func RegisterLib(ns core.Namespace) error {
     9  	return ns.RegisterFunctions(
    10  		core.NewFunctionsFromMap(map[string]core.Function{
    11  			"APPEND":         Append,
    12  			"FIRST":          First,
    13  			"FLATTEN":        Flatten,
    14  			"INTERSECTION":   Intersection,
    15  			"LAST":           Last,
    16  			"MINUS":          Minus,
    17  			"NTH":            Nth,
    18  			"OUTERSECTION":   Outersection,
    19  			"POP":            Pop,
    20  			"POSITION":       Position,
    21  			"PUSH":           Push,
    22  			"REMOVE_NTH":     RemoveNth,
    23  			"REMOVE_VALUE":   RemoveValue,
    24  			"REMOVE_VALUES":  RemoveValues,
    25  			"SHIFT":          Shift,
    26  			"SLICE":          Slice,
    27  			"SORTED":         Sorted,
    28  			"SORTED_UNIQUE":  SortedUnique,
    29  			"UNION":          Union,
    30  			"UNION_DISTINCT": UnionDistinct,
    31  			"UNIQUE":         Unique,
    32  			"UNSHIFT":        Unshift,
    33  		}))
    34  }
    35  
    36  func ToUniqueArray(arr *values.Array) *values.Array {
    37  	hashTable := make(map[uint64]bool)
    38  	result := values.NewArray(int(arr.Length()))
    39  
    40  	arr.ForEach(func(item core.Value, _ int) bool {
    41  		h := item.Hash()
    42  
    43  		_, exists := hashTable[h]
    44  
    45  		if !exists {
    46  			hashTable[h] = true
    47  			result.Push(item)
    48  		}
    49  
    50  		return true
    51  	})
    52  
    53  	return result
    54  }