chicken-users
[Top][All Lists]
Advanced

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

Re: [Chicken-users] Working with ports


From: Mario Domenech Goulart
Subject: Re: [Chicken-users] Working with ports
Date: Mon, 21 Oct 2013 19:46:33 +0000
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux)

Hi,

On Mon, 21 Oct 2013 23:13:03 +0400 Loïc Faure-Lacroix <address@hidden> wrote:

> Being new to scheme, I hardly understand in what are ports superior
> than having a file handle and accessing files using a file descriptor.
>
> In chicken, there is the posix unit that warps usual functions to work
> with files. I would expect these functions to work very well. 
>
> While reading here and there the documentation, I found this:
>
> with-output-to-port and with-input-from-port which rebind
> current-output-port and current-input-port to the one passed to the
> function.
>
> I then wrote this macro:
>
> (define-syntax with-output-file
> (syntax-rules ()
> ((_ file body ...)
> (call-with-output-file file (lambda (port)
> (with-output-to-port port
> (lambda ()
> body ...)))))))
>
> and wrote this function:
>
> ; Dump changes to current-output-port
> (define (dump-changes files)
> (for file in files
> (display (string-append (number->string (file-change-time file)))
> (display ":")
> (display file)
> (newline))))
>
> As you can see, the function is using display to "current-output-port"
> which lead me to this:
>
> (define (save-changes file pattern)
> (with-output-file file
> (dump-changes (glob pattern)))
>
> I'm not sure if I'm doing it right. Usually, I'd write a function that
> receive a port as a parameter explicitly and read/write directly to
> the port. In this case, it is not clear that a user would have to
> capture the output by rebinding the current-output-port. But the
> function that does the work is independent of the port that can be
> used. I'm wondering if this is a good way to write code in scheme.
>
> Being from python, we often say that something is or isn't pythonic. 

Welcome to the Schemeland. :-)

Sorry, it's not very clear to me what you are trying to do.  If you
simply want to write to a file, you can use with-output-to-file.
Example:

(with-output-to-file the-filename
  (lambda ()
    (display "hello")))

If you don't specify a port as argument for `display', it will print to
the value bound to current-output-port.  When you use
with-output-to-file, it binds current-output-port to a port connected to
the given filename.  So, this way you will end up printing to a file.

If you want to write to a file the change time of files that match a
glob pattern you can do something like:

(define (dump-changes files)
  (for-each (lambda (file)
              (display (file-change-time file))
              (display ": ")
              (display file)
              (newline))
            files))

(define (save-changes file pattern)
  (with-output-to-file file
    (lambda ()
      (dump-changes (glob pattern)))))

Does it help?

Best wishes.
Mario
-- 
http://parenteses.org/mario



reply via email to

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