@@ -87,6 +87,11 @@ var Builtins = []*Function{
87
87
Predicate : true ,
88
88
Types : types (new (func ([]any , func (any ) any ) map [any ][]any )),
89
89
},
90
+ {
91
+ Name : "sortBy" ,
92
+ Predicate : true ,
93
+ Types : types (new (func ([]any , func (any ) bool , string ) []any )),
94
+ },
90
95
{
91
96
Name : "reduce" ,
92
97
Predicate : true ,
@@ -905,109 +910,65 @@ var Builtins = []*Function{
905
910
},
906
911
{
907
912
Name : "sort" ,
908
- Func : func (args ... any ) (any , error ) {
913
+ Safe : func (args ... any ) (any , uint , error ) {
909
914
if len (args ) != 1 && len (args ) != 2 {
910
- return nil , fmt .Errorf ("invalid number of arguments (expected 1 or 2, got %d)" , len (args ))
915
+ return nil , 0 , fmt .Errorf ("invalid number of arguments (expected 1 or 2, got %d)" , len (args ))
911
916
}
912
917
913
- v := reflect .ValueOf (args [0 ])
914
- if v .Kind () != reflect .Slice && v .Kind () != reflect .Array {
915
- return nil , fmt .Errorf ("cannot sort %s" , v .Kind ())
916
- }
918
+ var array []any
917
919
918
- orderBy := OrderBy {}
919
- if len (args ) == 2 {
920
- dir , err := ascOrDesc (args [1 ])
921
- if err != nil {
922
- return nil , err
920
+ switch in := args [0 ].(type ) {
921
+ case []any :
922
+ array = make ([]any , len (in ))
923
+ copy (array , in )
924
+ case []int :
925
+ array = make ([]any , len (in ))
926
+ for i , v := range in {
927
+ array [i ] = v
928
+ }
929
+ case []float64 :
930
+ array = make ([]any , len (in ))
931
+ for i , v := range in {
932
+ array [i ] = v
933
+ }
934
+ case []string :
935
+ array = make ([]any , len (in ))
936
+ for i , v := range in {
937
+ array [i ] = v
923
938
}
924
- orderBy .Desc = dir
925
939
}
926
940
927
- sortable , err := copyArray (v , orderBy )
928
- if err != nil {
929
- return nil , err
930
- }
931
- sort .Sort (sortable )
932
- return sortable .Array , nil
933
- },
934
- Validate : func (args []reflect.Type ) (reflect.Type , error ) {
935
- if len (args ) != 1 && len (args ) != 2 {
936
- return anyType , fmt .Errorf ("invalid number of arguments (expected 1 or 2, got %d)" , len (args ))
937
- }
938
- switch kind (args [0 ]) {
939
- case reflect .Interface , reflect .Slice , reflect .Array :
940
- default :
941
- return anyType , fmt .Errorf ("cannot sort %s" , args [0 ])
942
- }
941
+ var desc bool
943
942
if len (args ) == 2 {
944
- switch kind (args [1 ]) {
945
- case reflect .String , reflect .Interface :
943
+ switch args [1 ].(string ) {
944
+ case "asc" :
945
+ desc = false
946
+ case "desc" :
947
+ desc = true
946
948
default :
947
- return anyType , fmt .Errorf ("invalid argument for sort ( expected string, got %s) " , args [1 ])
949
+ return nil , 0 , fmt .Errorf ("invalid order %s, expected asc or desc " , args [1 ])
948
950
}
949
951
}
950
- return arrayType , nil
951
- },
952
- },
953
- {
954
- Name : "sortBy" ,
955
- Func : func (args ... any ) (any , error ) {
956
- if len (args ) != 2 && len (args ) != 3 {
957
- return nil , fmt .Errorf ("invalid number of arguments (expected 2 or 3, got %d)" , len (args ))
958
- }
959
952
960
- v := reflect .ValueOf (args [0 ])
961
- if v .Kind () != reflect .Slice && v .Kind () != reflect .Array {
962
- return nil , fmt .Errorf ("cannot sort %s" , v .Kind ())
963
- }
964
-
965
- orderBy := OrderBy {}
966
-
967
- field , ok := args [1 ].(string )
968
- if ! ok {
969
- return nil , fmt .Errorf ("invalid argument for sort (expected string, got %s)" , reflect .TypeOf (args [1 ]))
970
- }
971
- orderBy .Field = field
972
-
973
- if len (args ) == 3 {
974
- dir , err := ascOrDesc (args [2 ])
975
- if err != nil {
976
- return nil , err
977
- }
978
- orderBy .Desc = dir
979
- }
980
-
981
- sortable , err := copyArray (v , orderBy )
982
- if err != nil {
983
- return nil , err
953
+ sortable := & runtime.Sort {
954
+ Desc : desc ,
955
+ Array : array ,
984
956
}
985
957
sort .Sort (sortable )
986
- return sortable .Array , nil
987
- },
988
- Validate : func (args []reflect.Type ) (reflect.Type , error ) {
989
- if len (args ) != 2 && len (args ) != 3 {
990
- return anyType , fmt .Errorf ("invalid number of arguments (expected 2 or 3, got %d)" , len (args ))
991
- }
992
- switch kind (args [0 ]) {
993
- case reflect .Interface , reflect .Slice , reflect .Array :
994
- default :
995
- return anyType , fmt .Errorf ("cannot sort %s" , args [0 ])
996
- }
997
- switch kind (args [1 ]) {
998
- case reflect .String , reflect .Interface :
999
- default :
1000
- return anyType , fmt .Errorf ("invalid argument for sort (expected string, got %s)" , args [1 ])
1001
- }
1002
- if len (args ) == 3 {
1003
- switch kind (args [2 ]) {
1004
- case reflect .String , reflect .Interface :
1005
- default :
1006
- return anyType , fmt .Errorf ("invalid argument for sort (expected string, got %s)" , args [1 ])
1007
- }
1008
- }
1009
- return arrayType , nil
958
+
959
+ return sortable .Array , uint (len (array )), nil
1010
960
},
961
+ Types : types (
962
+ new (func ([]any , string ) []any ),
963
+ new (func ([]int , string ) []any ),
964
+ new (func ([]float64 , string ) []any ),
965
+ new (func ([]string , string ) []any ),
966
+
967
+ new (func ([]any ) []any ),
968
+ new (func ([]float64 ) []any ),
969
+ new (func ([]string ) []any ),
970
+ new (func ([]int ) []any ),
971
+ ),
1011
972
},
1012
973
bitFunc ("bitand" , func (x , y int ) (any , error ) {
1013
974
return x & y , nil
0 commit comments