From mboxrd@z Thu Jan 1 00:00:00 1970 From: Efraim Flashner Subject: Re: Adding wc to Bournish Date: Thu, 26 May 2016 20:50:09 +0300 Message-ID: <20160526175009.GA28047@debian-netbook> References: <20160524184720.GA27449@debian-netbook> <87wpmhi4v6.fsf@gnu.org> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="HcAYCG3uE/tztfnV" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:47470) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b5zQR-0005KB-Ak for guix-devel@gnu.org; Thu, 26 May 2016 13:50:28 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b5zQN-0006g8-7K for guix-devel@gnu.org; Thu, 26 May 2016 13:50:27 -0400 Content-Disposition: inline In-Reply-To: <87wpmhi4v6.fsf@gnu.org> 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: Ludovic =?utf-8?Q?Court=C3=A8s?= Cc: guix-devel@gnu.org --HcAYCG3uE/tztfnV Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, May 26, 2016 at 10:46:21AM +0200, Ludovic Court=C3=A8s wrote: >=20 > As Ricardo suggests, you could use =E2=80=98port->stream=E2=80=99 and =E2= =80=98stream-fold=E2=80=99 to > iterate over the characters read from the port. I suspect that=E2=80=99d= be > rather slow though, at least on 2.0, so another option is something > like: >=20 > (define (lines+chars port) > ;; Return the number of lines and number of chars read from PORT. > (let loop ((lines 1) (chars 0)) > (match (read-char port) > ((? eof-object?) ;done! > (values lines port)) > (#\newline ;recurse > (loop (+ 1 lines) (+ 1 chars))) > (_ ;recurse > (loop lines (+ 1 chars)))))) >=20 > (define (wc-command file) > (let-values (((lines chars) > (call-with-input-file file lines+chars))) > (format #t "~a ~a ~a~%" lines chars file))) >=20 Are you suggesting just dropping the word count part of `wc'? I've been thinking about it, and the simplest way I can think of to describe a word is a space followed by a character, or, to use the char-sets from the guile manual, a character from char-set:whitespace followed by a character from char-set:graphic. I can compare (read-char port) and (peek-char port) to get a word count (possibly). > > +(define (wc-command file) > > + (if (and (file-exists? file) (access? file 4)) >=20 > This check is not needed and is subject to a race condition (=E2=80=9CTOC= TTOU=E2=80=9D); > just let =E2=80=98call-with-input-file=E2=80=99 error out if the file can= not be read. >=20 > Bonus point: catch =E2=80=98system-error=E2=80=99 exceptions and report t= he inability to > open the file in a nice user-friendly way (but really, don=E2=80=99t both= er > about it for now.) >=20 I'm still wrapping my head around the following part. My wife says when I work I scowl at the computer a lot and mutter :) >=20 > Remember that Bournish is a compiler that compiles Bash to Scheme. > So we must distinguish the support functions that are used at run time, > such as =E2=80=98ls-command-implementation=E2=80=99, from what the Scheme= code that the > compiler emits (compile time). >=20 > In the case of =E2=80=98ls=E2=80=99, when the compiler encounters =E2=80= =98ls=E2=80=99 in the input, it > emits this code: >=20 > ((@@ (guix build bournish) ls-command-implementation)) >=20 > =E2=80=98ls-command-implementation=E2=80=99 is the implementation that is= called when we > run the compiled program. >=20 > Thus, you must similarly distinguish those two stages by providing: >=20 > 1. A =E2=80=98wc-command-implementation=E2=80=99 procedure that impleme= nts =E2=80=98wc=E2=80=99; >=20 > 2. A =E2=80=98wc-command=E2=80=99 procedure that emits the code that ca= lls > =E2=80=98wc-command-implementation=E2=80=99; so something like: >=20 > (define (wc-command args) > `((@@ (guix build bournish) wc-command-implementation) > ,@args)) >=20 > Better yet, =E2=80=98wc-command=E2=80=99 could check for the presenc= e of =E2=80=9C-l=E2=80=9D or > =E2=80=9C-c=E2=80=9D at compile time and emit a call to the right th= ing. I checked with coreutil's 'wc', and it emits in its particular order whether you call 'wc -l -c' or 'wc -lc' or 'wc -cl' >=20 > HTH! >=20 > Ludo=E2=80=99. --=20 Efraim Flashner =D7=90=D7=A4=D7=A8=D7=99=D7=9D = =D7=A4=D7=9C=D7=A9=D7=A0=D7=A8 GPG key =3D A28B F40C 3E55 1372 662D 14F7 41AA E7DC CA3D 8351 Confidentiality cannot be guaranteed on emails sent or received unencrypted --HcAYCG3uE/tztfnV Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iQIcBAEBCgAGBQJXRzdNAAoJEPTB05F+rO6T8VQP/1nl9FfxVmtDtf8NF9kTynrd +Ye7tIwZ1q4NSCV3Y7wwkrV+u2JldwwcqxtUOzSb309f2UtUQscIRG75Hu4QAq9M O1V3AdS84N6y3PJeOaJr+d/B5NyFhD/jPMGM9Ik4v8+c35LBg2XvGjVoI8skR+RJ U2qydCozsA8Nl+iR6sQ9WtxsN0MtydNX/Lc4/dCZ0z1dXn99y/M6w4HUIQgQFbvY JIARy7YQjvZFwwI2uU07jUaSjCVIydXio3Fm42lX3ve+DjRlxKMTU9N+LyfgdbJx K4ZsAU+/wYKDYD5wDaMaShWCH5dDZKgFyt2c8UoHVmcOlKN30XVzn7M+TfATeQkF k9RswFMUTxh7mGBnYPr7XmXiZao4cLLzp85QVC8XtALjF9Wy7ZC/YL2SQZ7E7A8K WXKTOsRyMCYz/V9WmnEfN9MEV/8ZjqWgOzxysR/FW7sSLuIfkSGm8nUMr9UWRCDb EMe3qCRzCygh/NBslXeRVB3s2xfm6/JHXjcAuuH5/t7ZG+M1indRDNKcLCeO4aKJ 6VWomvZF/pM7q9fP0/yOur+DTw+abbYmiREEaZx+ZNVA8YevqBrLgEQGXD1A+qEV VJQO0mTxw6qi9gJfYJ9L2J14UjZI+fkhcKcq3EleLoA7g2wN5NbpygNEEEeAyrPN fRahSNPyOfZfa8R0I6d/ =csoV -----END PGP SIGNATURE----- --HcAYCG3uE/tztfnV--