From 1a3f148c2c087ebd313ebebbd1c7b45803c35d98 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 1 Dec 2008 16:43:18 -0600 Subject: [PATCH] Add each-line combinator --- core/io/io-docs.factor | 6 ++++++ core/io/io.factor | 3 +++ 2 files changed, 9 insertions(+) diff --git a/core/io/io-docs.factor b/core/io/io-docs.factor index c55377e4a0..02af963e1a 100644 --- a/core/io/io-docs.factor +++ b/core/io/io-docs.factor @@ -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 } diff --git a/core/io/io.factor b/core/io/io.factor index d7d4edf49f..c1fd69a16a 100644 --- a/core/io/io.factor +++ b/core/io/io.factor @@ -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