diff --git a/README.md b/README.md index 1daf1ad..6d3e82d 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/). For a steady stream of TILs from a variety of rocketeers, checkout [til.hashrocket.com](https://til.hashrocket.com/). -_730 TILs and counting..._ +_731 TILs and counting..._ --- @@ -523,6 +523,7 @@ _730 TILs and counting..._ - [Compile Reason To Native With Dune](reason/compile-reason-to-native-with-dune.md) - [Compile Reason With An OCaml Package Using Dune](reason/compile-reason-with-an-ocaml-package-using-dune.md) - [Create A Stream From An Array](reason/create-a-stream-from-an-array.md) +- [Creating A 2D Array](reason/creating-a-2d-array.md) - [Data Structures With Self-Referential Types](reason/data-structures-with-self-referential-types.md) - [Defining Variants With Constructor Arguments](reason/defining-variants-with-constructor-arguments.md) - [Dynamically Create A Printf String Format](reason/dynamically-create-a-printf-string-format.md) diff --git a/reason/creating-a-2d-array.md b/reason/creating-a-2d-array.md new file mode 100644 index 0000000..9f6267e --- /dev/null +++ b/reason/creating-a-2d-array.md @@ -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.