Skip to content

Commit

Permalink
exercises: use new for loop syntax (#312)
Browse files Browse the repository at this point in the history
Zig 0.11.0 added new for loop syntax [1]. Prefer to loop over a range,
which avoids some variables.

For more discussion, see Loris' blog post [2].

[1] https://ziglang.org/download/0.11.0/release-notes.html#Multi-Object-For-Loops
[2] https://kristoff.it/blog/zig-multi-sequence-for-loops/

Refs: #313
  • Loading branch information
ee7 authored Sep 14, 2023
1 parent 38c5f47 commit 1030835
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 12 deletions.
3 changes: 1 addition & 2 deletions exercises/practice/acronym/.meta/example.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ pub fn abbreviate(allocator: mem.Allocator, words: []const u8) mem.Allocator.Err
var letters = std.ArrayList(u8).init(allocator);
defer letters.deinit();

var i: usize = 0;
while (i < words.len) : (i += 1) {
for (0..words.len) |i| {
if (!ascii.isAlphabetic(words[i])) continue;

if (i == 0 or words[i - 1] == ' ' or words[i - 1] == '-' or words[i - 1] == '_') {
Expand Down
3 changes: 1 addition & 2 deletions exercises/practice/dnd-character/.meta/example.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ const random = prng.random();

pub fn ability() u8 {
var lowest: u8 = std.math.maxInt(u8);
var i: usize = 0;
var result: u8 = 0;
while (i < 4) : (i += 1) {
for (0..4) |_| {
const roll = random.intRangeAtMost(u8, 1, 6);
result += roll;
lowest = @min(lowest, roll);
Expand Down
6 changes: 2 additions & 4 deletions exercises/practice/dnd-character/test_dnd_character.zig
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ fn isValidAbilityScore(n: isize) bool {
}

test "random ability is within range" {
var i: usize = 0;
while (i < 20) : (i += 1) {
for (0..20) |_| {
const actual = dnd_character.ability();
try testing.expect(isValidAbilityScore(actual));
}
Expand All @@ -123,8 +122,7 @@ fn isValid(c: Character) bool {
}

test "random character is valid" {
var i: usize = 0;
while (i < 20) : (i += 1) {
for (0..20) |_| {
const character = Character.init();
try testing.expect(isValid(character));
}
Expand Down
3 changes: 1 addition & 2 deletions exercises/practice/perfect-numbers/.meta/example.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ pub const Classification = enum {
fn aliquotSum(n: u64) u64 {
if (n == 1) return 0;
var result: u64 = 1;
var i: usize = 2;
const isqrt_n = std.math.sqrt(n);
while (i <= isqrt_n) : (i += 1) {
for (2..isqrt_n + 1) |i| {
if (n % i == 0) result += i + (n / i);
}
// When `n` is a square number, we added the same divisor twice inside the loop.
Expand Down
3 changes: 1 addition & 2 deletions exercises/practice/proverb/.meta/example.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ pub fn recite(allocator: mem.Allocator, words: []const []const u8) (fmt.AllocPri
var main_slice = try allocator.alloc([]u8, words.len);

if (words.len > 1) {
var i: usize = 0;
while (i < words.len - 1) : (i += 1) {
for (0..words.len - 1) |i| {
main_slice[i] = try fmt.allocPrint(allocator, "For want of a {s} the {s} was lost.\n", .{ words[i], words[i + 1] });
}
}
Expand Down

0 comments on commit 1030835

Please sign in to comment.