Skip to content

Commit e6dce44

Browse files
committed
Major Zig cleanup
1 parent b0d5d51 commit e6dce44

File tree

9 files changed

+49
-34
lines changed

9 files changed

+49
-34
lines changed

zig/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Zig Explorations
44

5-
To build and run Zig programs on your local machine, download and install Zig from the [Zig Download](https://ziglang.org/download/) or use your favorite package manager, e.g. `brew install zig` on macOS.
5+
To build and run Zig programs on your local machine, download and install Zig from the [Zig Download](https://ziglang.org/download/) page or use your favorite package manager, e.g. `brew install zig` on macOS.
66

77
Once installed, programs in this folder can be run from the command line like so:
88

@@ -18,6 +18,8 @@ zig run permutations.zig -- I like carrots
1818
zig run top_ten_scorers.zig < ../test/wnba_input
1919
```
2020

21+
Test scripts are provided in this folder for both Bash and PowerShell.
22+
2123
## About Zig
2224

2325
The Zig language was created by Andrew Kelley in 2016, as a pragmatic alternative to the venerable <a href="https://github.com/rtoal/ple/tree/main/c">C</a> programming language. Zig offers many safety features and a growing ecosystem for building and testing. It is known for having “no surprises”: there are no hidden function calls and no hidden memory allocations. Unlike C, Zig has no preprocessor and no macros. Zig does add option types, generics, and custom allocators.
@@ -28,8 +30,8 @@ Continue your exploration of Zig via:
2830

2931
- [Zig Home Page](https://ziglang.org)
3032
- [Zig Docs](https://ziglang.org/documentation/master/)
31-
- [Intro to Zig](https://www.youtube.com/watch?v=YXrb-DqsBNU)
32-
- [ZigLearn](https://ziglearn.org/learn/)
33+
- Andrew Kelley’s [Intro to Zig](https://www.youtube.com/watch?v=YXrb-DqsBNU) video from GOTO 2022
34+
- [ZigLearn](https://ziglang.org/learn/)
3335
- [Wikipedia](<https://en.wikipedia.org/wiki/Zig_(programming_language)>)
3436
- The book [Introduction to Zig](https://github.com/pedropark99/zig-book)
3537

zig/clockhands.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
const print = @import("std").debug.print;
1+
const stdout = @import("std").io.getStdOut().writer();
22

3-
pub fn main() void {
3+
pub fn main() !void {
44
for (0..11) |i| {
55
const t = (43200 * i + 21600) / 11;
66
const h = t / 3600;
77
const m = t / 60 % 60;
88
const s = t % 60;
9-
print("{d:0>2}:{d:0>2}:{d:0>2}\n", .{ if (h == 0) 12 else h, m, s });
9+
try stdout.print("{d:0>2}:{d:0>2}:{d:0>2}\n", .{ if (h == 0) 12 else h, m, s });
1010
}
1111
}

zig/coords.zig

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
const std = @import("std");
2-
const print = std.debug.print;
2+
const stdout = std.io.getStdOut().writer();
33

4-
fn coord(x: i32, y: i32, z: i32) void {
5-
print("The coordinates are: x = {}, y = {}, z = {}\n", .{ x, y, z });
4+
fn showCoordinates(x: i32, y: i32, z: i32) void {
5+
stdout.print("Got: x = {}, y = {}, z = {}\n", .{ x, y, z }) catch {
6+
std.debug.panic("{s}\n", .{"Failed to print"});
7+
};
68
}
79

810
pub fn main() void {
9-
coord(20, 40, 0);
11+
showCoordinates(5, 8, 3);
1012
}

zig/hello.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const print = @import("std").debug.print;
1+
var stdout = @import("std").io.getStdOut().writer();
22

3-
pub fn main() void {
4-
print("Hello, world!\n", .{});
3+
pub fn main() !void {
4+
try stdout.print("{s}\n", .{"Hello, world!"});
55
}

zig/odd_even.zig

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
const std = @import("std");
2-
const print = std.debug.print;
1+
var stdout = @import("std").io.getStdOut().writer();
32

4-
pub fn main() void {
3+
pub fn main() !void {
54
const number: i32 = 21;
65

76
if (number % 2 == 0) {
8-
print("The number {} is even\n", .{number});
7+
try stdout.print("The number {} is even\n", .{number});
98
} else {
10-
print("The number {} is odd\n", .{number});
9+
try stdout.print("The number {} is odd\n", .{number});
1110
}
1211
}
File renamed without changes.

zig/test.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env bash
2+
zig run clockhands.zig && \
3+
zig run coords.zig && \
4+
zig run hello.zig && \
5+
zig run odd_even.zig && \
6+
zig run permutations.zig -- I like carrots | diff ../test/carrots_expected - && \
7+
zig run top_ten_scorers.zig < ../test/wnba_input | diff ../test/wnba_expected - && \
8+
zig run triple.zig | diff ../test/triple_expected - && \
9+
10+
if [ $? -ne 0 ]; then
11+
echo
12+
echo "*** ZIG TESTS FAILED ***"
13+
exit 1
14+
else
15+
echo
16+
echo "ZIG TESTS PASSED"
17+
fi

zig/top_ten_scorers.zig

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
const std = @import("std");
2+
const reader = std.io.getStdIn().reader();
3+
const stdout = std.io.getStdOut().writer();
4+
const allocator = std.heap.page_allocator;
25

36
const Player = struct {
47
name: []const u8,
@@ -7,10 +10,6 @@ const Player = struct {
710
};
811

912
pub fn main() !void {
10-
const allocator = std.heap.page_allocator;
11-
const reader = std.io.getStdIn().reader();
12-
var stdout = std.io.getStdOut().writer();
13-
1413
var players = std.ArrayList(Player).init(allocator);
1514

1615
var buf: [1000]u8 = undefined;

zig/triple.zig

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
const std = @import("std");
2-
const math = @import("std").math;
3-
const print = std.debug.print;
1+
const stdout = @import("std").io.getStdOut().writer();
42

5-
pub fn main() void {
6-
const limit: usize = 40;
7-
for (1..limit) |a| {
8-
for (a..limit) |b| {
9-
const c_squared: usize = a * a + b * b;
10-
const c: usize = @intCast(math.sqrt(c_squared));
11-
12-
if (a * a + b * b == c * c and c <= limit) {
13-
print("{}, {}, {}\n", .{ a, b, c });
3+
pub fn main() !void {
4+
for (1..41) |c| {
5+
for (1..c) |b| {
6+
for (1..b) |a| {
7+
if (a * a + b * b == c * c) {
8+
try stdout.print("{}, {}, {}\n", .{ a, b, c });
9+
}
1410
}
1511
}
1612
}

0 commit comments

Comments
 (0)