On Tue, Jun 14, 2016 at 01:20:29PM +0300, Efraim Flashner wrote:
> On Tue, Jun 14, 2016 at 11:57:26AM +0200, Ricardo Wurmus wrote:
> > 
> > Efraim Flashner <[email protected]> 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
> 
> 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.


-- 
Efraim Flashner   <[email protected]>   אפרים פלשנר
GPG key = A28B F40C 3E55 1372 662D  14F7 41AA E7DC CA3D 8351
Confidentiality cannot be guaranteed on emails sent or received unencrypted
From 09eef9cd841a7d212e024be0609168611923696b Mon Sep 17 00:00:00 2001
From: Efraim Flashner <[email protected]>
Date: Sun, 22 May 2016 14:56:06 +0300
Subject: [PATCH] bournish: Add `wc' command.

* guix/build/bournish.scm (lines+chars, only-files, wc-commands,
wc-command-implementation, wc-l-command-implementation,
wc-c-command-implementation): New variables.
(%commands): Add wc command.
---
 guix/build/bournish.scm | 58 ++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 57 insertions(+), 1 deletion(-)

diff --git a/guix/build/bournish.scm b/guix/build/bournish.scm
index 1f17e0a..a8f6591 100644
--- a/guix/build/bournish.scm
+++ b/guix/build/bournish.scm
@@ -1,5 +1,6 @@
 ;;; GNU Guix --- Functional package management for GNU
 ;;; Copyright © 2016 Ludovic Courtès <[email protected]>
+;;; Copyright © 2016 Efraim Flashner <[email protected]>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -25,6 +26,7 @@
   #:use-module (ice-9 match)
   #:use-module (ice-9 ftw)
   #:use-module (srfi srfi-1)
+  #:use-module (srfi srfi-11)
   #:use-module (srfi srfi-26)
   #:export (%bournish-language))
 
@@ -103,6 +105,59 @@ characters."
        ((@ (guix build utils) dump-port) port (current-output-port))
        *unspecified*)))
 
+(define (lines+chars port)
+  ;; Return the number of lines and number of chars read from PORT.
+  ;; TODO: Also return the number of words.
+  (let loop ((lines 0) (chars 0))
+    (match (read-char port) ; get the next char ready
+      ((? eof-object?)              ;done!
+       (values lines chars))
+      (#\newline                    ;recurse
+       (loop (1+ lines) (1+ chars)))
+      (_                            ;recurse
+       (loop lines (1+ chars))))))
+
+(define (only-files 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))
+
+(define (wc-command-implementation . files)
+  (for-each
+    (lambda (file)
+      (let-values (((lines chars)
+                    (call-with-input-file file lines+chars)))
+                  (format #t "~a ~a ~a~%" lines chars file)))
+    ((@@ (guix build bournish) only-files) files)))
+
+(define (wc-l-command-implementation . files)
+  (for-each
+    (lambda (file)
+      (let-values (((lines chars)
+                    (call-with-input-file file lines+chars)))
+                  (format #t "~a ~a~%" lines file)))
+    ((@@ (guix build bournish) only-files) files)))
+
+(define (wc-c-command-implementation . files)
+  (for-each
+    (lambda (file)
+      (let-values (((lines chars)
+                    (call-with-input-file file lines+chars)))
+                  (format #t "~a ~a~%" chars file)))
+    ((@@ (guix build bournish) only-files) files)))
+
+(define (wc-commands . args)
+  (cond ((member "-l" args) `((@@ (guix build bournish) 
wc-l-command-implementation) ,@(delete "-l" args)))
+        ((member "-c" args) `((@@ (guix build bournish) 
wc-c-command-implementation) ,@(delete "-c" args)))
+        (else `((@@ (guix build bournish) wc-command-implementation) ,@args))))
+
 (define (help-command . _)
   (display "\
 Hello, this is Bournish, a minimal Bourne-like shell in Guile!
@@ -129,7 +184,8 @@ commands such as 'ls' and 'cd'; it lacks globbing, 
pipes---everything.\n"))
     ("help"   ,help-command)
     ("ls"     ,ls-command)
     ("which"  ,which-command)
-    ("cat"    ,cat-command)))
+    ("cat"    ,cat-command)
+    ("wc"     ,wc-commands)))
 
 (define (read-bournish port env)
   "Read a Bournish expression from PORT, and return the corresponding Scheme
-- 
2.8.4

Attachment: signature.asc
Description: PGP signature

Reply via email to