Skip to content

Commit

Permalink
exercises: use usize, not isize, when guaranteed non-negative (#207)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ee7 authored Mar 3, 2023
1 parent 07fe8b3 commit b7cd99f
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions exercises/practice/collatz-conjecture/.meta/example.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/darts/.meta/example.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/darts/darts.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
6 changes: 3 additions & 3 deletions exercises/practice/difference-of-squares/.meta/example.zig
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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");
}
4 changes: 2 additions & 2 deletions exercises/practice/resistor-color-duo/.meta/example.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
}
Original file line number Diff line number Diff line change
@@ -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");
}
2 changes: 1 addition & 1 deletion exercises/practice/resistor-color/.meta/example.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/resistor-color/resistor_color.zig
Original file line number Diff line number Diff line change
@@ -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");
}

Expand Down

0 comments on commit b7cd99f

Please sign in to comment.