From 5f7e9389d5a8d5829f9a7a7fe50153e3d82abf7f Mon Sep 17 00:00:00 2001 From: Dave Gauer Date: Sun, 28 Feb 2021 13:23:22 -0500 Subject: [PATCH] Added ex 48, additional comment on 46 --- README.md | 2 +- build.zig | 12 ++++- exercises/46_optionals2.zig | 14 +++++- exercises/48_methods2.zig | 68 +++++++++++++++++++++++++++++ patches/patches/46_optionals2.patch | 19 ++++++-- patches/patches/48_methods2.patch | 4 ++ 6 files changed, 112 insertions(+), 7 deletions(-) create mode 100644 exercises/48_methods2.zig create mode 100644 patches/patches/48_methods2.patch diff --git a/README.md b/README.md index f446cfe..de0a808 100644 --- a/README.md +++ b/README.md @@ -127,7 +127,7 @@ Planned exercises: * [x] Structs * [x] Pointers * [x] Optionals -* [ ] Struct methods +* [x] Struct methods * [ ] Slices * [ ] Multi pointers * [ ] Unions diff --git a/build.zig b/build.zig index 0911881..17efa18 100644 --- a/build.zig +++ b/build.zig @@ -250,8 +250,16 @@ const exercises = [_]Exercise{ .output = "5 aliens. 4 aliens. 1 aliens. 0 aliens. Earth is saved!", .hint = "Use the heat ray. And the method!", }, - // use struct method for elephant tails - // quiz: add elephant trunk (like tail)! + .{ + .main_file = "48_methods2.zig", + .output = "Elephant A (U). Elephant B (U). Elephant C (U).", + .hint = "This just needs one little fix." + }, + // 48 use struct method for elephant tails + // 49 quiz: add elephant trunk (like tail)! + // 50 null vs undefined + // 51 pass-by-value and const fn params + // 52 slices! }; /// Check the zig version to make sure it can compile the examples properly. diff --git a/exercises/46_optionals2.zig b/exercises/46_optionals2.zig index a037382..d3f65bb 100644 --- a/exercises/46_optionals2.zig +++ b/exercises/46_optionals2.zig @@ -5,11 +5,23 @@ // linked to the first elephant. This is because we had NO CONCEPT // of a tail that didn't point to another elephant! // +// We also introduce the handy ".?" shortcut: +// +// const foo = bar.?; +// +// is the same as +// +// const foo = bar orelse unreachable; +// +// See if you can find where we use this shortcut below. +// +// Now let's make those elephant tails optional! +// const std = @import("std"); const Elephant = struct { letter: u8, - tail: *Elephant = null, // <---- make this optional! + tail: *Elephant = null, // Hmm... tail needs something... visited: bool = false, }; diff --git a/exercises/48_methods2.zig b/exercises/48_methods2.zig new file mode 100644 index 0000000..b9477da --- /dev/null +++ b/exercises/48_methods2.zig @@ -0,0 +1,68 @@ +// +// Now that we've seen how methods work, let's see if we can help +// our elephants out a bit more with some Elephant methods. +// +const std = @import("std"); + +const Elephant = struct { + letter: u8, + tail: ?*Elephant = null, + visited: bool = false, + + // New Elephant methods! + pub fn getTail(self: *Elephant) *Elephant { + return self.tail.?; // Remember, this is means "orelse unreachable" + } + + pub fn hasTail(self: *Elephant) bool { + return (self.tail != null); + } + + pub fn visit(self: *Elephant) void { + self.visited = true; + } + + pub fn print(self: *Elephant) void { + // Prints elephant letter and (V)isited or (U)nvisited. + var v: u8 = if (self.visited) 'V' else 'U'; + std.debug.print("Elephant {u} ({u}). ", .{ self.letter, v}); + } +}; + +pub fn main() void { + var elephantA = Elephant{ .letter = 'A' }; + var elephantB = Elephant{ .letter = 'B' }; + var elephantC = Elephant{ .letter = 'C' }; + + // Link the elephants so that each tail "points" to the next. + elephantA.tail = &elephantB; + elephantB.tail = &elephantC; + + visitElephants(&elephantA); + + std.debug.print("\n", .{}); +} + +// This function visits all elephants once, starting with the +// first elephant and following the tails to the next elephant. +fn visitElephants(first_elephant: *Elephant) void { + var e = first_elephant; + + while (true) { + e.print(); + e.visit(); + + // Get the next elephant or stop. + if (e.hasTail()) { + e = e.???; // Which method do we want here? + } else { + break; + } + } +} + +// Bonus: Zig's enums can also have methods! Can you find +// one in the wild? If you can, mention it along with your +// name or alias in a comment below this one and make a +// pull request on GitHub for a piece of eternal Ziglings +// glory. The first five (5) PRs will be accepted! diff --git a/patches/patches/46_optionals2.patch b/patches/patches/46_optionals2.patch index 5becede..10705d9 100644 --- a/patches/patches/46_optionals2.patch +++ b/patches/patches/46_optionals2.patch @@ -1,8 +1,21 @@ -12c12 -< tail: *Elephant = null, // <---- make this optional! +8,19d7 +< // We also introduce the handy ".?" shortcut: +< // +< // const foo = bar.?; +< // +< // is the same as +< // +< // const foo = bar orelse unreachable; +< // +< // See if you can find where we use this shortcut below. +< // +< // Now let's make those elephant tails optional! +< // +24c12 +< tail: *Elephant = null, // Hmm... tail needs something... --- > tail: ?*Elephant = null, // <---- make this optional! -42c42 +54c42 < if (e.tail == null) ???; --- > if (e.tail == null) break; diff --git a/patches/patches/48_methods2.patch b/patches/patches/48_methods2.patch new file mode 100644 index 0000000..781a99e --- /dev/null +++ b/patches/patches/48_methods2.patch @@ -0,0 +1,4 @@ +57c57 +< e = e.???; // Which method do we want here? +--- +> e = e.getTail(); // Which method do we want here?