It's a great idea to include line reading
into a standard library. Here is a few comments.

There are two differences from the original
readlines:
 - overlapped reading (not once and only once)
   (with asserting presence of LF in current block)
 - automatic removal of terminators

The reason for the constraints put out earlier
was to meet user expectations on behavior of
line reading functionality, which is based on
experience from other languages, where such
functionality already existed and used extensively.

Once and only once: it to convey the stream-based
idea about reading. Although J's fread is random
access, it's more common to assume that the source
is a stream, as in C runtime fread. That would allow
to extend it later to stdin or sockets (in which case
the terminating condition would be not file
size, but EOF).

The maintenance of a buffer between reads does 
not impact performance nor does it complicate 
implementation, and even makes it simpler:
assertion of LF in block is an artifact of
random block reading, which would not have
been needed in stream buffer.

Handling terminators could be made into an
option. As it is in fapplylines, it is good for
casual processing, where we do not care about terminators.
Though, one test of line reading ability is implemnting
wc with it, thus accurate preservation of
terminators in readlines. That was modeled after perl:

open(LOGFILE) or die("Could not open log file.");
foreach $line (<LOGFILE>) {
    chomp($line);  # remove the newline from $line.
    # do line-by-line processing.
}


--- Chris Burke <[EMAIL PROTECTED]> wrote:

> Chris Burke wrote:
> > I suggest that we add two new definitions to the files script. One is
> > Joey's verb to read a LF-terminated block from a file, the other is
> > Oleg's adverb to apply a function to each line of a file.
> 
> There was typo in the second definition, which should be:
> 
> fapplylines=: 1 : 0
> y=. 8 u: y    NB. for j601
> s=. 1!:4 <y
> if. s = _1 do. return. end.
> p=. 0
> while. p < s do.
>   dat=. 1!:11 y;p,1e6<.s-p
>   len=. 1 + dat i: LF
>   p=. p + len
>   if. len > #dat do.
>     if. p > s do.
>       dat=. dat, LF
>     else.
>       'file not in LF-delimited lines' 13!:8[3
>     end.
>   else.
>     dat=. len {. dat
>   end.
>   u ;._2 dat -. CR
> end.
> )




__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to