Skip to content

Commit

Permalink
Update to roc nightly, change Nat to U64
Browse files Browse the repository at this point in the history
  • Loading branch information
mulias committed Aug 1, 2024
1 parent b790209 commit 4d26d15
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.envrc
*/**/main
12 changes: 6 additions & 6 deletions package/Array2D.roc
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ expect empty {} |> toList |> List.len == 0

## Return an [identity matrix](https://en.wikipedia.org/wiki/Identity_matrix)
## with the specified number of `1`s along the diagonal.
identity : Nat -> Array2D (Num *)
identity : U64 -> Array2D (Num *)
identity = \ones ->
init { rows: ones, cols: ones } \{ row, col } -> if row == col then 1 else 0

Expand Down Expand Up @@ -259,7 +259,7 @@ expect repeat 0 { rows: 3, cols: 4 } |> shape == { rows: 3, cols: 4 }
expect empty {} |> shape == { rows: 0, cols: 0 }

## Get the total number of elements in an array.
size : Array2D * -> Nat
size : Array2D * -> U64
size = \@Array2D array -> Shape2D.size array.shape

expect repeat 0 { rows: 3, cols: 4 } |> size == 12
Expand Down Expand Up @@ -765,7 +765,7 @@ expect

## Run the given function on each element of an array and return the number of
## elements for which the function returned Bool.true.
countIf : Array2D a, (a -> Bool) -> Nat
countIf : Array2D a, (a -> Bool) -> U64
countIf = \@Array2D array, fn -> List.countIf array.data fn

## Return the index of the first element in the array satisfying a predicate
Expand Down Expand Up @@ -934,18 +934,18 @@ expect
a = fromLists [[1, 2, 3], [4, 5, 6]] FitShortest
mul a (identity 3) == Ok a

listIndexOf : Shape2D, Index2D -> Result Nat [OutOfBounds]
listIndexOf : Shape2D, Index2D -> Result U64 [OutOfBounds]
listIndexOf = \arrayShape, index ->
if Shape2D.hasIndex arrayShape index then
Ok ((index.row * arrayShape.cols) + index.col)
else
Err OutOfBounds

arrayIndexOf : Nat, Shape2D -> Index2D
arrayIndexOf : U64, Shape2D -> Index2D
arrayIndexOf = \index, { cols } ->
{ row: index // cols, col: index % cols }

resize : List a, a, Nat -> List a
resize : List a, a, U64 -> List a
resize = \list, defaultValue, newLen ->
oldLen = List.len list
if newLen < oldLen then
Expand Down
10 changes: 5 additions & 5 deletions package/Index2D.roc
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ interface Index2D exposes [
]

## Two-dimensional index for referencing elements in an `Array2D`.
Index2D : { row : Nat, col : Nat }
Index2D : { row : U64, col : U64 }

## Add two indices, returning a new index with the sum of the two `row` values
## Add indices, returning a new index with the sum of the two `row` values
## and sum of the two `col` values as component parts. If the resulting index
## is not within the array shape then return `Err OutOfBounds`.
add : Index2D, Index2D, Shape2D -> Result Index2D [OutOfBounds]
Expand All @@ -39,9 +39,9 @@ add = \indexA, indexB, shape ->
else
Err OutOfBounds

## Add two indices, returning a new index with the difference of the two `row` values
## and difference of the two `col` values as component parts. If the resulting
## index is not within the array shape then return `Err OutOfBounds`.
## Subtract indices, returning a new index with the difference of the two `row`
## values and difference of the two `col` values as component parts. If the
## resulting index is not within the array shape then return `Err OutOfBounds`.
sub : Index2D, Index2D, Shape2D -> Result Index2D [OutOfBounds]
sub = \indexA, indexB, shape ->
result =
Expand Down
6 changes: 3 additions & 3 deletions package/Shape2D.roc
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ interface Shape2D exposes [

## Dimensions of an `Array2D` where `rows` is the total number of rows and
## `cols` is that total number of columns.
Shape2D : { rows : Nat, cols : Nat }
Shape2D : { rows : U64, cols : U64 }

Index2D : { row : Nat, col : Nat }
Index2D : { row : U64, col : U64 }

## Predicate to determine if an array shape can contain indices. When the shape
## has zero rows or zero columns then there is no index that can fall within the
Expand All @@ -23,5 +23,5 @@ hasIndex = \shape, index ->
index.row < shape.rows && index.col < shape.cols

## Total number of elements within the array shape.
size : Shape2D -> Nat
size : Shape2D -> U64
size = \{ rows, cols } -> rows * cols

0 comments on commit 4d26d15

Please sign in to comment.