From a326ffbb1e2610ecfbb22c4fc0b583b0a6f7d5e8 Mon Sep 17 00:00:00 2001 From: bui Date: Thu, 20 Jul 2023 17:30:58 +0200 Subject: [PATCH] add distinct --- pkg/exprhelpers/expr_lib.go | 10 ++++++++++ pkg/exprhelpers/helpers.go | 27 +++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/pkg/exprhelpers/expr_lib.go b/pkg/exprhelpers/expr_lib.go index 7880233a3..1d29539cb 100644 --- a/pkg/exprhelpers/expr_lib.go +++ b/pkg/exprhelpers/expr_lib.go @@ -25,6 +25,16 @@ var exprFuncs = []exprCustomFunc{ function: Flatten, signature: []interface{}{}, }, + { + name: "Distinct", + function: Distinct, + signature: []interface{}{}, + }, + { + name: "FlattenDistinct", + function: FlattenDistinct, + signature: []interface{}{}, + }, { name: "Distance", function: Distance, diff --git a/pkg/exprhelpers/helpers.go b/pkg/exprhelpers/helpers.go index 4122aad84..2309d47b1 100644 --- a/pkg/exprhelpers/helpers.go +++ b/pkg/exprhelpers/helpers.go @@ -173,6 +173,33 @@ func FileInit(fileFolder string, filename string, fileType string) error { // Expr helpers +func Distinct(params ...any) (any, error) { + + if rt := reflect.TypeOf(params[0]).Kind(); rt != reflect.Slice && rt != reflect.Array { + return nil, nil + } + array := params[0].([]interface{}) + if array == nil { + return nil, nil + } + + var exists map[any]bool = make(map[any]bool) + var ret []interface{} = make([]interface{}, 0) + + for _, val := range array { + if _, ok := exists[val]; !ok { + exists[val] = true + ret = append(ret, val) + } + } + return ret, nil + +} + +func FlattenDistinct(params ...any) (any, error) { + return Distinct(flatten(nil, reflect.ValueOf(params))) +} + func Flatten(params ...any) (any, error) { return flatten(nil, reflect.ValueOf(params)), nil }