mirror of
https://codeberg.org/andyscott/ziglings.git
synced 2024-12-22 14:03:10 -05:00
Added ex. 46 optionals 2 - elephants!
This commit is contained in:
parent
efdcfc70d4
commit
f27601b558
4 changed files with 64 additions and 1 deletions
|
@ -240,7 +240,11 @@ const exercises = [_]Exercise{
|
||||||
.main_file = "45_optionals.zig",
|
.main_file = "45_optionals.zig",
|
||||||
.output = "The Ultimate Answer: 42.",
|
.output = "The Ultimate Answer: 42.",
|
||||||
},
|
},
|
||||||
// optional fields (elephant tail - no longer need circular)
|
.{
|
||||||
|
.main_file = "46_optionals2.zig",
|
||||||
|
.output = "Elephant A. Elephant B. Elephant C.",
|
||||||
|
.hint = "Elephants!",
|
||||||
|
},
|
||||||
// super-simple struct method
|
// super-simple struct method
|
||||||
// use struct method for elephant tails
|
// use struct method for elephant tails
|
||||||
// quiz: add elephant trunk (like tail)!
|
// quiz: add elephant trunk (like tail)!
|
||||||
|
|
|
@ -28,6 +28,8 @@ pub fn main() void {
|
||||||
elephantC.tail = &elephantA;
|
elephantC.tail = &elephantA;
|
||||||
|
|
||||||
visitElephants(&elephantA);
|
visitElephants(&elephantA);
|
||||||
|
|
||||||
|
std.debug.print("\n", .{});
|
||||||
}
|
}
|
||||||
|
|
||||||
// This function visits all elephants once, starting with the
|
// This function visits all elephants once, starting with the
|
||||||
|
|
46
exercises/46_optionals2.zig
Normal file
46
exercises/46_optionals2.zig
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
//
|
||||||
|
// Now that we have optional types, we can apply them to structs.
|
||||||
|
// The last time we checked in with our elephants, we had to link
|
||||||
|
// all three of them together in a "circle" so that the last tail
|
||||||
|
// linked to the first elephant. This is because we had NO CONCEPT
|
||||||
|
// of a tail that didn't point to another elephant!
|
||||||
|
//
|
||||||
|
const std = @import("std"); // single quotes
|
||||||
|
|
||||||
|
const Elephant = struct {
|
||||||
|
letter: u8,
|
||||||
|
tail: *Elephant = undefined, // <---- make this optional!
|
||||||
|
visited: bool = false,
|
||||||
|
};
|
||||||
|
|
||||||
|
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 (!e.visited) {
|
||||||
|
std.debug.print("Elephant {u}. ", .{e.letter});
|
||||||
|
e.visited = true;
|
||||||
|
|
||||||
|
// We should stop once we encounter a tail that
|
||||||
|
// does NOT point to another element. What can
|
||||||
|
// we put here to make that happen?
|
||||||
|
if (e.tail == null) ???;
|
||||||
|
|
||||||
|
e = e.tail.?;
|
||||||
|
}
|
||||||
|
}
|
11
patches/patches/46_optionals2.patch
Normal file
11
patches/patches/46_optionals2.patch
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
12c12
|
||||||
|
< tail: *Elephant = undefined, // <---- make this optional!
|
||||||
|
---
|
||||||
|
> tail: ?*Elephant = undefined,
|
||||||
|
39,42c39
|
||||||
|
< // We should stop once we encounter a tail that
|
||||||
|
< // does NOT point to another element. What can
|
||||||
|
< // we put here to make that happen?
|
||||||
|
< if (e.tail == null) ???;
|
||||||
|
---
|
||||||
|
> if (e.tail == null) break;
|
Loading…
Reference in a new issue