guix-devel
[Top][All Lists]
Advanced

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

Re: Adding wc to Bournish


From: Ricardo Wurmus
Subject: Re: Adding wc to Bournish
Date: Tue, 14 Jun 2016 13:08:54 +0200
User-agent: mu4e 0.9.16; emacs 24.5.1

Efraim Flashner <address@hidden> writes:

>> It's already calling `((@@ (guix build bournish)
>> wc-l-command-implementation) ,@(delete "-l" args)), I could try changing
>> the ,@(delete part to ,@((@@ (guix build bournish) only-files) ,@(delete
>> "-l" args)) and then the various implementation functions will be just
>> printing the results
>> 
>
> It turns out I forgot that calling only-files from wc-commands would
> make it evaluate too soon, so I stuck it inside the different
> implementations. Factoring out the check if a file exists or not could
> also apply to the ls-command-implementation too later.

I don’t understand this (but don’t let this delay you).  I don’t see any
“only-files” in your patch.  To delay evaluation you can always wrap
expressions in “(lambda _ ...)”.

I was thinking of a first simplification like this:


(define (existing-files file)
  (catch 'system-error
    (lambda ()
      (stat file))
    (lambda args
      (let ((errno (system-error-errno args)))
        (format (current-error-port) "~a: ~a~%"
                file (strerror errno))
        #f))))

(define (wc-print file)
  (let-values (((lines chars)
                (call-with-input-file file lines+chars)))
    (format #t "~a ~a ~a~%" lines chars file)))

(define (wc-l-print file)
  (let-values (((lines chars)
                (call-with-input-file file lines+chars)))
    (format #t "~a ~a~%" lines file)))

(define (wc-c-print file)
  (let-values (((lines chars)
                (call-with-input-file file lines+chars)))
    (format #t "~a ~a~%" chars file)))

(define (wc-command-implementation files)
  (for-each wc-print (filter existing-files files)))

(define (wc-l-command-implementation files)
  (for-each wc-l-print (filter existing-files files)))

(define (wc-c-command-implementation files)
  (for-each wc-c-print (filter existing-files files)))


Even at this point there’s a lot of repetition that we could get rid of,
but maybe that’s too zealous.  Note that I didn’t follow this whole
discussion, so I could be completely off target.

~~ Ricardo



reply via email to

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