/
builtin_sort.go
41 lines (36 loc) · 1 KB
/
builtin_sort.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package expreduce
import (
"math/big"
"sort"
"github.com/corywalker/expreduce/expreduce/atoms"
"github.com/corywalker/expreduce/pkg/expreduceapi"
)
func getSortDefinitions() (defs []Definition) {
defs = append(defs, Definition{
Name: "Sort",
legacyEvalFn: func(this expreduceapi.ExpressionInterface, es expreduceapi.EvalStateInterface) expreduceapi.Ex {
if len(this.GetParts()) != 2 {
return this
}
exp, ok := this.GetParts()[1].(expreduceapi.ExpressionInterface)
if ok {
sortedExp := exp.DeepCopy().(expreduceapi.ExpressionInterface)
sortedExp.ClearHashes()
sort.Sort(sortedExp)
return sortedExp
}
return this
},
})
defs = append(defs, Definition{
Name: "Order",
legacyEvalFn: func(this expreduceapi.ExpressionInterface, es expreduceapi.EvalStateInterface) expreduceapi.Ex {
if len(this.GetParts()) != 3 {
return this
}
toreturn := atoms.ExOrder(this.GetParts()[1], this.GetParts()[2])
return atoms.NewInteger(big.NewInt(toreturn))
},
})
return
}