The plus and minus operators now support lists for join and difference respectively

This commit is contained in:
2024-03-30 07:05:22 +01:00
parent f540ec28e8
commit 85fb007a2b
2 changed files with 44 additions and 38 deletions
+28
View File
@@ -6,6 +6,7 @@ package expr
import (
"fmt"
"slices"
)
//-------- plus term
@@ -39,6 +40,23 @@ func evalPlus(ctx exprContext, self *term) (v any, err error) {
rightInt, _ := rightValue.(int64)
v = leftInt + rightInt
}
} else if isList(leftValue) || isList(rightValue) {
var leftList, rightList []any
var ok bool
if leftList, ok = leftValue.([]any); !ok {
leftList = []any{leftValue}
}
if rightList, ok = rightValue.([]any); !ok {
rightList = []any{rightValue}
}
sumList := make([]any, 0, len(leftList)+len(rightList))
for _, item := range leftList {
sumList = append(sumList, item)
}
for _, item := range rightList {
sumList = append(sumList, item)
}
v = sumList
} else {
err = self.errIncompatibleTypes(leftValue, rightValue)
}
@@ -74,6 +92,16 @@ func evalMinus(ctx exprContext, self *term) (v any, err error) {
rightInt, _ := rightValue.(int64)
v = leftInt - rightInt
}
} else if isList(leftValue) && isList(rightValue) {
leftList, _ := leftValue.([]any)
rightList, _ := rightValue.([]any)
diffList := make([]any, 0, len(leftList)-len(rightList))
for _, item := range leftList {
if slices.Index(rightList, item) < 0 {
diffList = append(diffList, item)
}
}
v = diffList
} else {
err = self.errIncompatibleTypes(leftValue, rightValue)
}