Hello all! I'd like a little help with some bash style scripting that I would like to do in my gambit repl. How can I do in scheme what I would do in bash like f1 < input-file | f2, where f1 and f2 are 2 scheme functions reading and writing to stdin and stdout respectively. I know that the < input-file part is done with (with-input-from-file input-file (lambda () f1) put it is the pipe part that remains obscur to me... Thanks! David
Afficher les réponses par date
David St-Hilaire <sthilaid@iro.umontreal.ca> writes:
Hello all!
I'd like a little help with some bash style scripting that I would like to do in my gambit repl.
How can I do in scheme what I would do in bash like f1 < input-file | f2, where f1 and f2 are 2 scheme functions reading and writing to stdin and stdout respectively.
I know that the < input-file part is done with (with-input-from-file input-file (lambda () f1) put it is the pipe part that remains obscur to me...
(shell-command "...") FYI this returns the exit status of the program, not its output. Joel -- Joel J. Adamson Biostatistician Pediatric Psychopharmacology Research Unit Massachusetts General Hospital Boston, MA 02114 (617) 643-1432 (303) 880-3109 Public key: http://pgp.mit.edu http://www.unc.edu/~adamsonj The information transmitted in this electronic communication is intended only for the person or entity to whom it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this information in error, please contact the Compliance HelpLine at 800-856-1983 and properly dispose of this information.
On 23-Apr-08, at 11:26 AM, David St-Hilaire wrote:
Hello all!
I'd like a little help with some bash style scripting that I would like to do in my gambit repl.
How can I do in scheme what I would do in bash like f1 < input-file | f2, where f1 and f2 are 2 scheme functions reading and writing to stdin and stdout respectively.
I know that the < input-file part is done with (with-input-from-file input-file (lambda () f1) put it is the pipe part that remains obscur to me...
You don't need to create files, or execute shell commands. It can all be done within Gambit using threads and "pipe" string ports: (define (producer) (for-each (lambda (x) (thread-sleep! 0.5) ;; simulate heavy computation (pretty-print (* x x x)) (force-output)) '(1 2 3)) (if (not (tty? (current-output-port))) (close-output-port (current-output-port)))) (define (consumer) (let loop () (let ((x (read))) (if (not (eof-object? x)) (begin (pretty-print (sqrt x)) (force-output) (loop))))) (if (not (tty? (current-output-port))) (close-output-port (current-output-port)))) (define (pipe p c) (receive (out in) (open-string-pipe '(direction: output)) (let ((t (thread-start! (make-thread (lambda () (with-output-to-port out p)))))) (with-input-from-port in c)))) (display "------------------------\n") ;; try it with producer outputting to console (producer) (display "------------------------\n") ;; now, pipe producer's output to consumer's input (pipe producer consumer) (display "------------------------\n") ;; a more complex pipe... (pipe (lambda () (pipe producer consumer)) consumer)
Thank you! That is exactly what I was looking for! David Marc Feeley wrote:
On 23-Apr-08, at 11:26 AM, David St-Hilaire wrote:
Hello all!
I'd like a little help with some bash style scripting that I would like to do in my gambit repl.
How can I do in scheme what I would do in bash like f1 < input-file | f2, where f1 and f2 are 2 scheme functions reading and writing to stdin and stdout respectively.
I know that the < input-file part is done with (with-input-from-file input-file (lambda () f1) put it is the pipe part that remains obscur to me...
You don't need to create files, or execute shell commands. It can all be done within Gambit using threads and "pipe" string ports:
(define (producer) (for-each (lambda (x) (thread-sleep! 0.5) ;; simulate heavy computation (pretty-print (* x x x)) (force-output)) '(1 2 3)) (if (not (tty? (current-output-port))) (close-output-port (current-output-port))))
(define (consumer) (let loop () (let ((x (read))) (if (not (eof-object? x)) (begin (pretty-print (sqrt x)) (force-output) (loop))))) (if (not (tty? (current-output-port))) (close-output-port (current-output-port))))
(define (pipe p c) (receive (out in) (open-string-pipe '(direction: output)) (let ((t (thread-start! (make-thread (lambda () (with-output-to-port out p)))))) (with-input-from-port in c))))
(display "------------------------\n")
;; try it with producer outputting to console (producer)
(display "------------------------\n")
;; now, pipe producer's output to consumer's input (pipe producer consumer)
(display "------------------------\n")
;; a more complex pipe... (pipe (lambda () (pipe producer consumer)) consumer)
participants (3)
-
David St-Hilaire -
jadamson@partners.org -
Marc Feeley