[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
- Re: Adding wc to Bournish, Efraim Flashner, 2016/06/05
- Re: Adding wc to Bournish, Ludovic Courtès, 2016/06/05
- Re: Adding wc to Bournish, Efraim Flashner, 2016/06/07
- Re: Adding wc to Bournish, Ludovic Courtès, 2016/06/08
- Re: Adding wc to Bournish, Efraim Flashner, 2016/06/14
- Re: Adding wc to Bournish,
Ricardo Wurmus <=
- Re: Adding wc to Bournish, Efraim Flashner, 2016/06/14
- Re: Adding wc to Bournish, Efraim Flashner, 2016/06/14
- Re: Adding wc to Bournish, Ricardo Wurmus, 2016/06/14
- Re: Adding wc to Bournish, Ludovic Courtès, 2016/06/15
- Re: Adding wc to Bournish, Efraim Flashner, 2016/06/15
- Re: Adding wc to Bournish, Ludovic Courtès, 2016/06/23