mirror of
https://codeberg.org/andyscott/ziglings.git
synced 2024-12-21 21:53:11 -05:00
add ex086 async 3
This commit is contained in:
parent
54c048b0a0
commit
3e5647d88c
3 changed files with 42 additions and 0 deletions
|
@ -422,6 +422,10 @@ const exercises = [_]Exercise{
|
|||
.main_file = "085_async2.zig",
|
||||
.output = "Hello async!",
|
||||
},
|
||||
.{
|
||||
.main_file = "086_async3.zig",
|
||||
.output = "5 4 3 2 1",
|
||||
},
|
||||
};
|
||||
|
||||
/// Check the zig version to make sure it can compile the examples properly.
|
||||
|
|
29
exercises/086_async3.zig
Normal file
29
exercises/086_async3.zig
Normal file
|
@ -0,0 +1,29 @@
|
|||
//
|
||||
// Because they can suspend and resume, async Zig functions are
|
||||
// an example of a more general programming concept called
|
||||
// "coroutines". One of the neat things about Zig async functions
|
||||
// is that they retain their state as they are suspended and
|
||||
// resumed.
|
||||
//
|
||||
// See if you can make this program print "5 4 3 2 1".
|
||||
//
|
||||
const print = @import("std").debug.print;
|
||||
|
||||
pub fn main() void {
|
||||
const n = 5;
|
||||
var foo_frame = async foo(n);
|
||||
|
||||
???
|
||||
|
||||
print("\n", .{});
|
||||
}
|
||||
|
||||
fn foo(countdown: u32) void {
|
||||
var current = countdown;
|
||||
|
||||
while (current > 0) {
|
||||
print("{} ", .{current});
|
||||
current -= 1;
|
||||
suspend;
|
||||
}
|
||||
}
|
9
patches/patches/086_async3.patch
Normal file
9
patches/patches/086_async3.patch
Normal file
|
@ -0,0 +1,9 @@
|
|||
16c16,21
|
||||
< ???
|
||||
---
|
||||
> // Silly solution. You can also use a loop.
|
||||
> resume foo_frame;
|
||||
> resume foo_frame;
|
||||
> resume foo_frame;
|
||||
> resume foo_frame;
|
||||
> resume foo_frame;
|
Loading…
Reference in a new issue