September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
|
|
@ -0,0 +1,28 @@
|
|||
// version 1.3.20 with kotlinx-coroutines-core version 1.1.1
|
||||
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.channels.sumBy
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import java.io.File
|
||||
|
||||
suspend fun main() {
|
||||
coroutineScope {
|
||||
val lines = Channel<String>()
|
||||
|
||||
val count = async {
|
||||
lines.sumBy { line ->
|
||||
println(line)
|
||||
1
|
||||
}
|
||||
}
|
||||
|
||||
File("input.txt").bufferedReader().useLines { text ->
|
||||
for (line in text) {
|
||||
lines.send(line)
|
||||
}
|
||||
}
|
||||
lines.close()
|
||||
println("\nNumber of lines printed = ${count.await()}")
|
||||
}
|
||||
}
|
||||
|
|
@ -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))
|
||||
|
|
@ -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))
|
||||
|
|
@ -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")
|
||||
Loading…
Add table
Add a link
Reference in a new issue