diff options
-rw-r--r-- | mew.scm | 18 | ||||
-rw-r--r-- | mew.svnwiki | 9 | ||||
-rw-r--r-- | tests/test.mew | 6 |
3 files changed, 31 insertions, 2 deletions
diff --git a/mew.scm b/mew.scm index 7f491b9..4caebd1 100644 --- a/mew.scm +++ b/mew.scm @@ -11,7 +11,7 @@ imp inc inject into juxt keys - len loc + len lines loc mod negate odometer one-of op op* @@ -823,6 +823,22 @@ (read-string #f (current-input-port))))) (else (error "no slurp defined")))) + (define lines + (case-lambda + (() read-line) + ((x) (cond ((input-port? x) + (lambda () + (read-line x))) + ((string? x) + (let ((file (open-input-file x))) + (lambda () + (let ((line (read-line file))) + (when (eof-object? line) + (close-input-port file)) + line)))) + (else + (error "can't read lines")))))) + (define (hash-table->generator h) (make-for-each-generator (lambda (f t) (hash-table-for-each t (lambda (k v) diff --git a/mew.svnwiki b/mew.svnwiki index 6de85b1..c705e29 100644 --- a/mew.svnwiki +++ b/mew.svnwiki @@ -304,6 +304,15 @@ If {{<obj>}} is an input-port, read all data into a string. If {{<obj>}} is a string, read all data from the file named {{<<obj>>}. If {{<obj>}} is false, read all data from {{*current-input-port*}}. +<procedure>(lines [<obj>])</procedure> + +If {{<obj>}} is missing, return {{read-line}} (a generator that +reads lines from {{*current-input-port*}}). +If {{<obj>}} is an input-port, return a generator that reads lines +from {{<obj>}}. +If {{<obj>}} is a string, return a generator that reads lines +from the file named {{<<obj>>} and closes it on EOF. + == Equality diff --git a/tests/test.mew b/tests/test.mew index 4bcce7f..92eb35c 100644 --- a/tests/test.mew +++ b/tests/test.mew @@ -427,7 +427,11 @@ (test-group "slurp" (test #t (string? (slurp "/usr/bin/chicken-status"))) - (test-error (string? (slurp "/dev/null/noexist")))) + (test-error (slurp "/dev/null/noexist"))) + +(test-group "lines" + (test #t (string? ((lines "/usr/bin/chicken-status")))) + (test-error (lines "/dev/null/noexist"))) (test-group "gen" (test '(1 2 3) (into '() (gen '(1 2 3)))) |