Skip to content

Commit

Permalink
Remove countBy
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Aug 30, 2023
1 parent 3b2df5a commit e3bfec7
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 84 deletions.
14 changes: 0 additions & 14 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
35 changes: 15 additions & 20 deletions builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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 change: 0 additions & 1 deletion builtin/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
18 changes: 0 additions & 18 deletions checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 0 additions & 11 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ var predicates = map[string]struct {
"findLast": {2},
"findLastIndex": {2},
"groupBy": {2},
"countBy": {2},
"reduce": {3},
}

Expand Down
2 changes: 0 additions & 2 deletions vm/opcodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 0 additions & 6 deletions vm/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,6 @@ func (program *Program) Opcodes(w io.Writer) {
case OpGetGroupBy:
code("OpGetGroupBy")

case OpGetCountBy:
code("OpGetCountBy")

case OpGetAcc:
code("OpGetAcc")

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

case OpCountBy:
code("OpCountBy")

case OpSetAcc:
code("OpSetAcc")

Expand Down
11 changes: 0 additions & 11 deletions vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ type Scope struct {
Len int
Count int
GroupBy map[any][]any
CountBy map[any]int
Acc any
}

Expand Down Expand Up @@ -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)

Expand All @@ -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)
Expand Down

0 comments on commit e3bfec7

Please sign in to comment.