Merge pull request #22 from quexxon/apply-zig-fmt

Apply `zig fmt` to exercises and generate remaining patch files
This commit is contained in:
Dave Gauer 2021-02-15 19:32:00 -05:00 committed by GitHub
commit a2b6b68a25
81 changed files with 422 additions and 196 deletions

View file

@ -19,4 +19,3 @@ const std = @import("std");
fn main() void { fn main() void {
std.debug.print("Hello world!\n", .{}); std.debug.print("Hello world!\n", .{});
} }

View file

@ -46,6 +46,7 @@ pub fn main() void {
// Use the len property to get the length of the array: // Use the len property to get the length of the array:
const length = some_primes.???; const length = some_primes.???;
std.debug.print("First: {}, Fourth: {}, Length: {}\n", std.debug.print("First: {}, Fourth: {}, Length: {}\n", .{
.{first, fourth, length}); first, fourth, length,
});
} }

View file

@ -34,7 +34,6 @@ pub fn main() void {
// That's all the problems. Let's see our results: // That's all the problems. Let's see our results:
std.debug.print("d={u} {s}{s}\n", .{ d, laugh, major_tom }); std.debug.print("d={u} {s}{s}\n", .{ d, laugh, major_tom });
//
// Keen eyes will notice that we've put 'u' and 's' inside the '{}' // Keen eyes will notice that we've put 'u' and 's' inside the '{}'
// placeholders in the format string above. This tells the // placeholders in the format string above. This tells the
// print() function to format the values as a UTF-8 character and // print() function to format the values as a UTF-8 character and

View file

@ -14,4 +14,3 @@ pub fn main() void {
std.debug.print("With the discount, the price is ${}.\n", .{price}); std.debug.print("With the discount, the price is ${}.\n", .{price});
} }

View file

@ -23,6 +23,5 @@ pub fn main() void {
std.debug.print("The End.\n", .{}); std.debug.print("The End.\n", .{});
} }
//
// Note that "for" loops also work on things called "slices" // Note that "for" loops also work on things called "slices"
// which we'll see later. // which we'll see later.

View file

@ -23,7 +23,6 @@ pub fn main() void {
std.debug.print("Answer to the Ultimate Question: {}\n", .{answer}); std.debug.print("Answer to the Ultimate Question: {}\n", .{answer});
} }
//
// Please define the deepThought() function below. // Please define the deepThought() function below.
// //
// We're just missing a couple things. One thing we're NOT missing is the // We're just missing a couple things. One thing we're NOT missing is the

View file

@ -18,7 +18,6 @@ pub fn main() void {
}); });
} }
//
// Please give this function the correct input parameter(s). // Please give this function the correct input parameter(s).
// You'll need to figure out the parameter name and type that we're // You'll need to figure out the parameter name and type that we're
// expecting. The output type has already been specified for you. // expecting. The output type has already been specified for you.

View file

@ -14,7 +14,6 @@ pub fn main() void {
std.debug.print("\n", .{}); std.debug.print("\n", .{});
} }
//
// You won't see this every day: a function that takes an array with // You won't see this every day: a function that takes an array with
// exactly four u16 numbers. This is not how you would normally pass // exactly four u16 numbers. This is not how you would normally pass
// an array to a function. We'll learn about slices and pointers in // an array to a function. We'll learn about slices and pointers in
@ -28,7 +27,6 @@ fn printPowersOfTwo(numbers: [4]u16) ??? {
} }
} }
//
// This function bears a striking resemblance to twoToThe() in the last // This function bears a striking resemblance to twoToThe() in the last
// exercise. But don't be fooled! This one does the math without the aid // exercise. But don't be fooled! This one does the math without the aid
// of the standard library! // of the standard library!

View file

@ -27,4 +27,3 @@ pub fn main() void {
std.debug.print("I compiled!", .{}); std.debug.print("I compiled!", .{});
} }

View file

