unofficial mirror of guix-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Efraim Flashner <efraim@flashner.co.il>
To: guix-devel@gnu.org,
	Ludovic =?utf-8?Q?Court=C3=A8s?= <ludo@gnu.org>,
	"Eric Bavier" <ericbavier@openmailbox.org>
Subject: Re: Adding wc to Bournish
Date: Thu, 26 May 2016 22:27:55 +0300	[thread overview]
Message-ID: <20160526192755.GB28047@debian-netbook> (raw)
In-Reply-To: <20160524184720.GA27449@debian-netbook>


[-- Attachment #1.1: Type: text/plain, Size: 812 bytes --]

Here's where I'm currently at with 'wc'. Currently I'm getting an error
about having the wrong number of arguments for lambda (I assume)

;;; /home/efraim/workspace/guix/guix/build/bournish.scm:143:2: warning:
wrong number of arguments to `#<tree-il (lambda () (lambda-case (((lines
words chars) #f #f #f () (lines-24244 words-24245 chars-24246)) (apply
(toplevel format) (const #t) (const "~a ~a ~a ~a~%") (lexical lines
lines-24244) (lexical words words-24245) (lexical chars chars-24246)
(lexical file file-24242)))))>'

It's not ready as-is, but I wanted to show what I had so far :)

-- 
Efraim Flashner   <efraim@flashner.co.il>   אפרים פלשנר
GPG key = A28B F40C 3E55 1372 662D  14F7 41AA E7DC CA3D 8351
Confidentiality cannot be guaranteed on emails sent or received unencrypted

[-- Attachment #1.2: 0001-Bournish-Add-wc-command.patch --]
[-- Type: text/plain, Size: 3656 bytes --]

From f909c39530c727f414c8c827bdbccb2285ad1cc5 Mon Sep 17 00:00:00 2001
From: Efraim Flashner <efraim@flashner.co.il>
Date: Sun, 22 May 2016 14:56:06 +0300
Subject: [PATCH] Bournish: Add `wc' command.

* guix/build/bournish.scm (file-size, wc-c-command, wc-w-command,
wc-l-command, wc-command): New variables.
(%commands): Add wc command.
---
 guix/build/bournish.scm | 56 ++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 55 insertions(+), 1 deletion(-)

diff --git a/guix/build/bournish.scm b/guix/build/bournish.scm
index 4022796..d133796 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 <ludo@gnu.org>
+;;; Copyright © 2016 Efraim Flashner <efraim@flashner.co.il>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -21,11 +22,13 @@
   #:use-module (system base compile)
   #:use-module (system repl command)
   #:use-module (system repl common)
+  ;#:use-module (ice-9 streams)
   #:use-module (ice-9 rdelim)
   #:use-module (ice-9 match)
   #:use-module (ice-9 ftw)
   #:use-module (srfi srfi-1)
   #:use-module (srfi srfi-26)
+  #:use-module (srfi srfi-41)
   #:export (%bournish-language))
 
 ;;; Commentary:
@@ -103,6 +106,56 @@ characters."
        ((@ (guix build utils) dump-port) port (current-output-port))
        *unspecified*)))
 
+(define (file-size file)
+    (stat:size (stat file)))
+
+(define (wc-c-command file)
+  ;; Faster when only `wc -c' is called
+  (file-size file))
+
+(define (wc-l-command file)
+  ;; Faster when only `wc -l' is called
+  (stream-length
+    (stream-filter
+      (lambda (chr)
+        (char=? chr #\newline))
+      (port->stream (open-file file "r")))))
+
+;(define (wc-l-command file)
+;  (call-with-input-file file
+;    (lambda (port)
+
+(define (lines+chars port)
+  ;; Return the number of lines and number of chars read from PORT.
+  (let loop ((lines 0) (words 0) (chars 0))
+    (match (read-char port)
+      ((? eof-object?)              ;done!
+       (values lines words chars))
+      (#\newline                    ;recurse
+       (loop (1+ lines) (1+ words) (1+ chars)))
+      ((and (char-set<= ? char-set:blank) ; need to fix/replace the '?'
+            (char-set<= (peek-char port) char-set:graphic))
+       (loop lines (1+ words) (1+ chars)))
+      (_                            ;recurse
+       (loop lines words (1+ chars))))))
+
+(define (wc-command file)
+  ((lambda (lines words chars) ; lambda doesn't like 3 variables
+     (format #t "~a ~a ~a ~a~%" lines words chars file))
+   (call-with-input-file file lines+chars)))
+
+;  let-values is undefined
+;  (let-values (((lines words chars)
+;    (call-with-input-file file lines+chars)))
+;      (format #t "~a ~a ~a ~a~%" lines chars words file)))
+
+;(define (wc-command file)
+;  (let ((wc-l (wc-l-command file))
+;        (wc-w (wc-w-command file))
+;        (wc-c (wc-c-command file)))
+;    (format #t "~{~a ~}\n"
+;            (list wc-l wc-w wc-c file))))
+
 (define (help-command . _)
   (display "\
 Hello, this is Bournish, a minimal Bourne-like shell in Guile!
@@ -129,7 +182,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-command)))
 
 (define (read-bournish port env)
   "Read a Bournish expression from PORT, and return the corresponding Scheme
-- 
2.8.3


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

  parent reply	other threads:[~2016-05-26 19:28 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-05-24 18:47 Adding wc to Bournish Efraim Flashner
2016-05-25  9:03 ` Ricardo Wurmus
2016-05-25  9:43   ` Efraim Flashner
2016-05-25  9:26 ` Ricardo Wurmus
2016-05-25 10:05   ` Efraim Flashner
2016-05-26  8:46 ` Ludovic Courtès
2016-05-26 17:50   ` Efraim Flashner
2016-05-26 19:27 ` Efraim Flashner [this message]
2016-05-27 13:37   ` Eric Bavier
2016-05-27 15:28     ` Taylan Ulrich Bayırlı/Kammer
2016-05-27 15:32       ` Thompson, David
2016-06-05 12:40 ` Efraim Flashner
2016-06-05 20:37   ` Ludovic Courtès
2016-06-07  7:41     ` Efraim Flashner
2016-06-08 15:43       ` Ludovic Courtès
2016-06-14  9:27         ` Efraim Flashner
2016-06-14  9:57           ` Ricardo Wurmus
2016-06-14 10:20             ` Efraim Flashner
2016-06-14 10:50               ` Efraim Flashner
2016-06-14 11:08                 ` Ricardo Wurmus
2016-06-15 13:56                 ` Ludovic Courtès
2016-06-15 20:28                   ` Efraim Flashner
2016-06-23  8:34                     ` Ludovic Courtès

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://guix.gnu.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160526192755.GB28047@debian-netbook \
    --to=efraim@flashner.co.il \
    --cc=ericbavier@openmailbox.org \
    --cc=guix-devel@gnu.org \
    --cc=ludo@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).