From 3267fc1ace530a37fecd05d0838809c6f0af22a7 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Thu, 6 Feb 2014 21:18:32 -0500 Subject: Add new coroutine macro. * 2d/coroutine.scm (call-with-coroutine): New name for coroutine. (coroutine): New macro. (colambda, codefine, codefine*): Use call-with-coroutine. * examples/coroutine.scm: Use coroutine macro. * README.org: Update coroutine and agenda examples. --- 2d/coroutine.scm | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to '2d') diff --git a/2d/coroutine.scm b/2d/coroutine.scm index 036cdc4..322f236 100644 --- a/2d/coroutine.scm +++ b/2d/coroutine.scm @@ -22,14 +22,15 @@ ;;; Code: (define-module (2d coroutine) - #:export (coroutine + #:export (call-with-coroutine + coroutine colambda codefine codefine*) #:replace (yield)) -(define (coroutine thunk) - "Calls a procedure that can yield a continuation." +(define (call-with-coroutine thunk) + "Apply THUNK with a coroutine prompt." (define (handler cont callback . args) (define (resume . args) ;; Call continuation that resumes the procedure. @@ -42,11 +43,16 @@ ;; Call procedure. (call-with-prompt 'coroutine-prompt thunk handler)) +;; emacs: (put 'coroutine 'scheme-indent-function 0) +(define-syntax-rule (coroutine body ...) + "Evaluate BODY as a coroutine." + (call-with-coroutine (lambda () body ...))) + ;; emacs: (put 'colambda 'scheme-indent-function 1) (define-syntax-rule (colambda args body ...) "Syntacic sugar for a lambda that is run as a coroutine." (lambda args - (coroutine + (call-with-coroutine (lambda () body ...)))) ;; emacs: (put 'codefine 'scheme-indent-function 1) @@ -57,7 +63,7 @@ coroutine." ;; Create an inner procedure with the same signature so that a ;; recursive procedure call does not create a new prompt. (define (name ...) . body) - (coroutine + (call-with-coroutine (lambda () (name ...))))) ;; emacs: (put 'codefine* 'scheme-indent-function 1) @@ -68,7 +74,7 @@ coroutine." ;; Create an inner procedure with the same signature so that a ;; recursive procedure call does not create a new prompt. (define* (name . formals) . body) - (coroutine + (call-with-coroutine (lambda () (apply name args))))) (define (yield callback) -- cgit v1.2.3