Channel.values
func Channel.values[T](c: Channel[T], ctx: Context | null = null): Seq[T]Channel.values returns a sequence of every value received from c until it is closed and drained, or until ctx is done. It receives one value per yield, so values left after a break stay in c, and it tells a null value from a close. A ctx done already takes nothing.
Details
- The loop receives each value, so a value it yields is gone from the channel. It receives one value per step and nothing ahead, so the values left after a
breakstay in the channel. - A
ctxthat is done already yields nothing, even from a channel that holds values. - The sequence ends only when the channel is closed and drained, or when
ctxis done. A loop over a channel nobody closes, without a context, waits until the run is cancelled. - Unlike
Channel.receive, the sequence tells anullvalue from a close, so aChannel[T | null]yields itsnullvalues. - When
ctxends the loop, nothing tells it apart from a close. AskContext.isDone(ctx)afterward.
Basic examples
Print every value a goroutine sends, until it closes the channel:
let numbers: Channel[int] = Channel.new()
go {
defer { numbers.close() }
for n in [1, 2, 3] {
numbers.send(n)
}
}
for n in Channel.values(numbers) {
println(n)
}1
2
3A closed buffered channel still yields what it holds:
let words: Channel[string] = Channel.new(capacity: 2)
words.send("ant")
words.send("bee")
words.close()
for w in Channel.values(words) {
println(w)
}ant
beeScope
A closed, empty channel yields nothing, so the loop body never runs:
let empty: Channel[int] = Channel.new()
empty.close()
for n in empty.values() {
println(n)
}
println("done")doneA channel of a union yields each value with its own case. Here null is an ordinary value, not the end of the channel:
let readings: Channel[int | null] = Channel.new(capacity: 3)
readings.send(20)
readings.send(null)
readings.send(22)
readings.close()
for r in readings.values() {
println(r ?? "missing")
}20
missing
22Records and tagged unions travel as they are:
type Job = { id: int, name: string }
type Stop = { reason: string }
let events: Channel[Job | Stop] = Channel.new(capacity: 3)
events.send(Job{ id: 1, name: "build" })
events.send(Job{ id: 2, name: "test" })
events.send(Stop{ reason: "shutdown" })
events.close()
for e in events.values() {
if e is Job {
println("job ${e.id}: ${e.name}")
} else {
println("stop: ${e.reason}")
}
}job 1: build
job 2: test
stop: shutdownThe result is a Seq[T], so the Iter functions chain on it in a pipe:
let c: Channel[int] = Channel.new()
go {
defer { c.close() }
for n in Iter.range(1, 10) {
c.send(n)
}
}
let evenSquares = c.values()
| Iter.filter({ x % 2 == 0 })
| Iter.map({ x * x })
| Iter.toArray()
println(evenSquares)[4, 16, 36, 64]Named arguments
ctx
With ctx:, the sequence also ends when the context is done. Here the channel holds two values and is never closed, so the timeout ends the loop, and Context.reason tells why:
let c: Channel[string] = Channel.new(capacity: 2)
c.send("first")
c.send("second")
let ctx = Context.new(50ms)
for s in c.values(ctx: ctx) {
println(s)
}
println(ctx.reason())first
second
timeoutApplications
Collect the results of several workers. Each worker sends on one channel, and a goroutine closes it when the WaitGroup reaches zero, which ends the loop:
let results: Channel[string] = Channel.new()
let wg = WaitGroup.new()
for name in ["ann", "bob", "cid"] {
wg.add(1)
go {
defer { wg.done() }
results.send(name.toUpper())
}
}
go {
wg.wait()
results.close()
}
let names = results.values() | Iter.toArray() | Array.sort()
println(names)["ANN", "BOB", "CID"]Stop at the first value that matches:
let queue: Channel[int] = Channel.new(capacity: 5)
for n in [3, 8, 12, 5, 20] {
queue.send(n)
}
queue.close()
var found: int | null = null
for n in queue.values() {
if n > 10 {
found = n
break
}
}
println(found)12Sum a stream of numbers from a producer that reads them from text:
func produce(text: string, out: Channel[int]) {
defer { out.close() }
for line in text.split("\n") {
let n = line.trim().toInt()
if n == null { continue }
out.send(n)
}
}
let numbers: Channel[int] = Channel.new()
go { produce("4\n x\n10\n6", numbers) }
var total = 0
for n in numbers.values() {
total += n
}
println(total)20Properties and relations
For a channel whose element type excludes null, the sequence yields the same values as calling Channel.receive until it returns null:
func fill(): Channel[int] {
let c: Channel[int] = Channel.new(capacity: 3)
for n in [1, 2, 3] {
c.send(n)
}
c.close()
c
}
let viaValues = fill().values() | Iter.toArray()
let c = fill()
let viaReceive: Array[int] = []
while true {
let n = c.receive() ?? break
viaReceive.push(n)
}
assert(viaValues == viaReceive)The loop takes the values out, so a second run over the same closed channel yields nothing:
let c: Channel[int] = Channel.new(capacity: 2)
c.send(1)
c.send(2)
c.close()
let vs = c.values()
assert((vs | Iter.toArray()) == [1, 2])
assert((vs | Iter.toArray()).isEmpty())
assert(c.isClosed())Possible issues
The producer must close the channel. Without close and without a context, the loop waits for a value that never comes, until the run is cancelled. Close with defer as the producer's first line, so a return or a panic in the producer still ends the loop:
let lines: Channel[string] = Channel.new()
go {
defer { lines.close() }
lines.send("only line")
}
for line in lines.values() {
println(line)
}only lineA ticker is never closed, so a loop over its ticks ends only by break, a context, or Iter.take:
let ticks = Time.ticker(10ms)
let first = ticks.values() | Iter.take(3) | Iter.toArray()
println(first.length())3See also
Channel.receive, Channel.close, Channel.new, Iter.toArray, Iter.take, Parallel.each, Time.ticker, for, go
