[racket] [scsh vs Racket] examples

60 views
Skip to first unread message

Vlad Kozin

unread,
Oct 3, 2013, 5:12:13 PM10/3/13
to Racket Users
Hi.

Here's my Racket implementation of:

sh:> md5 -q README.md > README.md.md5

scsh:> (run (md5 -q README.md) (> README.md.md5))

 (begin
   (define md5 (find-executable-path "md5" #f))
   (define infile "README.md")
   (define outfile "README.md.md5")   
   (parameterize 
       ([current-custodian (make-custodian)])
     (current-subprocess-custodian-mode #f)
     (let*-values
         ([(to) (open-output-file outfile #:mode 'text #:exists 'replace)]
          [(sub in out err) (subprocess to #f 'stdout md5 "-q" infile)])
       (begin
         (subprocess-wait sub)
         (custodian-shutdown-all (current-custodian))
         (subprocess-status sub))))))

I could of course cheat:
(system "md5 -q README.md > README.md.md5")

Can my code be improved? Source on github if you want to play with it.
I hope to add more examples.
---

Sam Tobin-Hochstadt

unread,
Oct 3, 2013, 5:37:23 PM10/3/13
to Vlad Kozin, Racket Users
On Thu, Oct 3, 2013 at 5:12 PM, Vlad Kozin <vladile...@gmail.com> wrote:
>
> Can my code be improved? Source on github if you want to play with it.
> I hope to add more examples.


Overall, this looks right.

I sent you a pull request with some stylistic changes.

Sam
____________________
Racket Users list:
http://lists.racket-lang.org/users

Vlad Kozin

unread,
Oct 3, 2013, 6:14:32 PM10/3/13
to Sam Tobin-Hochstadt, Racket Users
Got it. Thanks Sam. 

Have a question that I just forgot to ask. There's a reason I have (let*-values ..) instead of definitions:

(let*-values
         ([(to) (open-output-file outfile #:mode 'text #:exists 'replace)]
          [(sub in out err) (subprocess to #f 'stdout md5 "-q" infile)])

since ports are let-bound wouldn't they be garbage-collected once I'm out of let-body making all custodian machinery in my code redundant? Its probably somewhere in documentation, but at this point I'm overwhelmed with information :)

cheers
---

Sam Tobin-Hochstadt

unread,
Oct 3, 2013, 6:20:37 PM10/3/13
to Vlad Kozin, Racket Users
On Thu, Oct 3, 2013 at 6:14 PM, Vlad Kozin <vladile...@gmail.com> wrote:
> Got it. Thanks Sam.
>
> Have a question that I just forgot to ask. There's a reason I have
> (let*-values ..) instead of definitions:

There isn't a difference between the code you wrote an what I wrote --
using `define` inside another context just gets transformed into `let`
or `let-values`.

Vlad Kozin

unread,
Oct 3, 2013, 6:32:33 PM10/3/13
to Sam Tobin-Hochstadt, Racket Users
got it. But just to confirm port-values in Racket are subject to gc, right?
---

Sam Tobin-Hochstadt

unread,
Oct 4, 2013, 11:14:50 AM10/4/13
to Vlad Kozin, Racket Users
The port value is GC'ed, but you have to explicitly close it, or use
`custodian-shutdown-all`, to free the underlying resources. See the
docs here: http://www.cs.utah.edu/plt/snapshots/current/doc/reference/ports.html

Sam

Vlad Kozin

unread,
Oct 4, 2013, 12:49:30 PM10/4/13
to Sam Tobin-Hochstadt, Racket Users, Yuhao Dong
understood. Thank you!

---
Reply all
Reply to author
Forward
0 new messages