guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Playing with guile (vs python). Generate file for GDP suitable for g


From: Thien-Thi Nguyen
Subject: Re: Playing with guile (vs python). Generate file for GDP suitable for gnuplot.
Date: Mon, 06 Mar 2017 14:50:31 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (gnu/linux)

() address@hidden (Ludovic Courtès)
() Sat, 11 Feb 2017 15:12:00 +0100

   > (define (file->lines filename)
   >   "Returns a list of lines contained in a file"
   >   (call-with-input-file
   >       filename
   >     (lambda (p)
   >       (let loop ([line (read-line p)])
   >         (if (eof-object? line) (list)
   >             (cons
   >              (substring line 0 (1- (string-length line)))
   >              (loop (read-line p))))))))

   UTF-8 I/O is usually faster in Guile 2.0.  You might want to
   make sure your file is opened as UTF-8:

     (with-fluids ((%default-port-encoding "UTF-8"))
       (call-with-input-file file …))

FWIW, we can also use SRFI 13 to save a few cycles:

 (use-modules (ice-9 rdelim) (srfi srfi-13))
 
 (define (file->lines filename)
   "Return a list of lines contained in a file."
   (call-with-input-file
       filename
     (lambda (p)
       (let loop ((acc '()))
         (let ((line (read-line p)))
           (if (eof-object? line)
               (reverse! acc)            ; rv
               (loop (cons (string-drop-right line 1)
                           acc))))))))

This variant is also more stack-conserving (amenable to TCO),
which should afford some additional speedup.

-- 
Thien-Thi Nguyen -----------------------------------------------
 (defun responsep (query)
   (pcase (context query)
     (`(technical ,ml) (correctp ml))
     ...))                              748E A0E8 1CB8 A748 9BFA
--------------------------------------- 6CE4 6703 2224 4C80 7502

Attachment: signature.asc
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]