From e3bfec77efb0953146b53a335752c16825d6aac5 Mon Sep 17 00:00:00 2001 From: Anton Medvedev Date: Wed, 30 Aug 2023 14:55:05 +0200 Subject: [PATCH] Remove countBy --- bench_test.go | 14 -------------- builtin/builtin.go | 35 +++++++++++++++-------------------- builtin/builtin_test.go | 1 - checker/checker.go | 18 ------------------ compiler/compiler.go | 11 ----------- parser/parser.go | 1 - vm/opcodes.go | 2 -- vm/program.go | 6 ------ vm/vm.go | 11 ----------- 9 files changed, 15 insertions(+), 84 deletions(-) diff --git a/bench_test.go b/bench_test.go index 0aea490b3..8d1a272c3 100644 --- a/bench_test.go +++ b/bench_test.go @@ -513,20 +513,6 @@ func Benchmark_groupBy(b *testing.B) { require.Equal(b, 6, out.([]any)[0]) } -func Benchmark_countBy(b *testing.B) { - program, err := expr.Compile(`countBy(1..100, # % 7)[6]`) - require.NoError(b, err) - - var out any - b.ResetTimer() - for n := 0; n < b.N; n++ { - out, _ = vm.Run(program, nil) - } - b.StopTimer() - - require.Equal(b, 14, out.(int)) -} - func Benchmark_reduce(b *testing.B) { program, err := expr.Compile(`reduce(1..100, # + #acc)`) require.NoError(b, err) diff --git a/builtin/builtin.go b/builtin/builtin.go index f33c6059c..d7248a867 100644 --- a/builtin/builtin.go +++ b/builtin/builtin.go @@ -58,11 +58,6 @@ var Builtins = []*ast.Function{ Predicate: true, Types: types(new(func([]any, func(any) any) []any)), }, - { - Name: "count", - Predicate: true, - Types: types(new(func([]any, func(any) bool) int)), - }, { Name: "find", Predicate: true, @@ -83,6 +78,21 @@ var Builtins = []*ast.Function{ Predicate: true, Types: types(new(func([]any, func(any) bool) int)), }, + { + Name: "count", + Predicate: true, + Types: types(new(func([]any, func(any) bool) int)), + }, + { + Name: "groupBy", + Predicate: true, + Types: types(new(func([]any, func(any) any) map[any][]any)), + }, + { + Name: "reduce", + Predicate: true, + Types: types(new(func([]any, func(any, any) any, any) any)), + }, { Name: "len", Fast: Len, @@ -920,19 +930,4 @@ var Builtins = []*ast.Function{ return arrayType, nil }, }, - { - Name: "groupBy", - Predicate: true, - Types: types(new(func([]any, func(any) any) map[any][]any)), - }, - { - Name: "countBy", - Predicate: true, - Types: types(new(func([]any, func(any) any) map[any]int)), - }, - { - Name: "reduce", - Predicate: true, - Types: types(new(func([]any, func(any, any) any, any) any)), - }, } diff --git a/builtin/builtin_test.go b/builtin/builtin_test.go index 6b320e72c..62bd12562 100644 --- a/builtin/builtin_test.go +++ b/builtin/builtin_test.go @@ -118,7 +118,6 @@ func TestBuiltin(t *testing.T) { {`groupBy(1..3, # > 1)[true]`, []any{2, 3}}, {`groupBy(1..3, # > 1 ? nil : "")[nil]`, []any{2, 3}}, {`groupBy(ArrayOfFoo, .Value).a`, []any{mock.Foo{Value: "a"}}}, - {`countBy(1..9, # % 2)`, map[any]int{0: 4, 1: 5}}, {`reduce(1..9, # + #acc, 0)`, 45}, {`reduce(1..9, # + #acc)`, 45}, {`reduce([.5, 1.5, 2.5], # + #acc, 0)`, 4.5}, diff --git a/checker/checker.go b/checker/checker.go index 5002b7ca2..287164b64 100644 --- a/checker/checker.go +++ b/checker/checker.go @@ -737,24 +737,6 @@ func (v *checker) BuiltinNode(node *ast.BuiltinNode) (reflect.Type, info) { } return v.error(node.Arguments[1], "predicate should has one input and one output param") - case "countBy": - collection, _ := v.visit(node.Arguments[0]) - if !isArray(collection) && !isAny(collection) { - return v.error(node.Arguments[0], "builtin %v takes only array (got %v)", node.Name, collection) - } - - v.begin(collection) - closure, _ := v.visit(node.Arguments[1]) - v.end() - - if isFunc(closure) && - closure.NumOut() == 1 && - closure.NumIn() == 1 && isAny(closure.In(0)) { - - return reflect.TypeOf(map[any]int{}), info{} - } - return v.error(node.Arguments[1], "predicate should has one input and one output param") - case "reduce": collection, _ := v.visit(node.Arguments[0]) if !isArray(collection) && !isAny(collection) { diff --git a/compiler/compiler.go b/compiler/compiler.go index deb38065b..66ad4f1f2 100644 --- a/compiler/compiler.go +++ b/compiler/compiler.go @@ -825,17 +825,6 @@ func (c *compiler) BuiltinNode(node *ast.BuiltinNode) { c.emit(OpEnd) return - case "countBy": - c.compile(node.Arguments[0]) - c.emit(OpBegin) - c.emitLoop(func() { - c.compile(node.Arguments[1]) - c.emit(OpCountBy) - }) - c.emit(OpGetCountBy) - c.emit(OpEnd) - return - case "reduce": c.compile(node.Arguments[0]) c.emit(OpBegin) diff --git a/parser/parser.go b/parser/parser.go index 9f20a5678..b7a8752d5 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -29,7 +29,6 @@ var predicates = map[string]struct { "findLast": {2}, "findLastIndex": {2}, "groupBy": {2}, - "countBy": {2}, "reduce": {3}, } diff --git a/vm/opcodes.go b/vm/opcodes.go index 5f36fb1a3..1106cd3f0 100644 --- a/vm/opcodes.go +++ b/vm/opcodes.go @@ -73,12 +73,10 @@ const ( OpGetCount OpGetLen OpGetGroupBy - OpGetCountBy OpGetAcc OpPointer OpThrow OpGroupBy - OpCountBy OpSetAcc OpBegin OpEnd // This opcode must be at the end of this list. diff --git a/vm/program.go b/vm/program.go index a81eb3ae2..c45a2bff2 100644 --- a/vm/program.go +++ b/vm/program.go @@ -292,9 +292,6 @@ func (program *Program) Opcodes(w io.Writer) { case OpGetGroupBy: code("OpGetGroupBy") - case OpGetCountBy: - code("OpGetCountBy") - case OpGetAcc: code("OpGetAcc") @@ -307,9 +304,6 @@ func (program *Program) Opcodes(w io.Writer) { case OpGroupBy: code("OpGroupBy") - case OpCountBy: - code("OpCountBy") - case OpSetAcc: code("OpSetAcc") diff --git a/vm/vm.go b/vm/vm.go index 290206ae6..1a62b587a 100644 --- a/vm/vm.go +++ b/vm/vm.go @@ -44,7 +44,6 @@ type Scope struct { Len int Count int GroupBy map[any][]any - CountBy map[any]int Acc any } @@ -467,9 +466,6 @@ func (vm *VM) Run(program *Program, env any) (_ any, err error) { case OpGetGroupBy: vm.push(vm.Scope().GroupBy) - case OpGetCountBy: - vm.push(vm.Scope().CountBy) - case OpGetAcc: vm.push(vm.Scope().Acc) @@ -492,13 +488,6 @@ func (vm *VM) Run(program *Program, env any) (_ any, err error) { key := vm.pop() scope.GroupBy[key] = append(scope.GroupBy[key], it) - case OpCountBy: - scope := vm.Scope() - if scope.CountBy == nil { - scope.CountBy = make(map[any]int) - } - scope.CountBy[vm.pop()]++ - case OpBegin: a := vm.pop() array := reflect.ValueOf(a)