Skip to content

Instantly share code, notes, and snippets.

@tcolgate
Last active December 14, 2015 10:49
Show Gist options
  • Select an option

  • Save tcolgate/5075232 to your computer and use it in GitHub Desktop.

Select an option

Save tcolgate/5075232 to your computer and use it in GitHub Desktop.
Another wingo co-routine
(define (call-with-yield proc)
(let ((tag (make-prompt-tag)))
(define (handler k . args)
(define (resume . args)
(call-with-prompt tag
(lambda () (apply k args))
handler))
(apply values resume args))
(call-with-prompt
tag
(lambda ()
(let ((yield (lambda args
(apply abort-to-prompt tag args))))
(proc yield)))
handler)))
(define-syntax with-yield
(syntax-rules ()
((_ yield exp exp* ...)
(call-with-yield
(lambda (yield) exp exp* ...)))))
(define (generate traverse collection)
(with-yield yield
(traverse yield collection)))
> (generate for-each '(three blind mice))
$1 = #<procedure resume args>
$2 = three
> ($1)
$3 = #<procedure resume args>
$4 = blind
> ($3)
$5 = #<procedure resume args>
$6 = mice
> ($1)
$7 = #<procedure resume args>
$8 = blind
> ($5)
>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment