Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
37
Task/Synchronous-concurrency/Ol/synchronous-concurrency.ol
Normal file
37
Task/Synchronous-concurrency/Ol/synchronous-concurrency.ol
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
(import (owl parse))
|
||||
|
||||
(coroutine 'reader (lambda ()
|
||||
; lazy line-by-line file reader
|
||||
(define (not-a-newline x) (not (eq? x #\newline)))
|
||||
(define parser (let-parse*
|
||||
((line (greedy* (byte-if not-a-newline)))
|
||||
(newline (imm #\newline)))
|
||||
(bytes->string line)))
|
||||
(define file (file->bytestream "input.txt"))
|
||||
|
||||
(let loop ((in (try-parse parser file #false)))
|
||||
(cond
|
||||
((not in) ; file is ended
|
||||
(define envelope (wait-mail)) ; wait for a request
|
||||
(mail (ref envelope 1) #eof)) ; send an end-of-file to caller
|
||||
((pair? in) ; new line is read
|
||||
(define envelope (wait-mail)) ; wait for a request
|
||||
(mail (ref envelope 1) (car in)) ; send a line to caller
|
||||
(loop (try-parse parser (cdr in) #false)))
|
||||
(else ; just a lazy read, let's repeat
|
||||
(loop (force in)))))
|
||||
|
||||
(print "total lines read: " (await (mail 'writer #t)))
|
||||
))
|
||||
|
||||
(coroutine 'writer (lambda ()
|
||||
(let loop ((n 0))
|
||||
(define line (await (mail 'reader #t)))
|
||||
|
||||
(if (eof? line)
|
||||
then
|
||||
(define envelope (wait-mail)) ; wait for a request
|
||||
(mail (ref envelope 1) n) ; send a lines count to caller
|
||||
else
|
||||
(print "read line: " line)
|
||||
(loop (+ n 1))))))
|
||||
Loading…
Add table
Add a link
Reference in a new issue