66 lines
1.6 KiB
Plaintext
66 lines
1.6 KiB
Plaintext
sendOne(value: Int, target: Channel<Int>) () { send(target, value) }
|
|
|
|
produce(values: List<Int>, target: Channel<Int>) () {
|
|
each(sendOne, values, target)
|
|
close(target)
|
|
}
|
|
|
|
receiveOne(position: Int, source: Channel<Int>) (value: Int?) {
|
|
value = receive(source)
|
|
}
|
|
|
|
channelRoundtrip(values: List<Int>, capacity: Int) (received: List<Int?>, ended: Bool) {
|
|
target = channel(Int, capacity)
|
|
pending = run(produce(values, target))
|
|
received = each(receiveOne, values, target)
|
|
wait(pending)
|
|
ended = receive(target) == null
|
|
}
|
|
|
|
add(value: Int, state: Int) (next: Int) pure { next = state + value }
|
|
|
|
until(value: Int, state: Int) (next: Int, stop: Bool) pure {
|
|
next = state + value
|
|
stop = value >= 8
|
|
}
|
|
|
|
streamPull(values: List<Int>) (total: Int, after: Int) {
|
|
source = stream(values)
|
|
alias = source
|
|
total = reduce(add, source, 0)
|
|
after = reduce(add, alias, 0)
|
|
}
|
|
|
|
streamPrefix(values: List<Int>) (total: Int, after: Int) {
|
|
source = stream(values)
|
|
alias = source
|
|
total = reduceUntil(until, source, 0)
|
|
after = reduce(add, alias, 0)
|
|
}
|
|
|
|
streamBoundary() (prefix: Int, unpulled: Int?, after: Int) {
|
|
target = channel(Int, 3)
|
|
send(target, 7)
|
|
send(target, 8)
|
|
send(target, 9)
|
|
source = stream(target)
|
|
alias = source
|
|
prefix = reduceUntil(until, source, 0)
|
|
unpulled = receive(target)
|
|
send(target, 10)
|
|
close(target)
|
|
after = reduce(add, alias, 0)
|
|
}
|
|
|
|
consume(source: Channel<Int>) (total: Int) {
|
|
total = reduce(add, stream(source), 0)
|
|
}
|
|
|
|
parallelBounded(values: List<Int>, limit: Int) (total: Int) {
|
|
target = channel(Int, 1)
|
|
pending = run(consume(target))
|
|
parallelEach(sendOne, values, limit, target)
|
|
close(target)
|
|
total = wait(pending)
|
|
}
|