Update generator syntax

This commit is contained in:
Dane Johnson 2024-11-11 22:15:27 -06:00
parent 7682d622a8
commit 921d770986
2 changed files with 15 additions and 6 deletions

11
d-.scm
View File

@ -9,7 +9,7 @@
for
partial
argmin
make-generator))
generator))
(define-syntax ~>
(syntax-rules ()
@ -67,3 +67,12 @@
(lambda (k value)
(set! thunk k)
value))))
(define-syntax generator
(lambda (x)
(syntax-case x ()
[(generator expr ...)
(with-syntax ([yield (datum->syntax x 'yield)])
#'(make-generator
(lambda (yield)
expr ...)))])))

View File

@ -85,13 +85,13 @@
(test
(assert-equal (argmin cdr < '(a . 5) '(b . 2) '(c . 3)) '(b . 2))))
(define-test make-generator
(define-test generator
(test
(define number-generator
(make-generator (lambda (yield)
(generator
(let loop ([i 1])
(yield i)
(loop (1+ i))))))
(loop (1+ i)))))
(assert-equal (number-generator) 1)
(assert-equal (number-generator) 2)
(assert-equal (number-generator) 3)