You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.3KB

  1. ;;; Module storing utility functions for common use.
  2. (module util (curry applied thunk just perhaps compose-symbols mapn)
  3. (import scheme)
  4. (import chicken.base)
  5. (import srfi-13)
  6. (import matchable)
  7. ;; Partial application function for binary functions.
  8. (define (curry fn a)
  9. (lambda (b)
  10. (fn a b)))
  11. ;; Make a function applied, as per (applied +) = (lambda (ln) (+ (car ln) (cdr ln))).
  12. (define (applied fn)
  13. (curry apply fn))
  14. ;; Take a list of expressions and wrap them in a thunk.
  15. (define-syntax thunk
  16. (syntax-rules ()
  17. ((_ exp ...)
  18. (lambda () exp ...))))
  19. ;; Wrap a thunk in a function that discards all its arguments
  20. ;; and returns the result of evaluating the thunk.
  21. (define (just fn)
  22. (lambda args
  23. (fn)))
  24. ;; Haskell Maybe lifting pattern equivalent for booleans,
  25. ;; apply fn to arg if arg is not false.
  26. (define (perhaps fn arg)
  27. (if arg
  28. (fn arg)
  29. arg))
  30. (define (mapn fn ln)
  31. (match ln
  32. (() '())
  33. ((x) (fn x))
  34. (_ (begin
  35. (fn (car ln))
  36. (mapn fn (cdr ln))))))
  37. ;; Compose the argument symbols into a new symbol, joined by '-'.
  38. ;; (compose-symbols 'apple 'pear 'orange) = 'apple-pear-orange
  39. (define (compose-symbols . ln)
  40. (string->symbol (string-concatenate (intersperse (map symbol->string ln) "-")))))