aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Gauer <dave@ratfactor.com>2021-05-12 21:25:48 -0400
committerDave Gauer <dave@ratfactor.com>2021-05-12 21:25:48 -0400
commit5fec2602cff707f5732ac5d821e8bb18ff69ed79 (patch)
tree9c313b7d437ae72eccdbc504b25d287ef5af8a3d
parentd8dddd128fdb3e7d0f36c60c522a56cbcd64bc22 (diff)
downloadziglings-5fec2602cff707f5732ac5d821e8bb18ff69ed79.tar.gz
ziglings-5fec2602cff707f5732ac5d821e8bb18ff69ed79.tar.bz2
ziglings-5fec2602cff707f5732ac5d821e8bb18ff69ed79.tar.xz
ziglings-5fec2602cff707f5732ac5d821e8bb18ff69ed79.zip
add ex086 async 3
-rw-r--r--build.zig4
-rw-r--r--exercises/086_async3.zig29
-rw-r--r--patches/patches/086_async3.patch9
3 files changed, 42 insertions, 0 deletions
diff --git a/build.zig b/build.zig
index 707c212..28e4682 100644
--- a/build.zig
+++ b/build.zig
@@ -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.
diff --git a/exercises/086_async3.zig b/exercises/086_async3.zig
new file mode 100644
index 0000000..ae5a9a6
--- /dev/null
+++ b/exercises/086_async3.zig
@@ -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;
+ }
+}
diff --git a/patches/patches/086_async3.patch b/patches/patches/086_async3.patch
new file mode 100644
index 0000000..2e52b2b
--- /dev/null
+++ b/patches/patches/086_async3.patch
@@ -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;