generic.scm 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. (module (arguile base generic)
  2. #:export (+ * length))
  3. (use (arguile guile)
  4. (arguile base fn)
  5. (arguile base type))
  6. (def + args
  7. (cond ((null? args) 0)
  8. ((one-of `(,str? ,chr?) (car args))
  9. (apply str-join (map (\\ -> str _) args)))
  10. ((sym? (car args))
  11. (apply sym-join (map (\\ -> sym _) args)))
  12. (else (apply _+ args))))
  13. ;;; TODO: Add cartesian product for data
  14. (def * args
  15. (cond ((null? args) 0)
  16. ((one-of `(,str? ,chr?) (car args))
  17. (apply str-join
  18. (map (fn (val) (-> str (car args)))
  19. (iota (apply _* (cdr args))))))
  20. ((sym? (car args))
  21. (apply sym-join
  22. (map (fn (val) (-> sym (car args)))
  23. (iota (apply _* (cdr args))))))
  24. (else (apply _* args))))
  25. (def length (x)
  26. (cond ((lst? (_length x)))
  27. ((str? x) (str-len x))
  28. ((hash-table? x) (hash-count (const #t) x))
  29. ((vector? x) (vector-length x))
  30. (else (length x))))
  31. (def one-of (tests val)
  32. (if (null? tests) #f
  33. (or ((car tests) val)
  34. (one-of (cdr tests) val))))