chicken-users
[Top][All Lists]
Advanced

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

[Chicken-users] Working with ports


From: Loïc Faure-Lacroix
Subject: [Chicken-users] Working with ports
Date: Mon, 21 Oct 2013 23:13:03 +0400

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. 


-- 
Loïc Faure-Lacroix

reply via email to

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