Skip to content

Commit

Permalink
update docs (#575)
Browse files Browse the repository at this point in the history
Co-authored-by: bob.hongbo.zhang <[email protected]>
  • Loading branch information
bobzhang and bob.hongbo.zhang authored Jun 20, 2024
1 parent f4af277 commit 1b3cdcf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
18 changes: 9 additions & 9 deletions builtin/LinkedHashMap.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ A mutable linked hash map based on a Robin Hood hash table, links all entry node
You can create an empty map using `new()` or construct it using `from_array()`.

```moonbit
let map1 : Map[String, Int] = Map::new()
let map2 = Map::[("one", 1), ("two", 2), ("three", 3), ("four", 4), ("five", 5)]
let map1 : Map[String, Int] = {}
let map2 = { "one" : 1 , "two" : 2, "three" : 3, "four" : 4, "five" : 5}
```

### Set & Get

You can use `set()` to add a key-value pair to the map, and use `get()` to get a key-value pair.

```moonbit
let map : Map[String, Int] = Map::new()
let map : Map[String, Int] = {}
map.set("a", 1)
map.get("a") // 1
map.get_or_default("a", 0) // 1
Expand All @@ -32,7 +32,7 @@ map.get_or_default("b", 0) // 0
You can use `remove()` to remove a key-value pair.

```moonbit
let map = Map::[("a", 1), ("b", 2), ("c", 3)]
let map = {"a" : 1, "b" : 2, "c" : 3}
map.remove("a")
```

Expand All @@ -41,7 +41,7 @@ map.remove("a")
You can use `contains()` to check whether a key exists.

```moonbit
let map = Map::[("a", 1), ("b", 2), ("c", 3)]
let map = {"a" : 1, "b" : 2, "c" : 3}
map.contains("a") // true
map.contains("d") // false
```
Expand All @@ -51,15 +51,15 @@ map.contains("d") // false
You can use `size()` to get the number of key-value pairs in the map, or `capacity()` to get the current capacity.

```moonbit
let map = Map::[("a", 1), ("b", 2), ("c", 3)]
let map = {"a" : 1, "b": 2, "c": 3}
map.size() // 3
map.capacity() // 8
```

Similarly, you can use `is_empty()` to check whether the map is empty.

```moonbit
let map : Map[String, Int] = Map::new()
let map : Map[String, Int] = {}
map.is_empty() // true
```

Expand All @@ -68,7 +68,7 @@ map.is_empty() // true
You can use `clear` to remove all key-value pairs from the map, but the allocated memory will not change.

```moonbit
let map = Map::[("a", 1), ("b", 2), ("c", 3)]
let map = {"a": 1, "b": 2, "c": 3}
map.clear()
map.is_empty() // true
```
Expand All @@ -78,7 +78,7 @@ map.is_empty() // true
You can use `iter()` or `iteri()` to iterate through all key-value pairs in the order of insertion.

```moonbit
let map = Map::[("a", 1), ("b", 2), ("c", 3)]
let map = {"a": 1, "b": 2, "c": 3}
map.iter(fn(k, v) { println("\(k)-\(v)") })
map.iteri(fn(i, k, v) { println("\(i)-\(k)-\(v)") })
```
2 changes: 1 addition & 1 deletion builtin/linkedhashmap.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ priv struct ListNode[K, V] {
/// # Example
///
/// ```
/// let map = HashMap::[(3, "three"), (8, "eight"), (1, "one")]
/// let map = { 3: "three", 8 : "eight", 1 : "one"}
/// println(map.get(2)) // output: None
/// println(map.get(3)) // output: Some("three")
/// map.set(3, "updated")
Expand Down

0 comments on commit 1b3cdcf

Please sign in to comment.