Skip to content

Commit e3bfec7

Browse files
committed
Remove countBy
1 parent 3b2df5a commit e3bfec7

File tree

9 files changed

+15
-84
lines changed

9 files changed

+15
-84
lines changed

bench_test.go

-14
Original file line numberDiff line numberDiff line change
@@ -513,20 +513,6 @@ func Benchmark_groupBy(b *testing.B) {
513513
require.Equal(b, 6, out.([]any)[0])
514514
}
515515

516-
func Benchmark_countBy(b *testing.B) {
517-
program, err := expr.Compile(`countBy(1..100, # % 7)[6]`)
518-
require.NoError(b, err)
519-
520-
var out any
521-
b.ResetTimer()
522-
for n := 0; n < b.N; n++ {
523-
out, _ = vm.Run(program, nil)
524-
}
525-
b.StopTimer()
526-
527-
require.Equal(b, 14, out.(int))
528-
}
529-
530516
func Benchmark_reduce(b *testing.B) {
531517
program, err := expr.Compile(`reduce(1..100, # + #acc)`)
532518
require.NoError(b, err)

builtin/builtin.go

+15-20
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,6 @@ var Builtins = []*ast.Function{
5858
Predicate: true,
5959
Types: types(new(func([]any, func(any) any) []any)),
6060
},
61-
{
62-
Name: "count",
63-
Predicate: true,
64-
Types: types(new(func([]any, func(any) bool) int)),
65-
},
6661
{
6762
Name: "find",
6863
Predicate: true,
@@ -83,6 +78,21 @@ var Builtins = []*ast.Function{
8378
Predicate: true,
8479
Types: types(new(func([]any, func(any) bool) int)),
8580
},
81+
{
82+
Name: "count",
83+
Predicate: true,
84+
Types: types(new(func([]any, func(any) bool) int)),
85+
},
86+
{
87+
Name: "groupBy",
88+
Predicate: true,
89+
Types: types(new(func([]any, func(any) any) map[any][]any)),
90+
},
91+
{
92+
Name: "reduce",
93+
Predicate: true,
94+
Types: types(new(func([]any, func(any, any) any, any) any)),
95+
},
8696
{
8797
Name: "len",
8898
Fast: Len,
@@ -920,19 +930,4 @@ var Builtins = []*ast.Function{
920930
return arrayType, nil
921931
},
922932
},
923-
{
924-
Name: "groupBy",
925-
Predicate: true,
926-
Types: types(new(func([]any, func(any) any) map[any][]any)),
927-
},
928-
{
929-
Name: "countBy",
930-
Predicate: true,
931-
Types: types(new(func([]any, func(any) any) map[any]int)),
932-
},
933-
{
934-
Name: "reduce",
935-
Predicate: true,
936-
Types: types(new(func([]any, func(any, any) any, any) any)),
937-
},
938933
}

builtin/builtin_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ func TestBuiltin(t *testing.T) {
118118
{`groupBy(1..3, # > 1)[true]`, []any{2, 3}},
119119
{`groupBy(1..3, # > 1 ? nil : "")[nil]`, []any{2, 3}},
120120
{`groupBy(ArrayOfFoo, .Value).a`, []any{mock.Foo{Value: "a"}}},
121-
{`countBy(1..9, # % 2)`, map[any]int{0: 4, 1: 5}},
122121
{`reduce(1..9, # + #acc, 0)`, 45},
123122
{`reduce(1..9, # + #acc)`, 45},
124123
{`reduce([.5, 1.5, 2.5], # + #acc, 0)`, 4.5},

checker/checker.go

-18
Original file line numberDiff line numberDiff line change
@@ -737,24 +737,6 @@ func (v *checker) BuiltinNode(node *ast.BuiltinNode) (reflect.Type, info) {
737737
}
738738
return v.error(node.Arguments[1], "predicate should has one input and one output param")
739739

740-
case "countBy":
741-
collection, _ := v.visit(node.Arguments[0])
742-
if !isArray(collection) && !isAny(collection) {
743-
return v.error(node.Arguments[0], "builtin %v takes only array (got %v)", node.Name, collection)
744-
}
745-
746-
v.begin(collection)
747-
closure, _ := v.visit(node.Arguments[1])
748-
v.end()
749-
750-
if isFunc(closure) &&
751-
closure.NumOut() == 1 &&
752-
closure.NumIn() == 1 && isAny(closure.In(0)) {
753-
754-
return reflect.TypeOf(map[any]int{}), info{}
755-
}
756-
return v.error(node.Arguments[1], "predicate should has one input and one output param")
757-
758740
case "reduce":
759741
collection, _ := v.visit(node.Arguments[0])
760742
if !isArray(collection) && !isAny(collection) {

compiler/compiler.go

-11
Original file line numberDiff line numberDiff line change
@@ -825,17 +825,6 @@ func (c *compiler) BuiltinNode(node *ast.BuiltinNode) {
825825
c.emit(OpEnd)
826826
return
827827

828-
case "countBy":
829-
c.compile(node.Arguments[0])
830-
c.emit(OpBegin)
831-
c.emitLoop(func() {
832-
c.compile(node.Arguments[1])
833-
c.emit(OpCountBy)
834-
})
835-
c.emit(OpGetCountBy)
836-
c.emit(OpEnd)
837-
return
838-
839828
case "reduce":
840829
c.compile(node.Arguments[0])
841830
c.emit(OpBegin)

parser/parser.go

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ var predicates = map[string]struct {
2929
"findLast": {2},
3030
"findLastIndex": {2},
3131
"groupBy": {2},
32-
"countBy": {2},
3332
"reduce": {3},
3433
}
3534

vm/opcodes.go

-2
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,10 @@ const (
7373
OpGetCount
7474
OpGetLen
7575
OpGetGroupBy
76-
OpGetCountBy
7776
OpGetAcc
7877
OpPointer
7978
OpThrow
8079
OpGroupBy
81-
OpCountBy
8280
OpSetAcc
8381
OpBegin
8482
OpEnd // This opcode must be at the end of this list.

vm/program.go

-6
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,6 @@ func (program *Program) Opcodes(w io.Writer) {
292292
case OpGetGroupBy:
293293
code("OpGetGroupBy")
294294

295-
case OpGetCountBy:
296-
code("OpGetCountBy")
297-
298295
case OpGetAcc:
299296
code("OpGetAcc")
300297

@@ -307,9 +304,6 @@ func (program *Program) Opcodes(w io.Writer) {
307304
case OpGroupBy:
308305
code("OpGroupBy")
309306

310-
case OpCountBy:
311-
code("OpCountBy")
312-
313307
case OpSetAcc:
314308
code("OpSetAcc")
315309

vm/vm.go

-11
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ type Scope struct {
4444
Len int
4545
Count int
4646
GroupBy map[any][]any
47-
CountBy map[any]int
4847
Acc any
4948
}
5049

@@ -467,9 +466,6 @@ func (vm *VM) Run(program *Program, env any) (_ any, err error) {
467466
case OpGetGroupBy:
468467
vm.push(vm.Scope().GroupBy)
469468

470-
case OpGetCountBy:
471-
vm.push(vm.Scope().CountBy)
472-
473469
case OpGetAcc:
474470
vm.push(vm.Scope().Acc)
475471

@@ -492,13 +488,6 @@ func (vm *VM) Run(program *Program, env any) (_ any, err error) {
492488
key := vm.pop()
493489
scope.GroupBy[key] = append(scope.GroupBy[key], it)
494490

495-
case OpCountBy:
496-
scope := vm.Scope()
497-
if scope.CountBy == nil {
498-
scope.CountBy = make(map[any]int)
499-
}
500-
scope.CountBy[vm.pop()]++
501-
502491
case OpBegin:
503492
a := vm.pop()
504493
array := reflect.ValueOf(a)

0 commit comments

Comments
 (0)