From f690959ef59004ba00d2d54073c9b8265e5b6d57 Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Thu, 1 Dec 2022 21:03:45 +0100 Subject: add lines --- mew.scm | 18 +++++++++++++++++- mew.svnwiki | 9 +++++++++ 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 {{}} is an input-port, read all data into a string. If {{}} is a string, read all data from the file named {{<>}. If {{}} is false, read all data from {{*current-input-port*}}. +(lines []) + +If {{}} is missing, return {{read-line}} (a generator that +reads lines from {{*current-input-port*}}). +If {{}} is an input-port, return a generator that reads lines +from {{}}. +If {{}} is a string, return a generator that reads lines +from the file named {{<>} 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)))) -- cgit 1.4.1