From mboxrd@z Thu Jan 1 00:00:00 1970 From: Ricardo Wurmus Subject: Re: Adding wc to Bournish Date: Tue, 14 Jun 2016 11:57:26 +0200 Message-ID: References: <20160524184720.GA27449@debian-netbook> <20160605124033.GB859@debian-netbook> <87eg8bpe2v.fsf@gnu.org> <20160607074155.GB32264@debian-netbook> <878tyfelf6.fsf@gnu.org> <20160614092713.GA2832@debian-netbook> Mime-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:33286) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bCl86-0002G7-Rg for guix-devel@gnu.org; Tue, 14 Jun 2016 05:59:31 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bCl82-0007DV-Mf for guix-devel@gnu.org; Tue, 14 Jun 2016 05:59:29 -0400 In-Reply-To: <20160614092713.GA2832@debian-netbook> List-Id: "Development of GNU Guix and the GNU System distribution." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-devel-bounces+gcggd-guix-devel=m.gmane.org@gnu.org Sender: "Guix-devel" To: Efraim Flashner Cc: guix-devel@gnu.org Efraim Flashner 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 =E2=80=9C(define something (lambda ...))=E2=80=9D to =E2=80=9C(define (so= mething ...))=E2=80=9D. ~~ Ricardo