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 11:57:26 +0200
User-agent: mu4e 0.9.16; emacs 24.5.1

Efraim Flashner <address@hidden> writes:

> +(define wc-command-implementation
> +  (lambda files
> +    (let ((files (filter (lambda (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))))
> +                         files)))
> +      (for-each
> +        (lambda (file)
> +          (let-values (((lines chars)
> +                        (call-with-input-file file lines+chars)))
> +                      (format #t "~a ~a ~a~%" lines chars file)))
> +        files))))
> +
> +(define wc-l-command-implementation
> +  (lambda files
> +    (let ((files (filter (lambda (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))))
> +                         files)))
> +      (for-each
> +        (lambda (file)
> +          (let-values (((lines chars)
> +                        (call-with-input-file file lines+chars)))
> +                      (format #t "~a ~a~%" lines file)))
> +        files))))
> +
> +(define wc-c-command-implementation
> +  (lambda files
> +    (let ((files (filter (lambda (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))))
> +                         files)))
> +      (for-each
> +        (lambda (file)
> +          (let-values (((lines chars)
> +                        (call-with-input-file file lines+chars)))
> +                      (format #t "~a ~a~%" chars file)))
> +        files))))

It looks to me that the filter function is the same in all of these
procedures.  Even the actual implementation, i.e. the for-each over the
resulting files is almost exactly the same.

This could be simplified.  If only the format expression differs then
you could abstract this difference away.  You could still have three
different procedures, but they can be the result of evaluating a
higher-order function.

It also seems to me that you could use syntactic sugar to simplify
“(define something (lambda ...))” to “(define (something ...))”.

~~ Ricardo



reply via email to

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