From e47dccf2452ca68fb005a2d4727ea15a5e336bb5 Mon Sep 17 00:00:00 2001 From: Dave Gauer Date: Thu, 27 May 2021 19:04:11 -0400 Subject: [PATCH] Add ex089 Async 6 --- TEMP_TODO | 4 --- build.zig | 4 +++ exercises/089_async6.zig | 53 ++++++++++++++++++++++++++++++++ patches/patches/089_async6.patch | 6 ++++ 4 files changed, 63 insertions(+), 4 deletions(-) delete mode 100644 TEMP_TODO create mode 100644 exercises/089_async6.zig create mode 100644 patches/patches/089_async6.patch 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;