unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#1939: suggestion: adding read-line function to elisp
@ 2009-01-17 18:13 xah lee
  2009-01-18  5:48 ` Juanma Barranquero
       [not found] ` <mailman.5150.1232258617.26697.bug-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 3+ messages in thread
From: xah lee @ 2009-01-17 18:13 UTC (permalink / raw)
  To: bug-gnu-emacs

summary:
i'd like to have a  read-line function in elisp, that will read a  
given file and return a list of lines.

The reason is that in almost all modern scripting langs (php, perl,  
python, ruby), there's such a function, so that users can get a list  
of lines of a file in about single line of code. Getting lines as  
list is a very frequent need.

elisp is very suitable for tasks of text processing. So, having such  
a convenience function seems appropriate.

Here's a implementation:

(defun read-lines (file)
   "Return a list of lines in FILE."
   (with-temp-buffer
     (insert-file-contents file)
     (split-string
      (buffer-substring-no-properties 1 (point-max)) "\n" t)
     )
   )

Thanks.

The thought came from a recent newsgroup discussion in comp.lang.lisp
http://groups.google.com/group/comp.lang.lisp/browse_frm/thread/ 
761fe827c8c9ea5e/

Here's a short article for some context:

• A Ruby Illustration of Lisp Problems
   http://xahlee.org/UnixResource_dir/writ/lisp_problems_by_ruby.html

   Xah
∑ http://xahlee.org/^ permalink raw reply	[flat|nested] 3+ messages in thread

* bug#1939: suggestion: adding read-line function to elisp
  2009-01-17 18:13 bug#1939: suggestion: adding read-line function to elisp xah lee
@ 2009-01-18  5:48 ` Juanma Barranquero
       [not found] ` <mailman.5150.1232258617.26697.bug-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 3+ messages in thread
From: Juanma Barranquero @ 2009-01-18  5:48 UTC (permalink / raw)
  To: xah lee; +Cc: 1939

On Sat, Jan 17, 2009 at 19:13, xah lee <xah@xahlee.org> wrote:

> Getting lines as list is a very frequent need.
>
> elisp is very suitable for tasks of text processing. So, having such a
> convenience function seems appropriate.

Getting lines as list is much less frequent in elisp. The reason is
that is much more flexible (and faster) to manipulate text in a buffer
than as a collection of strings.

So in most cases, using `with-temp-buffer' (as in your read-lines
function) is the Right Thing To Do.

    Juanma






^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: bug#1939: suggestion: adding read-line function to elisp
       [not found] ` <mailman.5150.1232258617.26697.bug-gnu-emacs@gnu.org>
@ 2009-01-20 18:29   ` Ted Zlatanov
  0 siblings, 0 replies; 3+ messages in thread
From: Ted Zlatanov @ 2009-01-20 18:29 UTC (permalink / raw)
  To: bug-gnu-emacs

On Sun, 18 Jan 2009 06:48:10 +0100 Juanma Barranquero <lekktu@gmail.com> wrote: 

JB> On Sat, Jan 17, 2009 at 19:13, xah lee <xah@xahlee.org> wrote:
>> Getting lines as list is a very frequent need.
>> 
>> elisp is very suitable for tasks of text processing. So, having such a
>> convenience function seems appropriate.

JB> Getting lines as list is much less frequent in elisp. The reason is
JB> that is much more flexible (and faster) to manipulate text in a buffer
JB> than as a collection of strings.

JB> So in most cases, using `with-temp-buffer' (as in your read-lines
JB> function) is the Right Thing To Do.

OTOH, it would be nice to have a way to call a lambda for every line in
a file (`mapfile' essentially) without using a buffer.  The function
would get the line and the line number as parameters.  That would let
Emacs Lisp do some processing of large files without running out of
memory.

Ted


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2009-01-20 18:29 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-01-17 18:13 bug#1939: suggestion: adding read-line function to elisp xah lee
2009-01-18  5:48 ` Juanma Barranquero
     [not found] ` <mailman.5150.1232258617.26697.bug-gnu-emacs@gnu.org>
2009-01-20 18:29   ` Ted Zlatanov

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).