Added some generator clauses
Now in-file, in-port, in-lists and in-generator have generator clauses
This commit is contained in:
parent
7b3814c430
commit
bb0de3e949
2 changed files with 68 additions and 17 deletions
2
goof.scm
2
goof.scm
|
@ -33,7 +33,7 @@
|
||||||
#:use-module (goof helpers)
|
#:use-module (goof helpers)
|
||||||
#:use-module (goof ref-let)
|
#:use-module (goof ref-let)
|
||||||
#:use-module (rnrs io simple)
|
#:use-module (rnrs io simple)
|
||||||
#:use-module ((srfi srfi-1) #:select (circular-list find))
|
#:use-module ((srfi srfi-1) #:select (any circular-list find))
|
||||||
#:use-module ((srfi srfi-43) #:select (vector-copy vector-copy!))
|
#:use-module ((srfi srfi-43) #:select (vector-copy vector-copy!))
|
||||||
#:use-module (srfi srfi-71)
|
#:use-module (srfi srfi-71)
|
||||||
#:use-module (ice-9 futures)
|
#:use-module (ice-9 futures)
|
||||||
|
|
|
@ -101,7 +101,23 @@
|
||||||
((elts (map car pairs))
|
((elts (map car pairs))
|
||||||
(succ (map step pairs)))
|
(succ (map step pairs)))
|
||||||
()
|
()
|
||||||
. rest))))
|
. rest))
|
||||||
|
;; Generator clause
|
||||||
|
((in-lists lists ...)
|
||||||
|
(gen-lists lists ...))))
|
||||||
|
|
||||||
|
|
||||||
|
;; TODO: make this not slow.
|
||||||
|
(define (gen-lists . lists)
|
||||||
|
(lambda ()
|
||||||
|
(if (any null? lists)
|
||||||
|
(eof-object)
|
||||||
|
(let ((cars (map car lists))
|
||||||
|
(cdrs (map cdr lists)))
|
||||||
|
(set! lists cdrs)
|
||||||
|
(values cars)))))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
(define-syntax define-in-indexed
|
(define-syntax define-in-indexed
|
||||||
(syntax-rules ()
|
(syntax-rules ()
|
||||||
|
@ -156,10 +172,24 @@
|
||||||
((eof? var))
|
((eof? var))
|
||||||
()
|
()
|
||||||
()
|
()
|
||||||
. rest))))
|
. rest))
|
||||||
|
;; generator clauses
|
||||||
|
((in-port port)
|
||||||
|
(gen-port port read-char eof-object?))
|
||||||
|
((in-port port reader)
|
||||||
|
(gen-port port reader eof-object?))
|
||||||
|
((in-port port reader eof?)
|
||||||
|
(gen-port port reader eof?))))
|
||||||
|
|
||||||
|
(define (gen-port port read eof?)
|
||||||
|
(lambda ()
|
||||||
|
(let ((res (read port)))
|
||||||
|
(if (eof? res)
|
||||||
|
(eof-object)
|
||||||
|
res))))
|
||||||
|
|
||||||
(define-syntax in-file
|
(define-syntax in-file
|
||||||
(syntax-rules ()
|
(syntax-rules ()
|
||||||
((in-file ((var) source) next . rest)
|
((in-file ((var) source) next . rest)
|
||||||
(in-file ((var p) source) next . rest))
|
(in-file ((var p) source) next . rest))
|
||||||
((in-file ((var p) (file)) next . rest)
|
((in-file ((var p) (file)) next . rest)
|
||||||
|
@ -171,7 +201,23 @@
|
||||||
((var (r p) (r p)))
|
((var (r p) (r p)))
|
||||||
((e? var))
|
((e? var))
|
||||||
()
|
()
|
||||||
((close-input-port p)) . rest))))
|
((close-input-port p)) . rest))
|
||||||
|
;; generator clauses
|
||||||
|
((in-file path)
|
||||||
|
(gen-file path read-char eof-object?))
|
||||||
|
((in-file path read)
|
||||||
|
(gen-file path read eof-object?))
|
||||||
|
((in-file path read eof?)
|
||||||
|
(gen-file path read eof?))))
|
||||||
|
|
||||||
|
(define (gen-file path read eof?)
|
||||||
|
(let ((file (open-input-file path)))
|
||||||
|
(lambda ()
|
||||||
|
(let ((res (read file)))
|
||||||
|
(cond ((eof? res)
|
||||||
|
(close-port file)
|
||||||
|
(eof-object))
|
||||||
|
(else res))))))
|
||||||
|
|
||||||
(define-syntax in-generator
|
(define-syntax in-generator
|
||||||
(syntax-rules ()
|
(syntax-rules ()
|
||||||
|
@ -181,7 +227,12 @@
|
||||||
((eof-object? var))
|
((eof-object? var))
|
||||||
()
|
()
|
||||||
()
|
()
|
||||||
. rest))))
|
. rest))
|
||||||
|
;; yes, generator clause
|
||||||
|
((in-generator gen)
|
||||||
|
(let ((g gen))
|
||||||
|
(lambda ()
|
||||||
|
(g))))))
|
||||||
|
|
||||||
(define-syntax up-from
|
(define-syntax up-from
|
||||||
(syntax-rules (to by)
|
(syntax-rules (to by)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue