From 11dba13e4d36d57f0eea0524f52b9b4616049ed2 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Sat, 6 Dec 2014 11:16:26 -0500 Subject: utils: Add chain macro. * sly/utils.scm (chain, chain*): New syntax. --- sly/utils.scm | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) 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)) -- cgit v1.2.3