guix-devel
[Top][All Lists]
Advanced

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

Re: Adding wc to Bournish


From: Efraim Flashner
Subject: Re: Adding wc to Bournish
Date: Tue, 7 Jun 2016 10:41:55 +0300
User-agent: Mutt/1.6.1 (2016-04-27)

On Sun, Jun 05, 2016 at 10:37:12PM +0200, Ludovic Courtès wrote:
> Efraim Flashner <address@hidden> skribis:
> > +
> > +(define* (wc-command-implementation file #:optional args)
> > +  (let-values (((lines chars)
> > +                (call-with-input-file file lines+chars)))
> > +    (match args
> > +      (#\l
> > +       (format #t "~a ~a~%" lines file))
> > +      (#\c
> > +       (format #t "~a ~a~%" chars file))
> > +      (_
> > +       (format #t "~a ~a ~a~%" lines chars file)))))
> > +
> > +(define (wc-command args . rest)
> > +  (let* ((flags (cond ((string=? args "-l") #\l)
> > +                      ((string=? args "-c") #\c)
> > +                      (else #\nul)))    ; no flags, "args" is a file
> > +         (files (filter (lambda (file)
> > +                          (catch 'system-error
> > +                            (lambda ()
> > +                              (lstat file))
> > +                            (lambda args
> > +                               (let ((errno (system-error-errno args)))
> > +                                (format (current-error-port) "~a: ~a~%"
> > +                                        file (strerror errno))
> > +                                #f))))
> > +                        (if (char=? flags #\nul) (cons args rest) rest))))
> > +    (for-each
> > +      (lambda (file)
> > +        ((@@ (guix build bournish) wc-command-implementation) file flags))
> > +    files)))
> 
> As discussed at
> <https://lists.gnu.org/archive/html/guix-devel/2016-05/msg00782.html>,
> remember that ‘wc-command’ is called by the compiler to generate Scheme
> code from the input shell code.  Thus, it must emit code that does the
> job.  However, here, it does the job directly, at compilation time, and
> emits the result of ‘for-each’ as code.

copied from that email:
Thus, you must similarly distinguish those two stages by providing:

1. A ‘wc-command-implementation’ procedure that implements ‘wc’;

2. A ‘wc-command’ procedure that emits the code that calls
wc-command-implementation’; so something like:

(define (wc-command args)
 `((@@ (guix build bournish) wc-command-implementation) ,@args))

Better yet, ‘wc-command’ could check for the presence of “-l” or “-c” at
compile time and emit a call to the right thing.

> 
> I’ll commit a couple of fixes for bugs I just found and that prevent us
> from doing:
> 
>   (compile "ls" #:from %bournish-language #:to 'scheme).
> 
> This is useful to clearly understand what code is generated from the
> input.
> 
> Ludo’.

I've refactored the code so now in wc-command it checks if theres a flag
or not, and then passes the list of files to wc-command-implementation
to do the actual computation.

Does it make sense to switch it to something like the case-lambda setup
that `ls' uses or is for-each ok?

-- 
Efraim Flashner   <address@hidden>   אפרים פלשנר
GPG key = A28B F40C 3E55 1372 662D  14F7 41AA E7DC CA3D 8351
Confidentiality cannot be guaranteed on emails sent or received unencrypted

Attachment: 0001-bournish-Add-wc-command.patch
Description: Text document

Attachment: signature.asc
Description: PGP signature


reply via email to

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