June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,30 @@
\
\ co.fs Coroutines by continuations.
\
\ * Circular Queue. Capacity is power of 2.
\
VARIABLE HEAD VARIABLE TAIL
128 CELLS CONSTANT CQ#
\ * align by queue capacity
HERE DUP
CQ# 1- INVERT AND CQ# +
SWAP - ALLOT
\
HERE CQ# ALLOT CONSTANT START
\
: ADJUST ( -- ) [ CQ# 1- ]L AND START + ;
: PUT ( n-- ) TAIL @ TUCK ! CELL+ ADJUST TAIL ! ;
: TAKE ( --n ) HEAD @ DUP @ SWAP CELL+ ADJUST HEAD ! ;
: 0CQ ( -- ) START DUP HEAD ! TAIL ! ; 0CQ
: NOEMPTY? ( --f ) HEAD @ TAIL @ <> ;
: ;CO ( -- ) TAKE >R ;
\
\ * COROUTINES LEXEME
\
: CO: ( -- ) R> PUT ; \ Register continuation as coroutine. Exit.
: CO ( -- ) R> PUT TAKE >R ; \ Co-route.
: GO ( -- ) BEGIN NOEMPTY? WHILE ;CO REPEAT ; \ :-)
\
\ * CHANNELS LEXEME
\
: CHAN? ( a--f ) 2@ XOR ;

View file

@ -0,0 +1,20 @@
function inputlines(txtfile, iochannel)
for line in readlines(txtfile)
Base.put!(iochannel, line)
end
Base.put!(iochannel, nothing)
println("The other task printed $(take!(iochannel)) lines.")
end
function outputlines(iochannel)
totallines = 0
while (line = Base.take!(iochannel)) != nothing
totallines += 1
println(line)
end
Base.put!(iochannel, totallines)
end
c = Channel(0)
@async inputlines("filename.txt", c)
outputlines(c)

View file

@ -0,0 +1,30 @@
// version 1.1.51
import java.util.concurrent.SynchronousQueue
import kotlin.concurrent.thread
import java.io.File
val queue = SynchronousQueue<String>()
const val EOT = "\u0004" // end of transmission
fun main(args: Array<String>) {
thread {
var count = 0
while (true) {
val line = queue.take()
if (line == EOT) {
queue.put(count.toString())
break
}
println(line)
count++
}
}
File("input.txt").forEachLine { line -> queue.put(line) }
queue.put(EOT)
val count = queue.take().toInt()
println("\nNumber of lines printed = $count")
}

View file

@ -0,0 +1,31 @@
use "files"
actor Main
let _env: Env // The environment contains stdout, so we save it here
new create(env: Env) =>
_env = env
let printer: Printer tag = Printer(env)
try
let path = FilePath(env.root as AmbientAuth, "input.txt")? // this may fail, hence the ?
let file = File.open(path)
for line in FileLines(file) do
printer(line) // sugar for "printer.apply(line)"
end
end
printer.done(this)
be finish(count: USize) =>
_env.out.print("Printed: " + count.string() + " lines")
actor Printer
let _env: Env
var _count: USize = 0
new create(env: Env) => _env = env
be apply(line: String) =>
_count = _count + 1
_env.out.print(line)
be done(main: Main tag) => main.finish(_count)