Skip to content

Commit

Permalink
Implementation of sort builtin REPL function
Browse files Browse the repository at this point in the history
- Fixes #465
  • Loading branch information
BenderScript authored and timothyhinrichs committed Jan 8, 2018
1 parent 8a25538 commit 2233d13
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 0 deletions.
21 changes: 21 additions & 0 deletions ast/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ var DefaultBuiltins = [...]*Builtin{

// Graphs
WalkBuiltin,

//Sort
Sort,
}

// BuiltinMap provides a convenient mapping of built-in names to
Expand Down Expand Up @@ -653,6 +656,24 @@ var WalkBuiltin = &Builtin{
),
}

/**
* Sorting
*/

// Sort a collection
// Trying new built-in
var Sort = &Builtin{
Name: "sort",
Decl: types.NewFunction(
types.Args(
types.NewAny(
types.NewArray(nil, types.A),
),
),
types.A,
),
}

/**
* Deprecated built-ins.
*/
Expand Down
6 changes: 6 additions & 0 deletions ast/term.go
Original file line number Diff line number Diff line change
Expand Up @@ -1294,6 +1294,12 @@ func Item(key, value *Term) [2]*Term {
return [2]*Term{key, value}
}

// SortCollection sorts an array using custom function
func SortCollection(a []*Term) int {
sort.Sort(termSlice(a))
return 0
}

// Compare compares obj to other, return <0, 0, or >0 if it is less than, equal to,
// or greater than other.
func (obj *object) Compare(other Value) int {
Expand Down
Binary file added debug
Binary file not shown.
Binary file added opa
Binary file not shown.
19 changes: 19 additions & 0 deletions topdown/aggregates.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,29 @@ func builtinMin(a ast.Value) (ast.Value, error) {
return nil, builtins.NewOperandTypeErr(1, a, ast.SetTypeName, ast.ArrayTypeName)
}

func builtinSort(a ast.Value) (ast.Value, error) {
switch a := a.(type) {
case ast.Array:
if len(a) == 0 {
return nil, BuiltinEmpty{}
}
ast.SortCollection(a)
return a, nil
case ast.Set:
if a.Len() == 0 {
return nil, BuiltinEmpty{}
}
return a, nil
}

return nil, builtins.NewOperandTypeErr(1, a, ast.SetTypeName, ast.ArrayTypeName)
}

func init() {
RegisterFunctionalBuiltin1(ast.Count.Name, builtinCount)
RegisterFunctionalBuiltin1(ast.Sum.Name, builtinSum)
RegisterFunctionalBuiltin1(ast.Product.Name, builtinProduct)
RegisterFunctionalBuiltin1(ast.Max.Name, builtinMax)
RegisterFunctionalBuiltin1(ast.Min.Name, builtinMin)
RegisterFunctionalBuiltin1(ast.Sort.Name, builtinSort)
}
1 change: 1 addition & 0 deletions topdown/topdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,7 @@ func TestTopDownAggregates(t *testing.T) {
{"min virtual set", []string{`p = x { min(q, x) }`, `q[x] { a[_] = x }`}, "1"},
{"reduce ref dest", []string{`p = true { max([1, 2, 3, 4], a[3]) }`}, "true"},
{"reduce ref dest (2)", []string{`p = true { not max([1, 2, 3, 4, 5], a[3]) }`}, "true"},
{"sort", []string{`p = x { sort([4, 3, 2, 1], x) }`}, "[1 ,2, 3, 4]"},
}

data := loadSmallTestData()
Expand Down

0 comments on commit 2233d13

Please sign in to comment.