@ -37,7 +37,9 @@ pub fn main() void {
// detectProblems() Returns the number or an error. // detectProblems() Returns the number or an error.
// //
fn makeJustRight(n: u32) MyNumberError!u32 { fn makeJustRight(n: u32) MyNumberError!u32 {
return fixTooBig(n) catch |err| { return err; }; return fixTooBig(n) catch |err| {
return err;
};
} }
fn fixTooBig(n: u32) MyNumberError!u32 { fn fixTooBig(n: u32) MyNumberError!u32 {
@ -65,4 +67,3 @@ fn detectProblems(n: u32) MyNumberError!u32 {
if (n > 20) return MyNumberError.TooBig; if (n > 20) return MyNumberError.TooBig;
return n; return n;
} }

View file

@ -23,7 +23,6 @@ pub fn main() void {
} }
fn addFive(n: u32) MyNumberError!u32 { fn addFive(n: u32) MyNumberError!u32 {
//
// This function needs to return any error which might come back from detect(). // This function needs to return any error which might come back from detect().
// Please use a "try" statement rather than a "catch". // Please use a "try" statement rather than a "catch".
// //
@ -37,4 +36,3 @@ fn detect(n: u32) MyNumberError!u32 {
if (n > 20) return MyNumberError.TooBig; if (n > 20) return MyNumberError.TooBig;
return n; return n;
} }

View file

@ -9,7 +9,6 @@ pub fn main() void {
for (animals) |a| printAnimal(a); for (animals) |a| printAnimal(a);
std.debug.print("done.\n", .{}); std.debug.print("done.\n", .{});
} }
@ -21,9 +20,18 @@ fn printAnimal(animal: u8) void {
std.debug.print(") ", .{}); // <---- how!? std.debug.print(") ", .{}); // <---- how!?
if (animal == 'g'){ std.debug.print("Goat", .{}); return; } if (animal == 'g') {
if (animal == 'c'){ std.debug.print("Cat", .{}); return; } std.debug.print("Goat", .{});
if (animal == 'd'){ std.debug.print("Dog", .{}); return; } return;
}
if (animal == 'c') {
std.debug.print("Cat", .{});
return;
}
if (animal == 'd') {
std.debug.print("Dog", .{});
return;
}
std.debug.print("Unknown", .{}); std.debug.print("Unknown", .{});
} }

View file

@ -15,7 +15,6 @@
// //
const std = @import("std"); const std = @import("std");
//
var counter: u32 = 0; var counter: u32 = 0;
const MyErr = error{ GetFail, IncFail }; const MyErr = error{ GetFail, IncFail };

View file

