Add each-line combinator

db4
Slava Pestov 2008-12-01 16:43:18 -06:00
parent 7b392c88e6
commit 1a3f148c2c
2 changed files with 9 additions and 0 deletions

View File

@ -253,6 +253,10 @@ HELP: lines
{ $values { "stream" "an input stream" } { "seq" "a sequence of strings" } }
{ $description "Reads lines of text until the stream is exhausted, collecting them in a sequence of strings." } ;
HELP: each-line
{ $values { "quot" { $quotation "( str -- )" } } }
{ $description "Calls the quotatin with successive lines of text, until the current " { $link input-stream } " is exhausted." } ;
HELP: contents
{ $values { "stream" "an input stream" } { "str" string } }
{ $description "Reads the entire contents of a stream into a string." }
@ -364,6 +368,8 @@ ARTICLE: "stream-utils" "Stream utilities"
$nl
"First, a simple composition of " { $link stream-write } " and " { $link stream-nl } ":"
{ $subsection stream-print }
"Processing lines one by one:"
{ $subsection each-line }
"Sluring an entire stream into memory all at once:"
{ $subsection lines }
{ $subsection contents }

View File

@ -99,6 +99,9 @@ SYMBOL: error-stream
: lines ( stream -- seq )
[ [ readln dup ] [ ] [ drop ] produce ] with-input-stream ;
: each-line ( quot -- )
[ [ readln dup ] ] dip [ drop ] while ; inline
: contents ( stream -- str )
[
[ 65536 read dup ] [ ] [ drop ] produce concat f like