aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Gauer <dave@ratfactor.com>2021-05-27 19:04:11 -0400
committerDave Gauer <dave@ratfactor.com>2021-05-27 19:04:11 -0400
commitd09ae39714bbca6e12c67939509a36faa6a76198 (patch)
treec3154738af086fe007f3f24b51cbbe8afef70ed1
parent473b36b8736e38f697601910cb7edfa903f7b5e3 (diff)
downloadziglings-d09ae39714bbca6e12c67939509a36faa6a76198.tar.gz
ziglings-d09ae39714bbca6e12c67939509a36faa6a76198.tar.bz2
ziglings-d09ae39714bbca6e12c67939509a36faa6a76198.tar.xz
ziglings-d09ae39714bbca6e12c67939509a36faa6a76198.zip
Add ex089 Async 6
-rw-r--r--TEMP_TODO4
-rw-r--r--build.zig4
-rw-r--r--exercises/089_async6.zig53
-rw-r--r--patches/patches/089_async6.patch6
4 files changed, 63 insertions, 4 deletions
diff --git a/TEMP_TODO b/TEMP_TODO
deleted file mode 100644
index 9b74397..0000000
--- a/TEMP_TODO
+++ /dev/null
@@ -1,4 +0,0 @@
-071 - inline for - loop through struct fields
-072 - inline while (see lib/std/fmt.zig)
-073 - comptime block
-074 - quiz 8 - revisit 058_quiz7 and make those connections programatically?
diff --git a/build.zig b/build.zig
index 043bb2c..fe65a96 100644
--- a/build.zig
+++ b/build.zig
@@ -434,6 +434,10 @@ const exercises = [_]Exercise{
.main_file = "088_async5.zig",
.output = "Example Title.",
},
+ .{
+ .main_file = "089_async6.zig",
+ .output = ".com: Example Title, .org: Example Title.",
+ },
};
/// Check the zig version to make sure it can compile the examples properly.
diff --git a/exercises/089_async6.zig b/exercises/089_async6.zig
new file mode 100644
index 0000000..d1681f7
--- /dev/null
+++ b/exercises/089_async6.zig
@@ -0,0 +1,53 @@
+//
+// The power and purpose of async/await becomes more apparent
+// when we do multiple things concurrently. Foo and Bar do not
+// depend on each other and can happen at the same time, but End
+// requires that they both be finished.
+//
+// +---------+
+// | Start |
+// +---------+
+// / \
+// / \
+// +---------+ +---------+
+// | Foo | | Bar |
+// +---------+ +---------+
+// \ /
+// \ /
+// +---------+
+// | End |
+// +---------+
+//
+// We can express this in Zig like so:
+//
+// fn foo() u32 { ... }
+// fn bar() u32 { ... }
+//
+// // Start
+//
+// var foo_frame = async foo();
+// var bar_frame = async bar();
+//
+// var foo_value = await foo_frame;
+// var bar_value = await bar_frame;
+//
+// // End
+//
+// Please await TWO page titles!
+//
+const print = @import("std").debug.print;
+
+pub fn main() void {
+ var com_frame = async getPageTitle("http://example.com");
+ var org_frame = async getPageTitle("http://example.org");
+
+ var com_title = com_frame;
+ var org_title = org_frame;
+
+ print(".com: {s}, .org: {s}.\n", .{com_title, org_title});
+}
+
+fn getPageTitle(url: []const u8) []const u8 {
+ // Please PRETEND this is actually making a network request.
+ return "Example Title";
+}
diff --git a/patches/patches/089_async6.patch b/patches/patches/089_async6.patch
new file mode 100644
index 0000000..ecbef19
--- /dev/null
+++ b/patches/patches/089_async6.patch
@@ -0,0 +1,6 @@
+44,45c44,45
+< var com_title = com_frame;
+< var org_title = org_frame;
+---
+> var com_title = await com_frame;
+> var org_title = await org_frame;