Repository navigation
Expand file tree
/
Copy pathqueue.rugo
More file actions
87 lines (69 loc) · 1.66 KB
/
Copy pathqueue.rugo
File metadata and controls
87 lines (69 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Queue Module Examples
# Demonstrates producer-consumer, pipelines, bounded queues,
# timeout handling, and streaming results.
use "queue"
use "conv"
# ── Pattern 1: Basic producer-consumer ──────────────────
puts "--- Producer-Consumer ---"
q = queue.new()
spawn
for word in ["hello", "world", "from", "queue"]
q.push(word)
end
q.close()
end
q.each(fn(item)
puts item
end)
# ── Pattern 2: Pipeline (multi-stage) ──────────────────
puts ""
puts "--- Pipeline ---"
input = queue.new()
output = queue.new()
# Stage 1: generate numbers
spawn
for i in [1, 2, 3, 4, 5]
input.push(i)
end
input.close()
end
# Stage 2: multiply by 10
spawn
input.each(fn(n)
output.push(n * 10)
end)
output.close()
end
# Stage 3: consume
output.each(fn(result)
puts conv.to_s(result)
end)
# ── Pattern 3: Bounded queue with backpressure ──────────
puts ""
puts "--- Bounded Queue ---"
q = queue.new(2)
spawn
for i in [1, 2, 3, 4]
q.push(i)
end
q.close()
end
q.each(fn(item)
puts "processed: #{conv.to_s(item)}"
end)
# ── Pattern 4: Pop with timeout ─────────────────────────
puts ""
puts "--- Timeout ---"
q = queue.new()
result = try q.pop(1) or "timed out after 1s"
puts result
# ── Pattern 5: Properties ──────────────────────────────
puts ""
puts "--- Properties ---"
q = queue.new(10)
q.push("a")
q.push("b")
puts "size: #{conv.to_s(q.size)}"
puts "closed: #{conv.to_s(q.closed)}"
q.close()
puts "closed: #{conv.to_s(q.closed)}"