diff --git a/README.md b/README.md index 255c564..ff91079 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/). -_733 TILs and counting..._ +_734 TILs and counting..._ --- @@ -524,6 +524,7 @@ _733 TILs and counting..._ - [Break Out Of A While Loop](reason/break-out-of-a-while-loop.md) - [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 Map Of Strings](reason/create-a-map-of-strings.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) diff --git a/reason/create-a-map-of-strings.md b/reason/create-a-map-of-strings.md new file mode 100644 index 0000000..6cdf1f5 --- /dev/null +++ b/reason/create-a-map-of-strings.md @@ -0,0 +1,28 @@ +# Create A Map Of Strings + +[ReasonML](https://reasonml.github.io/en) has the [`Map.Make` +functor](https://reasonml.github.io/api/Map.Make.html) in its standard +library which allows you to create a `Map` module with a specific key type. +Here is how we can make a map module with string keys. + +```reason +module StringMap = Map.Make(String); +``` + +We can then use that module to to create an empty map followed by adding +key-value pairs to it. + +``` +StringMap.empty +|> StringMap.add("Morty", "Smith") +|> StringMap.add("Rick", "Sanchez") +|> StringMap.add("Scary", "Terry") +|> StringMap.iter((first, last) => { + print_endline(Printf.sprintf("%s %s", first, last)); +}); +/* +Morty Smith +Rick Sanchez +Scary Terry +*/ +```