September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View file

@ -0,0 +1,17 @@
(define (reader)
(for ([line (in-lines (open-input-file "input.txt"))])
(thread-send printer-thread line))
(thread-send printer-thread eof)
(printf "Number of lines: ~a\n" (thread-receive)))
(define (printer)
(thread-send reader-thread
(for/sum ([line (in-producer thread-receive eof)])
(displayln line)
1)))
(define printer-thread (thread printer))
(define reader-thread (thread reader))
(for-each thread-wait
(list printer-thread reader-thread))

View file

@ -0,0 +1,19 @@
(define (reader out-ch result-ch)
(for ([line (in-lines (open-input-file "input.txt"))])
(channel-put out-ch line))
(channel-put out-ch eof)
(printf "Number of lines: ~a\n" (channel-get result-ch)))
(define (printer in-ch result-ch)
(channel-put result-ch
(for/sum ([line (in-producer channel-get eof in-ch)])
(displayln line)
1)))
(define lines-ch (make-channel))
(define result-ch (make-channel))
(define printer-thread (thread (lambda () (printer lines-ch result-ch))))
(define reader-thread (thread (lambda () (reader lines-ch result-ch))))
(for-each thread-wait
(list printer-thread reader-thread))

View file

@ -1,29 +0,0 @@
(define reader->printer-channel (make-channel))
(define printer->reader-channel (make-channel))
(define (sync-line-counter filename)
(define (reader)
(define file-port (open-input-file filename))
(let loop ([line (read-line file-port)])
(when (not (eof-object? line))
(begin
(channel-put reader->printer-channel line)
(loop (read-line file-port)))))
(channel-put reader->printer-channel eof)
(let ([num-lines (channel-get printer->reader-channel)])
(printf "Number of lines printed = ~a~%" num-lines)))
(define (printer)
(define count 0)
(let loop ([line (channel-get reader->printer-channel)])
(when (not (eof-object? line))
(begin
(printf "~a~%" line)
(set! count (add1 count))
(loop (channel-get reader->printer-channel)))))
(channel-put printer->reader-channel count))
(thread reader)
(thread printer))
(sync-line-counter "input.txt")