2006/12/25
12/22のrangeジェネレータだが、ジェネレータと範囲を取ってジェネレータを 返す方が綺麗だな。
(define-constant *eof* (read-from-string "")) (define (eof) *eof*) (define (generator-range begin? end? gen) (define next #f) (define (in) (let1 elt (gen) (cond ((eof-object? elt) (set! next eof) *eof*) ((end? elt) (set! next out))) elt)) (define (out) (let1 elt (gen) (cond ((eof-object? elt) (set! next eof) *eof*) ((begin? elt) (set! next in) elt) (else (out))))) (set! next out) (lambda () (next)))
これで
gosh> (with-input-from-process "w3m -dump_source http://www.yahoo.com" (cut port-for-each print (generator-range #/^<style/ #/<\/style>/ read-line))) <style type="text/css"> a{color:#16387c;} a:link,a:visited{text-decoration:none;} a:hover{text-decoration:underline;} </style> <style type="text/css" media="all"> #p{width:310px;} form{margin:0;} </style> #<undef>
こうなる。
Tag: Gauche