1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 23:28:02 +00:00

Add Create A Map Of Strings as a reason til

This commit is contained in:
jbranchaud
2018-12-23 17:30:49 -06:00
parent 0f02830943
commit 63213b6726
2 changed files with 30 additions and 1 deletions

View File

@@ -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
*/
```