arrayutils.go 577 B

1234567891011121314151617181920212223242526272829303132
  1. package glisp
  2. import (
  3. "errors"
  4. )
  5. func MapArray(env *Glisp, fun SexpFunction, arr SexpArray) (SexpArray, error) {
  6. result := make([]Sexp, len(arr))
  7. var err error
  8. for i := range arr {
  9. result[i], err = env.Apply(fun, arr[i:i+1])
  10. if err != nil {
  11. return SexpArray(result), err
  12. }
  13. }
  14. return SexpArray(result), nil
  15. }
  16. func ConcatArray(arr SexpArray, expr Sexp) (SexpArray, error) {
  17. var arr2 SexpArray
  18. switch t := expr.(type) {
  19. case SexpArray:
  20. arr2 = t
  21. default:
  22. return arr, errors.New("second argument is not an array")
  23. }
  24. return append(arr, arr2...), nil
  25. }