Remove countBy

This commit is contained in:
Anton Medvedev
2023-08-30 14:55:05 +02:00
parent 714dd08022
commit 6d8d2390cf
9 changed files with 15 additions and 84 deletions
-14
View File
@@ -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)
+15 -20
View File
@@ -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)),
},
}
-1
View File
@@ -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},
-18
View File
@@ -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) {
-11
View File
@@ -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)
-1
View File
@@ -29,7 +29,6 @@ var predicates = map[string]struct {
"findLast": {2},
"findLastIndex": {2},
"groupBy": {2},
"countBy": {2},
"reduce": {3},
}
-2
View File
@@ -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.
-6
View File
@@ -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")
-11
View File
@@ -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)