From b7cd99f3db9457a2b8c14b84f70ed99b55191e06 Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Fri, 3 Mar 2023 14:21:32 +0100 Subject: [PATCH] exercises: use usize, not isize, when guaranteed non-negative (#207) Rationale: - This makes it self-documenting when the output cannot be negative. - This makes it self-documenting when a negative input is not meaningful/useful, and when the user doesn't need to return an error for a negative input. - There isn't much benefit for the user to change `isize` to `usize` themselves. --- exercises/practice/collatz-conjecture/.meta/example.zig | 4 ++-- exercises/practice/darts/.meta/example.zig | 2 +- exercises/practice/darts/darts.zig | 2 +- exercises/practice/difference-of-squares/.meta/example.zig | 6 +++--- .../difference-of-squares/difference_of_squares.zig | 6 +++--- exercises/practice/resistor-color-duo/.meta/example.zig | 4 ++-- .../practice/resistor-color-duo/resistor_color_duo.zig | 2 +- exercises/practice/resistor-color/.meta/example.zig | 2 +- exercises/practice/resistor-color/resistor_color.zig | 2 +- 9 files changed, 15 insertions(+), 15 deletions(-) diff --git a/exercises/practice/collatz-conjecture/.meta/example.zig b/exercises/practice/collatz-conjecture/.meta/example.zig index cd719453..42698576 100644 --- a/exercises/practice/collatz-conjecture/.meta/example.zig +++ b/exercises/practice/collatz-conjecture/.meta/example.zig @@ -2,12 +2,12 @@ pub const ComputationError = error{ IllegalArgument, }; -pub fn steps(start: isize) ComputationError!isize { +pub fn steps(start: isize) ComputationError!usize { if (start <= 0) { return ComputationError.IllegalArgument; } var number = start; - var count: isize = 0; + var count: usize = 0; while (number > 1) { if (@mod(number, 2) == 0) { number = @divTrunc(number, 2); diff --git a/exercises/practice/darts/.meta/example.zig b/exercises/practice/darts/.meta/example.zig index d3ed738d..e7b6cd33 100644 --- a/exercises/practice/darts/.meta/example.zig +++ b/exercises/practice/darts/.meta/example.zig @@ -12,7 +12,7 @@ pub const Coordinate = struct { }; } - pub fn score(self: Coordinate) isize { + pub fn score(self: Coordinate) usize { const d = math.sqrt(self.x_coord * self.x_coord + self.y_coord * self.y_coord); if (d > 10) { diff --git a/exercises/practice/darts/darts.zig b/exercises/practice/darts/darts.zig index 64fc066d..6c07d304 100644 --- a/exercises/practice/darts/darts.zig +++ b/exercises/practice/darts/darts.zig @@ -6,7 +6,7 @@ pub const Coordinate = struct { _ = y_coord; @compileError("please implement the init method"); } - pub fn score(self: Coordinate) isize { + pub fn score(self: Coordinate) usize { _ = self; @compileError("please implement the score method"); } diff --git a/exercises/practice/difference-of-squares/.meta/example.zig b/exercises/practice/difference-of-squares/.meta/example.zig index 4c4d615e..5719b84a 100644 --- a/exercises/practice/difference-of-squares/.meta/example.zig +++ b/exercises/practice/difference-of-squares/.meta/example.zig @@ -1,12 +1,12 @@ -pub fn squareOfSum(number: isize) isize { +pub fn squareOfSum(number: usize) usize { const result = @divExact(number * (number + 1), 2); return result * result; } -pub fn sumOfSquares(number: isize) isize { +pub fn sumOfSquares(number: usize) usize { return @divExact(number * (number + 1) * (2 * number + 1), 6); } -pub fn differenceOfSquares(number: isize) isize { +pub fn differenceOfSquares(number: usize) usize { return squareOfSum(number) - sumOfSquares(number); } diff --git a/exercises/practice/difference-of-squares/difference_of_squares.zig b/exercises/practice/difference-of-squares/difference_of_squares.zig index 1fc98e2b..c38f8a64 100644 --- a/exercises/practice/difference-of-squares/difference_of_squares.zig +++ b/exercises/practice/difference-of-squares/difference_of_squares.zig @@ -1,14 +1,14 @@ -pub fn squareOfSum(number: isize) isize { +pub fn squareOfSum(number: usize) usize { _ = number; @compileError("compute the sum of i from 0 to n then square it"); } -pub fn sumOfSquares(number: isize) isize { +pub fn sumOfSquares(number: usize) usize { _ = number; @compileError("compute the sum of i^2 from 0 to n"); } -pub fn differenceOfSquares(number: isize) isize { +pub fn differenceOfSquares(number: usize) usize { _ = number; @compileError("compute the difference between the square of sum and sum of squares"); } diff --git a/exercises/practice/resistor-color-duo/.meta/example.zig b/exercises/practice/resistor-color-duo/.meta/example.zig index 98629583..c94aa65e 100644 --- a/exercises/practice/resistor-color-duo/.meta/example.zig +++ b/exercises/practice/resistor-color-duo/.meta/example.zig @@ -11,6 +11,6 @@ pub const ColorBand = enum(u4) { white, }; -pub fn colorCode(colors: [2]ColorBand) isize { - return @as(isize, @enumToInt(colors[0])) * 10 + @as(isize, @enumToInt(colors[1])); +pub fn colorCode(colors: [2]ColorBand) usize { + return @as(usize, @enumToInt(colors[0])) * 10 + @as(usize, @enumToInt(colors[1])); } diff --git a/exercises/practice/resistor-color-duo/resistor_color_duo.zig b/exercises/practice/resistor-color-duo/resistor_color_duo.zig index 96f7bbe1..358bebb2 100644 --- a/exercises/practice/resistor-color-duo/resistor_color_duo.zig +++ b/exercises/practice/resistor-color-duo/resistor_color_duo.zig @@ -1,4 +1,4 @@ -pub fn colorCode(colors: [2]ColorBand) isize { +pub fn colorCode(colors: [2]ColorBand) usize { _ = colors; @compileError("please implement the colorCode function"); } diff --git a/exercises/practice/resistor-color/.meta/example.zig b/exercises/practice/resistor-color/.meta/example.zig index 21488c3f..413b4419 100644 --- a/exercises/practice/resistor-color/.meta/example.zig +++ b/exercises/practice/resistor-color/.meta/example.zig @@ -16,7 +16,7 @@ const band_colors = [_]ColorBand{ .green, .blue, .violet, .grey, .white, }; -pub fn colorCode(color: ColorBand) isize { +pub fn colorCode(color: ColorBand) usize { return @enumToInt(color); } diff --git a/exercises/practice/resistor-color/resistor_color.zig b/exercises/practice/resistor-color/resistor_color.zig index dfdc9f3f..cfcd51fb 100644 --- a/exercises/practice/resistor-color/resistor_color.zig +++ b/exercises/practice/resistor-color/resistor_color.zig @@ -1,4 +1,4 @@ -pub fn colorCode(color: ColorBand) isize { +pub fn colorCode(color: ColorBand) usize { @compileError("determine the value of a colorband on a resistor"); }