我试图端口yield
和yield from
从Python的方案。
这是我做过的实现:
(define (coroutine routine)
(let ((current routine)
(status 'new))
(lambda* (#:optional value)
(let ((continuation-and-value
(call/cc (lambda (return)
(let ((returner
(lambda (value)
(call/cc (lambda (next)
(return (cons next value)))))))
(if (equal? status 'new)
(begin
(set! status 'running)
(current returner))
(current (cons value returner)))
(set! status 'dead))))))
(if (pair? continuation-and-value)
(begin (set! current (car continuation-and-value))
(cdr continuation-and-value))
continuation-and-value)))))
这个问题,这个实现的是,它必须调用方式并不看起来像Python的yield
。
(define why (call/cc (lambda (yield)
(format #t "love me or leave me!")
(yield "I leave!")
;; the program never reach this part
(format #t "it probably left :("))))
(format #t "return actually populates WHY variable\n")
(format #t "WHY: ~a\n")
别的不说,每次我需要重新启动协程,我必须let
新return
变量能够exit
协程。基本上,我觉得语法太冗长。是否有其他有更清晰的语法?
它应该可以yield
和send
值的协程。下面是如何的协同程序必须使用一个例子:
(define-coroutine (zrange start step)
"compute a range of values starting a START with STEP between
each value. The coroutine must be restarted with 0 or more, which
is added to the step"
(let loop ((n start))
(loop (+ n step (yield n)))))
(coroutine-map (zrange 0 10) '(1 100 1000 10000 100000))
;; => 0 110 1120 11130 111140
在上文中,1
被忽略,然后100
,1000
是send
到发电机上。我做了一个实现的基础上,@sylwester代码,但是我有一个宏观的烦恼:
(define (make-generator procedure)
(define last-return #f)
(define last-value #f)
(define last-continuation (lambda (_) (procedure yield)))
(define (return value)
(newline)(display "fuuu")(newline)
(call/cc (lambda (continuation)
(set! last-continuation continuation)
(set! last-value value)
(last-return value))))
(lambda* (. rest) ; ignore arguments
(call/cc (lambda (yield)
(set! last-return yield)
(apply last-continuation rest)))))
(define-syntax define-coroutine
(syntax-rules ()
((_ (name args ...) body ...)
(define (name args ...)
(make-generator
(lambda (yield)
body ...))))))
(define-coroutine (zrange start step)
(let loop ((n start))
(loop (+ n step (yield n)))))
(display (map (zrange 0 10) '(1 100 1000 10000 100000)))
事情是这样的:
(define (make-generator procedure)
(define last-return values)
(define last-value #f)
(define (last-continuation _)
(let ((result (procedure yield)))
(last-return result)))
(define (yield value)
(call/cc (lambda (continuation)
(set! last-continuation continuation)
(set! last-value value)
(last-return value))))
(lambda args
(call/cc (lambda (return)
(set! last-return return)
(if (null? args)
(last-continuation last-value)
(apply last-continuation args))))))
使用这样的:
(define test
(make-generator
(lambda (collect)
(collect 1)
(collect 5)
(collect 10)
#f)))
(test) ; ==> 1
(test) ; ==> 5
(test) ; ==> 10
(test) ; ==> #f (procedure finished)
现在,我们可以包装内部成宏:
(define-syntax (define-coroutine stx)
(syntax-case stx ()
((_ (name . args) . body )
#`(define (name . args)
(make-generator
(lambda (#,(datum->syntax stx 'yield))
. body))))))
请注意,define-coroutine
使用语法的情况,因为我们需要yield
不卫生的实现。
(define-coroutine (countdown-from n)
(let loop ((n n))
(if (= n 0)
0
(loop (- (yield n) 1)))))
(define countdown-from-10 (countdown-from 10))
(define (ignore procedure)
(lambda ignore
(procedure)))
(map (ignore countdown-from-10) '(1 1 1 1 1 1)) ; ==> (10 9 8 7 6 5)
;; reset
(countdown-from-10 10) ; ==> 9
(countdown-from-10) ; ==> 8
;; reset again
(countdown-from-10 100) ; ==> 99
荣誉对@Sylwester为一个伟大的答案。
困难的部分是让yield
提供给发电机的功能。 datum->syntax
创建一个语法对象,并要求您提供从为新对象的背景下采取的另一种语法对象。在这种情况下,我们可以使用STX具有相同的上下文传递到宏观的功能。
如果人们发现它有用,我用一个简单的版本:
(define-syntax (set-continuation! stx)
"Simplifies the common continuation idiom
(call/cc (λ (k) (set! name k) <do stuff>))"
(syntax-case stx ()
[(_ name . body)
#`(call/cc (λ (k)
(set! name k)
. body))]))
(define-syntax (make-generator stx)
"Creates a Python-like generator.
Functions passed in can use the `yield` keyword to return values
while temporarily suspending operation and returning to where they left off
the next time they are called."
(syntax-case stx ()
[(_ fn)
#`(let ((resume #f)
(break #f))
(define #,(datum->syntax stx 'yield)
(λ (v)
(set-continuation! resume
(break v))))
(λ ()
(if resume
(resume #f)
(set-continuation! break
(fn)
'done))))]))
它的用法的例子:
(define countdown
(make-generator
(λ ()
(for ([n (range 5 0 -1)])
(yield n)))))
(countdown)
=> 5
...
(countdown)
=> 1
(countdown)
=> 'done
(countdown)
=> 'done
一种方法是在这里。如果您使用的诡计,你应该使用提示(他们是约两个数量级比使用与狡诈完全延续更快):
How to implement Python-style generator in Scheme (Racket or ChezScheme)?