diff options
author | Leah Neukirchen <leah@vuxu.org> | 2022-12-01 21:03:45 +0100 |
---|---|---|
committer | Leah Neukirchen <leah@vuxu.org> | 2022-12-01 22:56:31 +0100 |
commit | f690959ef59004ba00d2d54073c9b8265e5b6d57 (patch) | |
tree | d8dd2698ae088bfe321fd40e3724cf376bfd68af | |
parent | 73b23b27998906ec479a995434ca5b664f0c0912 (diff) | |
download | mew-f690959ef59004ba00d2d54073c9b8265e5b6d57.tar.gz mew-f690959ef59004ba00d2d54073c9b8265e5b6d57.tar.xz mew-f690959ef59004ba00d2d54073c9b8265e5b6d57.zip |
add lines
-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)))) |