@ -22,8 +22,6 @@
// return GameError.TooManyPlayers; // return GameError.TooManyPlayers;
// } // }
// //
//
//
const std = @import("std"); const std = @import("std");
pub fn main() void { pub fn main() void {

View file

@ -26,9 +26,15 @@ pub fn main() void {
for (operations) |op| { for (operations) |op| {
switch (op) { switch (op) {
1 => { current_value += 1; }, 1 => {
2 => { current_value -= 1; }, current_value += 1;
3 => { current_value *= current_value; }, },
2 => {
current_value -= 1;
},
3 => {
current_value *= current_value;
},
} }
std.debug.print("{} ", .{current_value}); std.debug.print("{} ", .{current_value});

View file

@ -29,16 +29,22 @@ pub fn main() void {
Ops.inc, Ops.inc,
Ops.pow, Ops.pow,
Ops.dec, Ops.dec,
Ops.dec Ops.dec,
}; };
var current_value: u32 = 0; var current_value: u32 = 0;
for (operations) |op| { for (operations) |op| {
switch (op) { switch (op) {
Ops.inc => { current_value += 1; }, Ops.inc => {
Ops.dec => { current_value -= 1; }, current_value += 1;
Ops.pow => { current_value *= current_value; }, },
Ops.dec => {
current_value -= 1;
},
Ops.pow => {
current_value *= current_value;
},
// No "else" needed! Why is that? // No "else" needed! Why is that?
} }

View file

@ -54,6 +54,6 @@ pub fn main() void {
std.debug.print("Your wizard has {} health and {} gold.", .{ std.debug.print("Your wizard has {} health and {} gold.", .{
glorp_the_wise.health, glorp_the_wise.health,
glorp_the_wise.gold glorp_the_wise.gold,
}); });
} }

View file

@ -45,7 +45,8 @@ pub fn main() void {
// Printing all RPG characters in a loop: // Printing all RPG characters in a loop:
for (chars) |c, num| { for (chars) |c, num| {
std.debug.print("Character {} - G:{} H:{} XP:{}\n", std.debug.print("Character {} - G:{} H:{} XP:{}\n", .{
.{num+1, c.gold, c.health, c.experience}); num + 1, c.gold, c.health, c.experience,
});
} }
} }

View file

@ -12,7 +12,6 @@ pub fn main() void {
makeFive(&num); makeFive(&num);
std.debug.print("num: {}, ", .{num}); std.debug.print("num: {}, ", .{num});
// Now something interesting. Let's pass a reference to a // Now something interesting. Let's pass a reference to a
// specific array value: // specific array value:
makeFive(&more_nums[2]); makeFive(&more_nums[2]);

View file

@ -44,7 +44,7 @@ const Class = enum{
const Character = struct { const Character = struct {
class: Class, class: Class,
gold: u32, gold: u32,
health: u8 = 100, // <--- You can also fields a default value! health: u8 = 100, // <--- You can also provide fields a default value!
experience: u32, experience: u32,
}; };
@ -60,7 +60,6 @@ pub fn main() void {
printCharacter(???); printCharacter(???);
} }
// Note how this function's "c" parameter is a pointer to a Character struct. // Note how this function's "c" parameter is a pointer to a Character struct.
fn printCharacter(c: *Character) void { fn printCharacter(c: *Character) void {

View file

@ -39,7 +39,6 @@ fn deepThought() ?u8 {
// But we'll leave this as-is. Sorry Deep Thought. // But we'll leave this as-is. Sorry Deep Thought.
return null; return null;
} }
//
// Blast from the past: // Blast from the past:
// //
// Optionals are a lot like error union types which can either // Optionals are a lot like error union types which can either

View file

@ -1 +1,12 @@
37c37
< const n: u8 = 50;
---
> var n: u8 = 50;
40c40
< const pi: u8 = 314159;
---
> const pi: u32 = 314159;
42c42
< const negative_eleven: u8 = -11;
---
> const negative_eleven: i8 = -11;

View file

@ -1 +1,12 @@
30c30
< const some_primes = [_]u8{ 1, 3, 5, 7, 11, 13, 17, 19 };
---
> var some_primes = [_]u8{ 1, 3, 5, 7, 11, 13, 17, 19 };
43c43
< const fourth = some_primes[???];
---
> const fourth = some_primes[3];
47c47
< const length = some_primes.???;
---
> const length = some_primes.len;

View file

@ -1 +1,8 @@
23c23
< const leet = ???;
---
> const leet = le ++ et;
28c28
< const bit_pattern = [_]u8{ ??? } ** 3;
---
> const bit_pattern = [_]u8{ 1, 0, 0, 1 } ** 3;

View file

@ -1 +1,12 @@
22c22
< const d: u8 = ziggy[???];
---
> const d: u8 = ziggy[4];
26c26
< const laugh = "ha " ???;
---
> const laugh = "ha " ** 3;
33c33
< const major_tom = major ??? tom;
---
> const major_tom = major ++ " " ++ tom;

View file

@ -1 +1,8 @@
18,20c18,20
< Ziggy played guitar
< Jamming good with Andrew Kelley
< And the Spiders from Mars
---
> \\Ziggy played guitar
> \\Jamming good with Andrew Kelley
> \\And the Spiders from Mars

View file

@ -1 +1,14 @@
16c16
< const x: u8 = 1;
---
> var x: u8 = 1;
27c27
< lang[???] = letters[x];
---
> lang[1] = letters[x];
29,30c29,30
< x = ???;
< lang[2] = letters[???];
---
> x = 5;
> lang[2] = letters[x];

View file

@ -1 +1,4 @@
26c26
< if (foo) {
---
> if (foo == 1) {

View file

@ -1 +1,4 @@
13c13
< var price: u8 = if ???;
---
> var price: u8 = if (discount) 17 else 20;

View file

@ -1 +1,4 @@
24c24
< while (???) {
---
> while (n < 1024) {

View file

@ -1 +1,4 @@
28c28
< while (n < 1000) : ??? {
---
> while (n < 1000) : (n *= 2) {

View file

@ -1 +1,6 @@
27,28c27,28
< if (n % 3 == 0) ???;
< if (n % 5 == 0) ???;
---
> if (n % 3 == 0) continue;
> if (n % 5 == 0) continue;

View file

@ -1 +1,4 @@
21c21
< if (???) ???;
---
> if (n == 4) break;

View file

@ -1 +1,4 @@
18c18
< for (???) |???| {
---
> for (story) |scene| {

View file

@ -1 +1,4 @@
27c27
< for (bits) |bit, ???| {
---
> for (bits) |bit, i| {

View file

@ -1 +1,16 @@
12c12
< const std = import standard library;
---
> const std = @import("std");
14c14
< function main() void {
---
> pub fn main() void {
19c19
< ??? (i <= stop_at) : (i += 1) {
---
> while (i <= stop_at) : (i += 1) {
23c23
< std.debug.print("{}", .{???});
---
> std.debug.print("{}", .{i});

View file

@ -1 +1,4 @@
31c31
< ??? deepThought() ??? {
---
> fn deepThought() u8 {

View file

@ -1 +1,4 @@
25c25
< fn twoToThe(???) u32 {
---
> fn twoToThe(my_number: u32) u32 {

View file

@ -1 +1,18 @@
24,25c24,25
< fn printPowersOfTwo(numbers: [4]u16) ??? {
< loop (numbers) |n| {
---
> fn printPowersOfTwo(numbers: [4]u16) void {
> for (numbers) |n| {
34c34
< fn twoToThe(number: u16) ??? {
---
> fn twoToThe(number: u16) u16 {
38c38
< loop (n < number) : (n += 1) {
---
> while (n < number) : (n += 1) {
42c42
< return ???;
---
> return total;

View file

@ -1 +1,8 @@
12c12
< ???,
---
> TooSmall,
29c29
< if (???) {
---
> if (number_error == MyNumberError.TooSmall) {

View file

@ -1 +1,4 @@
22c22
< var my_number: ??? = 5;
---
> var my_number: MyNumberError!u8 = 5;

View file

@ -1 +1,8 @@
15c15
< var b: u32 = addTwenty(4) ??? 22;
---
> var b: u32 = addTwenty(4) catch 22;
22c22
< fn addTwenty(n: u32) ??? {
---
> fn addTwenty(n: u32) MyNumberError!u32 {

View file

@ -1 +1,10 @@
62c62,68
< return detectProblems(n) ???
---
> return detectProblems(n) catch |err| {
> if (err == MyNumberError.TooSmall) {
> return 10;
> }
>
> return err;
> };

View file

@ -1 +1,4 @@
29c29
< var x = detect(n);
---
> var x = try detect(n);

View file

@ -1 +1,4 @@
22c22
< stdout.print("Hello world!\n", .{});
---
> try stdout.print("Hello world!\n", .{});

View file

@ -1 +1,4 @@
23c23
< std.debug.print("Two\n", .{});
---
> defer std.debug.print("Two\n", .{});

View file

@ -1 +1,4 @@
21c21
< std.debug.print(") ", .{}); // <---- how!?
---
> defer std.debug.print(") ", .{}); // <---- how!?

View file

@ -1 +1,4 @@
35c34
< std.debug.print("failed!\n", .{});
---
> errdefer std.debug.print("failed!\n", .{});

View file

@ -1 +1,2 @@
48a49
> else => std.debug.print("?", .{}),

View file

@ -1 +1,2 @@
33a34
> else => '!',

View file

@ -1 +1,2 @@
37a38
> else => unreachable,

View file

@ -1 +1,2 @@
36a37
> MyNumberError.TooSmall => std.debug.print("<4. ", .{}),

View file

@ -1 +1,8 @@
12c12
< pub fn main() void {
---
> pub fn main() !void {
15c15
< const my_num: u32 = getNumber();
---
> const my_num: u32 = try getNumber();

View file

@ -1 +1,4 @@
23c23
< const Ops = enum { ??? };
---
> const Ops = enum { dec, inc, pow };

View file

@ -1 +1,12 @@
32c32
< blue = ???,
---
> blue = 0x0000ff,
54c54
< \\ <span style="color: #{}">Blue</span>
---
> \\ <span style="color: #{x:0>6}">Blue</span>
59c59
< @enumToInt(???), // Oops! We're missing something!
---
> @enumToInt(Color.blue), // Oops! We're missing something!

View file

@ -1 +1,4 @@
38a39
> health: u8,
46a48
> .health = 100,

View file

@ -1 +1,7 @@
44a45,50
> chars[1] = Character{
> .class = Class.bard,
> .gold = 10,
> .health = 100,
> .experience = 20,
> };

View file

@ -1 +1,4 @@
33c33
< num2 = ???;
---
> num2 = num1_pointer.*;

View file

@ -1 +1,4 @@
24c24
< const b: *u8 = &a; // fix this!
---
> const b: *const u8 = &a; // fix this!

View file

@ -1 +1,4 @@
34c34
< ??? p: ??? = undefined;
---
> var p: *u8 = undefined;

View file

@ -1 +1,4 @@
31c31
< ??? = 5; // fix me!
---
> x.* = 5; // fix me!

View file

@ -1 +1,4 @@
60c60
< printCharacter(???);
---
> printCharacter(&glorp);

View file

@ -1 +1,4 @@
21a22
> var elephantB = Elephant{ .letter = 'B' };
27a29
> elephantB.tail = &elephantC;

View file

@ -1 +1,4 @@
32c32
< var answer: u8 = result;
---
> var answer: u8 = result orelse 42;