@@ -524,6 +524,42 @@ var Builtins = []*ast.Function{
524
524
return runtime .Fetch (args [0 ], args [1 ]), nil
525
525
},
526
526
},
527
+ {
528
+ Name : "take" ,
529
+ Func : func (args ... any ) (any , error ) {
530
+ if len (args ) != 2 {
531
+ return nil , fmt .Errorf ("invalid number of arguments (expected 2, got %d)" , len (args ))
532
+ }
533
+ v := reflect .ValueOf (args [0 ])
534
+ if v .Kind () != reflect .Slice && v .Kind () != reflect .Array {
535
+ return nil , fmt .Errorf ("cannot take from %s" , v .Kind ())
536
+ }
537
+ n := reflect .ValueOf (args [1 ])
538
+ if ! n .CanInt () {
539
+ return nil , fmt .Errorf ("cannot take %s elements" , n .Kind ())
540
+ }
541
+ if n .Int () > int64 (v .Len ()) {
542
+ return args [0 ], nil
543
+ }
544
+ return v .Slice (0 , int (n .Int ())).Interface (), nil
545
+ },
546
+ Validate : func (args []reflect.Type ) (reflect.Type , error ) {
547
+ if len (args ) != 2 {
548
+ return anyType , fmt .Errorf ("invalid number of arguments (expected 2, got %d)" , len (args ))
549
+ }
550
+ switch kind (args [0 ]) {
551
+ case reflect .Interface , reflect .Slice , reflect .Array :
552
+ default :
553
+ return anyType , fmt .Errorf ("cannot take from %s" , args [0 ])
554
+ }
555
+ switch kind (args [1 ]) {
556
+ case reflect .Interface , reflect .Int , reflect .Int8 , reflect .Int16 , reflect .Int32 , reflect .Int64 :
557
+ default :
558
+ return anyType , fmt .Errorf ("cannot take %s elements" , args [1 ])
559
+ }
560
+ return args [0 ], nil
561
+ },
562
+ },
527
563
{
528
564
Name : "keys" ,
529
565
Func : func (args ... any ) (any , error ) {
0 commit comments