mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 07:08:01 +00:00
Add Creating A 2D Array as a reason til
This commit is contained in:
36
reason/creating-a-2d-array.md
Normal file
36
reason/creating-a-2d-array.md
Normal file
@@ -0,0 +1,36 @@
|
||||
# Creating A 2D Array
|
||||
|
||||
In most languages if I wanted to create a two-dimensional array, I would
|
||||
utilize some nested looping construct to generate columns of rows. The
|
||||
[ReasonML `Array` module](https://reasonml.github.io/api/Array.html)
|
||||
abstracts this away.
|
||||
|
||||
```reason
|
||||
let grid = Array.make_matrix(10, 10, 0);
|
||||
|
||||
grid
|
||||
|> Array.iter(column => {
|
||||
column
|
||||
|> Array.iter(cell => {
|
||||
print_int(cell);
|
||||
});
|
||||
print_endline("");
|
||||
});
|
||||
|
||||
/*
|
||||
0000000000
|
||||
0000000000
|
||||
0000000000
|
||||
0000000000
|
||||
0000000000
|
||||
0000000000
|
||||
0000000000
|
||||
0000000000
|
||||
0000000000
|
||||
0000000000
|
||||
*/
|
||||
```
|
||||
|
||||
The `make_matrix` function allows you to specify dimensions of a
|
||||
two-dimensional array with all positions initialized to the same value --
|
||||
that third argument.
|
||||
Reference in New Issue
Block a user