diff options
author | David Thompson <dthompson2@worcester.edu> | 2014-12-06 11:16:26 -0500 |
---|---|---|
committer | David Thompson <dthompson2@worcester.edu> | 2014-12-06 13:20:49 -0500 |
commit | 11dba13e4d36d57f0eea0524f52b9b4616049ed2 (patch) | |
tree | 2c1fbe2844d70a340663a6c05e761f911a966cdf | |
parent | 19650ce35b164c1af1056043dbf161a1618b7c06 (diff) |
utils: Add chain macro.
* sly/utils.scm (chain, chain*): New syntax.
-rw-r--r-- | sly/utils.scm | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/sly/utils.scm b/sly/utils.scm index 7bf38f7..78c1b0f 100644 --- a/sly/utils.scm +++ b/sly/utils.scm @@ -31,7 +31,8 @@ define-guardian memoize forever - trampoline)) + trampoline + chain* chain)) (define (any-equal? elem . args) "Return #t if ELEM equals any of the elements in the list ARGS." @@ -74,3 +75,17 @@ same thread that is running the game loop." (define-syntax-rule (trampoline proc) (lambda args (apply proc args))) + +;; Handy macro for flattening nested procedure calls where the output +;; of an inner call is the last argument to the outer call. +(define-syntax chain* + (syntax-rules () + ((_ args (proc ...)) + (apply proc ... args)) + ((_ args (proc ...) . rest) + (chain* (call-with-values + (lambda () (apply proc ... args)) + list) . rest)))) + +(define-syntax-rule (chain arg (proc ...) . rest) + (chain* (list arg) (proc ...) . rest)) |