Created
July 15, 2022 19:37
-
-
Save SpexGuy/953e5780cd2d2c524cba6a79f13076e6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
const Channel = struct { | |
value: u32, | |
frame: anyframe, | |
}; | |
fn generate(channel: *Channel) void { | |
suspend { channel.frame = @frame(); } | |
var i: u32 = 2; | |
while (true) : (i +%= 1) { | |
channel.value = i; | |
suspend {} | |
} | |
} | |
fn filter(out_channel: *Channel, in_channel: *Channel, prime: u32) void { | |
suspend { out_channel.frame = @frame(); } | |
while (true) { | |
resume in_channel.frame; | |
if (in_channel.value % prime != 0) { | |
out_channel.value = in_channel.value; | |
suspend {} | |
} | |
} | |
} | |
pub fn main() !void { | |
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | |
defer arena.deinit(); | |
const args = try std.process.argsAlloc(arena.allocator()); | |
const n = if (args.len > 1) | |
try std.fmt.parseInt(usize, args[1], 10) | |
else | |
100; | |
var ch = try arena.allocator().create(Channel); | |
_ = async generate(ch); | |
var i: u32 = 0; | |
while (i < n) : (i += 1) { | |
resume ch.frame; | |
const prime = ch.value; | |
std.debug.print("{}\n", .{prime}); | |
if (i >= n - 1) break; | |
const ch1 = try arena.allocator().create(Channel); | |
const frame = try arena.allocator().create(@Frame(filter)); | |
frame.* = async filter(ch1, ch, prime); | |
ch = ch1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment