From 84eda7a1be76f9604b9fa314ad8660ed2157fb57 Mon Sep 17 00:00:00 2001 From: Hongbo Zhang Date: Thu, 13 Jun 2024 16:21:13 +0800 Subject: [PATCH] hongbo/get rid of T (#552) * remove usage of Array:: * tweak * add missing APIs for hashset * fmt --- array/array_test.mbt | 6 +-- array/fixedarray.mbt | 2 +- array/fixedarray_sort.mbt | 2 +- array/sort.mbt | 4 +- array/sort_by.mbt | 8 +-- builtin/array.mbt | 102 ++++++++++++++++++------------------- hashset/hashset.mbt | 13 +++++ hashset/hashset.mbti | 2 + hashset/hashset_test.mbt | 44 ++++++++-------- hashset/test/moon.pkg.json | 9 ++++ hashset/test/test.mbt | 22 ++++++++ hashset/test/test.mbti | 10 ++++ hashset/types.mbt | 2 +- immut/array/array.mbt | 4 +- immut/sorted_map/map.mbt | 2 +- 15 files changed, 142 insertions(+), 90 deletions(-) create mode 100644 hashset/test/moon.pkg.json create mode 100644 hashset/test/test.mbt create mode 100644 hashset/test/test.mbti diff --git a/array/array_test.mbt b/array/array_test.mbt index d120281b..539833aa 100644 --- a/array/array_test.mbt +++ b/array/array_test.mbt @@ -49,7 +49,7 @@ test "op_equal" { } test "pop" { - let v = Array::new() + let v = [] v.push(3) v.push(4) @assertion.assert_eq(v.length(), 2)? @@ -60,7 +60,7 @@ test "pop" { } test "pop_exn" { - let v = Array::new() + let v = [] v.push(3) v.push(4) @assertion.assert_eq(v.length(), 2)? @@ -175,7 +175,7 @@ test "filter" { } test "is_empty" { - let v = Array::new() + let v = [] @assertion.assert_true(v.is_empty())? v.push(3) @assertion.assert_false(v.is_empty())? diff --git a/array/fixedarray.mbt b/array/fixedarray.mbt index 66eb59a1..ad2893a9 100644 --- a/array/fixedarray.mbt +++ b/array/fixedarray.mbt @@ -327,7 +327,7 @@ pub fn FixedArray::from_array[T](array : Array[T]) -> FixedArray[T] { } test "from_array" { - let array = FixedArray::[1, 2, 3, 4, 5] + let array : FixedArray[_] = [1, 2, 3, 4, 5] @assertion.assert_eq(array, [1, 2, 3, 4, 5])? } diff --git a/array/fixedarray_sort.mbt b/array/fixedarray_sort.mbt index 6168a3e8..756edd09 100644 --- a/array/fixedarray_sort.mbt +++ b/array/fixedarray_sort.mbt @@ -52,7 +52,7 @@ fn timsort[T : Compare](arr : FixedArraySlice[T]) -> Unit { } let mut end = 0 let mut start = 0 - let runs : Array[TimSortRun] = Array::new() + let runs : Array[TimSortRun] = [] while end < len { let (streak_end, was_reversed) = find_streak(arr.slice(start, arr.end)) end += streak_end diff --git a/array/sort.mbt b/array/sort.mbt index 54021b3b..c55c5c11 100644 --- a/array/sort.mbt +++ b/array/sort.mbt @@ -19,9 +19,9 @@ /// # Example /// /// ``` -/// let arr = Array::[5, 4, 3, 2, 1] +/// let arr = [5, 4, 3, 2, 1] /// arr.sort() -/// debug(arr) //output: Array::[1, 2, 3, 4, 5] +/// debug(arr) //output: [1, 2, 3, 4, 5] /// ``` pub fn sort[T : Compare](self : Array[T]) -> Unit { quick_sort( diff --git a/array/sort_by.mbt b/array/sort_by.mbt index cf5b1610..d29b1a52 100644 --- a/array/sort_by.mbt +++ b/array/sort_by.mbt @@ -19,9 +19,9 @@ /// # Example /// /// ``` -/// let arr = Array::[5, 3, 2, 4, 1] +/// let arr = [5, 3, 2, 4, 1] /// arr.sort_by_key(fn (x) {-x}) -/// debug(arr) //output: Array::[5, 4, 3, 2, 1] +/// debug(arr) //output: [5, 4, 3, 2, 1] /// ``` pub fn sort_by_key[T, K : Compare](self : Array[T], map : (T) -> K) -> Unit { quick_sort_by( @@ -39,9 +39,9 @@ pub fn sort_by_key[T, K : Compare](self : Array[T], map : (T) -> K) -> Unit { /// # Example /// /// ``` -/// let arr = Array::[5, 3, 2, 4, 1] +/// let arr = [5, 3, 2, 4, 1] /// arr.sort_by(fn (a, b) { a - b }) -/// debug(arr) //output: Array::[1, 2, 3, 4, 5] +/// debug(arr) //output: [1, 2, 3, 4, 5] /// ``` pub fn sort_by[T](self : Array[T], cmp : (T, T) -> Int) -> Unit { quick_sort_by( diff --git a/builtin/array.mbt b/builtin/array.mbt index f92c8dff..34ca0fed 100644 --- a/builtin/array.mbt +++ b/builtin/array.mbt @@ -96,7 +96,7 @@ fn realloc[T](self : Array[T]) -> Unit { /// /// # Example /// ``` -/// let v = Array::new() +/// let v = [] /// v.push(3) /// println(v[0]) // 3 /// ``` @@ -115,7 +115,7 @@ pub fn op_get[T](self : Array[T], index : Int) -> T { /// /// # Example /// ``` -/// let v = Array::new() +/// let v = [] /// v.push(3) /// println(v.get(0)) // Some(3) /// ``` @@ -130,7 +130,7 @@ pub fn get[T](self : Array[T], index : Int) -> T? { /// /// # Example /// ``` -/// let v = Array::new() +/// let v = [] /// v.push(3) /// println(v[0]) // 3 /// ``` @@ -194,7 +194,7 @@ pub fn op_add[T](self : Array[T], other : Array[T]) -> Array[T] { /// /// # Example /// ``` -/// let v = Array::[1, 2, 3] +/// let v = [1, 2, 3] /// v.pop() /// ``` pub fn pop[T](self : Array[T]) -> T? { @@ -211,7 +211,7 @@ pub fn pop[T](self : Array[T]) -> T? { /// /// # Example /// ``` -/// let v = Array::[1, 2, 3] +/// let v = [1, 2, 3] /// v.pop_exn() // 3 /// ``` /// @alert unsafe "Panic if the vector is empty." @@ -229,7 +229,7 @@ pub fn pop_exn[T](self : Array[T]) -> T { /// /// # Example /// ``` -/// let v = Array::new() +/// let v = [] /// v.push(3) /// ``` pub fn push[T](self : Array[T], value : T) -> Unit { @@ -246,8 +246,8 @@ pub fn push[T](self : Array[T], value : T) -> Unit { /// /// # Example /// ``` -/// let v = Array::[3, 4, 5] -/// let vv = v.drain(1, 2) // vv = Array::[4], v = Array::[3, 5] +/// let v = [3, 4, 5] +/// let vv = v.drain(1, 2) // vv = [4], v = [3, 5] /// ``` /// @alert unsafe "Panic if index is out of bounds." pub fn drain[T](self : Array[T], begin : Int, end : Int) -> Array[T] { @@ -274,8 +274,8 @@ pub fn drain[T](self : Array[T], begin : Int, end : Int) -> Array[T] { /// /// # Example /// ``` -/// let v1 = Array::[3, 4, 5] -/// let v2 = Array::[6, 7, 8] +/// let v1 = [3, 4, 5] +/// let v2 = [6, 7, 8] /// v1.append(v2) /// ``` /// TODO: could be made more efficient @@ -424,7 +424,7 @@ pub fn filter[T](self : Array[T], f : (T) -> Bool) -> Array[T] { /// /// # Example /// ``` -/// let v = Array::new() +/// let v = [] /// v.is_empty() /// ``` pub fn is_empty[T](self : Array[T]) -> Bool { @@ -435,7 +435,7 @@ pub fn is_empty[T](self : Array[T]) -> Bool { /// /// # Example /// ``` -/// let v = Array::[3, 4, 5] +/// let v = [3, 4, 5] /// v.is_sorted() // true /// ``` pub fn is_sorted[T : Compare](self : Array[T]) -> Bool { @@ -454,7 +454,7 @@ pub fn is_sorted[T : Compare](self : Array[T]) -> Bool { /// /// # Example /// ``` -/// let v = Array::[3, 4, 5] +/// let v = [3, 4, 5] /// v.reverse() /// ``` pub fn reverse[T](self : Array[T]) -> Unit { @@ -469,7 +469,7 @@ pub fn reverse[T](self : Array[T]) -> Unit { /// /// # Example /// ``` -/// let v = Array::[3, 4, 5] +/// let v = [3, 4, 5] /// let (v1, v2) = v.split_at(1) /// ``` /// TODO: perf could be optimized @@ -496,7 +496,7 @@ pub fn split_at[T](self : Array[T], index : Int) -> (Array[T], Array[T]) { /// /// # Example /// ``` -/// let v = Array::[3, 4, 5] +/// let v = [3, 4, 5] /// v.contains(3) /// ``` pub fn contains[T : Eq](self : Array[T], value : T) -> Bool { @@ -513,8 +513,8 @@ pub fn contains[T : Eq](self : Array[T], value : T) -> Bool { /// /// # Example /// ``` -/// let v = Array::[3, 4, 5] -/// v.starts_with(Array::[3, 4]) +/// let v = [3, 4, 5] +/// v.starts_with([3, 4]) /// ``` pub fn starts_with[T : Eq](self : Array[T], prefix : Array[T]) -> Bool { if prefix.len > self.len { @@ -533,8 +533,8 @@ pub fn starts_with[T : Eq](self : Array[T], prefix : Array[T]) -> Bool { /// /// # Example /// ``` -/// let v = Array::[3, 4, 5] -/// v.ends_with(Array::[5]) +/// let v = [3, 4, 5] +/// v.ends_with([5]) /// ``` pub fn ends_with[T : Eq](self : Array[T], suffix : Array[T]) -> Bool { if suffix.len > self.len { @@ -555,8 +555,8 @@ pub fn ends_with[T : Eq](self : Array[T], suffix : Array[T]) -> Bool { /// /// # Example /// ``` -/// let v = Array::[3, 4, 5] -/// v.strip_prefix(Array::[3]) // Some(Array::[4, 5]) +/// let v = [3, 4, 5] +/// v.strip_prefix([3]) // Some([4, 5]) /// ``` pub fn strip_prefix[T : Eq](self : Array[T], prefix : Array[T]) -> Array[T]? { if self.starts_with(prefix) { @@ -577,8 +577,8 @@ pub fn strip_prefix[T : Eq](self : Array[T], prefix : Array[T]) -> Array[T]? { /// /// # Example /// ``` -/// let v = Array::[3, 4, 5] -/// v.strip_suffix(Array::[5]) // Some(Array::[3, 4]) +/// let v = [3, 4, 5] +/// v.strip_suffix([5]) // Some([3, 4]) /// ``` pub fn strip_suffix[T : Eq](self : Array[T], suffix : Array[T]) -> Array[T]? { if self.ends_with(suffix) { @@ -597,7 +597,7 @@ pub fn strip_suffix[T : Eq](self : Array[T], suffix : Array[T]) -> Array[T]? { /// /// # Example /// ``` -/// let v = Array::[3, 4, 5] +/// let v = [3, 4, 5] /// v.search(3) /// ``` pub fn search[T : Eq](self : Array[T], value : T) -> Int? { @@ -614,7 +614,7 @@ pub fn search[T : Eq](self : Array[T], value : T) -> Int? { /// /// # Example /// ``` -/// let v = Array::[3, 4, 5] +/// let v = [3, 4, 5] /// v.swap(1, 2) /// ``` /// @alert unsafe "Panic if index is out of bounds." @@ -636,7 +636,7 @@ pub fn swap[T](self : Array[T], i : Int, j : Int) -> Unit { /// /// # Example /// ``` -/// let v = Array::[3, 4, 5] +/// let v = [3, 4, 5] /// v.remove(1) /// ``` /// @alert unsafe "Panic if index is out of bounds." @@ -659,7 +659,7 @@ pub fn remove[T](self : Array[T], index : Int) -> T { /// /// # Example /// ``` -/// let v = Array::[3, 4, 5] +/// let v = [3, 4, 5] /// v.retain(fn(x) { x > 3 }) /// ``` /// TODO: perf could be improved @@ -678,7 +678,7 @@ pub fn retain[T](self : Array[T], f : (T) -> Bool) -> Unit { /// /// # Example /// ``` -/// Array::[3, 4, 5].resize(1) +/// [3, 4, 5].resize(1) /// ``` /// @alert unsafe "Panic if new length is negative." pub fn resize[T](self : Array[T], new_len : Int, f : T) -> Unit { @@ -698,7 +698,7 @@ pub fn resize[T](self : Array[T], new_len : Int, f : T) -> Unit { /// /// # Example /// ``` -/// Array::[3, 4, 5].insert(1, 6) +/// [3, 4, 5].insert(1, 6) /// ``` /// @alert unsafe "Panic if index is out of bounds." pub fn insert[T](self : Array[T], index : Int, value : T) -> Unit { @@ -748,10 +748,10 @@ pub fn fill_with[T](self : Array[T], f : () -> T) -> Unit { /// /// # Example /// ``` -/// Array::[Array::[3, 4], Array::[5, 6]].flatten() +/// [[3, 4], [5, 6]].flatten() /// ``` pub fn flatten[T](self : Array[Array[T]]) -> Array[T] { - let v = Array::new() + let v = [] for i = 0; i < self.len; i = i + 1 { v.append(self[i]) } @@ -762,7 +762,7 @@ pub fn flatten[T](self : Array[Array[T]]) -> Array[T] { /// /// # Example /// ``` -/// Array::[3, 4].repeat(2) +/// [3, 4].repeat(2) /// ``` pub fn repeat[T](self : Array[T], times : Int) -> Array[T] { let v = Array::with_capacity(self.len * times) @@ -776,10 +776,10 @@ pub fn repeat[T](self : Array[T], times : Int) -> Array[T] { /// /// # Example /// ``` -/// Array::[Array::[3, 4], Array::[5, 6]].join(0) +/// [[3, 4], [5, 6]].join(0) /// ``` pub fn join[T](self : Array[Array[T]], sep : T) -> Array[T] { - let v = Array::new() + let v = [] for i = 0; i < self.len; i = i + 1 { v.append(self[i]) if i < self.len - 1 { @@ -853,8 +853,8 @@ pub fn fold_righti[T, U](self : Array[T], f : (Int, U, T) -> U, ~init : U) -> U /// /// # Example /// ``` -/// let v = Array::[3, 4, 4, 5, 5, 5] -/// v.dedup() // v = Array::[3, 4, 5] +/// let v = [3, 4, 4, 5, 5, 5] +/// v.dedup() // v = [3, 4, 5] /// ``` pub fn dedup[T : Eq](self : Array[T]) -> Unit { for i = 0; i < self.len; i = i + 1 { @@ -874,12 +874,12 @@ pub fn dedup[T : Eq](self : Array[T]) -> Unit { /// This function will remove the elements from the original vector and return a new vector. /// # Example /// ``` -/// let v = Array::[3, 4, 5] -/// let vv = v.extract_if(fn(x) { x > 3 }) // vv = Array::[4, 5], v = Array::[3] +/// let v = [3, 4, 5] +/// let vv = v.extract_if(fn(x) { x > 3 }) // vv = [4, 5], v = [3] /// ``` pub fn extract_if[T](self : Array[T], f : (T) -> Bool) -> Array[T] { - let v = Array::new() - let indices = Array::new() + let v = [] + let indices = [] for i = 0; i < self.len; i = i + 1 { if f(self[i]) { v.push(self[i]) @@ -898,11 +898,11 @@ pub fn extract_if[T](self : Array[T], f : (T) -> Bool) -> Array[T] { /// /// # Example /// ``` -/// let v = Array::[1, 2, 3, 4, 5, 6, 7, 8, 9] -/// let chunks = v.chunks(3) // chunks = Array::[Array::[1, 2, 3], Array::[4, 5, 6], Array::[7, 8, 9]] +/// let v = [1, 2, 3, 4, 5, 6, 7, 8, 9] +/// let chunks = v.chunks(3) // chunks = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] /// ``` pub fn chunks[T](self : Array[T], size : Int) -> Array[Array[T]] { - let chunks = Array::new() + let chunks = [] let mut i = 0 while i < self.len { let chunk = Array::with_capacity(size) @@ -919,15 +919,15 @@ pub fn chunks[T](self : Array[T], size : Int) -> Array[Array[T]] { /// /// # Example /// ``` -/// let v = Array::[1, 1, 2, 3, 2, 3, 2, 3, 4] +/// let v = [1, 1, 2, 3, 2, 3, 2, 3, 4] /// let chunks = v.chunk_by(fn(x, y) { x <= y }) -/// // chunks = Array::[Array::[1, 1, 2, 3], Array::[2, 3], Array::[2, 3], Array::[4]] +/// // chunks = [[1, 1, 2, 3], [2, 3], [2, 3], [4]] /// ``` pub fn chunk_by[T](self : Array[T], pred : (T, T) -> Bool) -> Array[Array[T]] { - let chunks = Array::new() + let chunks = [] let mut i = 0 while i < self.len { - let chunk = Array::new() + let chunk = [] chunk.push(self[i]) i = i + 1 while i < self.len && pred(self[i - 1], self[i]) { @@ -943,14 +943,14 @@ pub fn chunk_by[T](self : Array[T], pred : (T, T) -> Bool) -> Array[Array[T]] { /// /// # Example /// ``` -/// let v = Array::[1, 0, 2, 0, 3, 0, 4] +/// let v = [1, 0, 2, 0, 3, 0, 4] /// let chunks = v.split(fn(x) { x == 0 }) /// ``` pub fn split[T](self : Array[T], pred : (T) -> Bool) -> Array[Array[T]] { - let chunks = Array::new() + let chunks = [] let mut i = 0 while i < self.len { - let chunk = Array::new() + let chunk = [] while i < self.len && pred(self[i]).not() { chunk.push(self[i]) i = i + 1 @@ -967,7 +967,7 @@ pub fn split[T](self : Array[T], pred : (T) -> Bool) -> Array[Array[T]] { /// # Example /// /// ``` -/// let v = Array::[1] +/// let v = [1] /// v.reserve_capacity(10) /// println(v.capacity()) // 10 /// ``` diff --git a/hashset/hashset.mbt b/hashset/hashset.mbt index 1491b242..a50630f9 100644 --- a/hashset/hashset.mbt +++ b/hashset/hashset.mbt @@ -32,6 +32,12 @@ pub fn HashSet::from_array[K : Hash + Eq](arr : Array[K]) -> HashSet[K] { m } +pub fn HashSet::of[K : Hash + Eq](arr : FixedArray[K]) -> HashSet[K] { + let m = new() + arr.iter(fn(e) { m.insert(e) }) + m +} + /// Insert a key into hash set. /// @alert unsafe "Panic if the hash set is full." pub fn insert[K : Hash + Eq](self : HashSet[K], key : K) -> Unit { @@ -286,3 +292,10 @@ fn debug_entries[K : Show](self : HashSet[K]) -> String { } s } + +pub fn to_string[K : Show](self : HashSet[K]) -> String { + let mut s = "@hashset.of(" + s += self.as_iter().collect().to_string() + s += ")" + s +} diff --git a/hashset/hashset.mbti b/hashset/hashset.mbti index e0cfb933..24a579a3 100644 --- a/hashset/hashset.mbti +++ b/hashset/hashset.mbti @@ -17,9 +17,11 @@ impl HashSet { iter[K](Self[K], (K) -> Unit) -> Unit iteri[K](Self[K], (Int, K) -> Unit) -> Unit new[K]() -> Self[K] + of[K : Hash + Eq](FixedArray[K]) -> Self[K] remove[K : Hash + Eq](Self[K], K) -> Unit size[K](Self[K]) -> Int symmetric_difference[K : Hash + Eq](Self[K], Self[K]) -> Self[K] + to_string[K : Show](Self[K]) -> String union[K : Hash + Eq](Self[K], Self[K]) -> Self[K] } diff --git a/hashset/hashset_test.mbt b/hashset/hashset_test.mbt index 39f88572..61ddab10 100644 --- a/hashset/hashset_test.mbt +++ b/hashset/hashset_test.mbt @@ -26,17 +26,13 @@ impl Show for MyString with to_string(self) { self.0 } test "set" { let m : HashSet[MyString] = HashSet::new() - fn i(s) { - MyString::MyString(s) - } - - m.insert("a" |> i) - m.insert("b" |> i) - m.insert("bc" |> i) - m.insert("abc" |> i) - m.insert("cd" |> i) - m.insert("c" |> i) - m.insert("d" |> i) + m.insert("a") + m.insert("b") + m.insert("bc") + m.insert("abc") + m.insert("cd") + m.insert("c") + m.insert("d") @assertion.assert_eq(m.size, 7)? @assertion.assert_eq( m.debug_entries(), @@ -56,7 +52,7 @@ test "insert" { } test "from_array" { - let m = HashSet::["a", "b", "c"] + let m = of(["a", "b", "c"]) @assertion.assert_true(m.contains("a"))? @assertion.assert_true(m.contains("b"))? @assertion.assert_true(m.contains("c"))? @@ -114,14 +110,14 @@ test "is_empty" { } test "iter" { - let m : HashSet[String] = HashSet::["a", "b", "c"] + let m : HashSet[String] = HashSet::of(["a", "b", "c"]) let mut sum = "" m.iter(fn(k) { sum += k }) @assertion.assert_eq(sum, "abc")? } test "iteri" { - let m : HashSet[String] = HashSet::["1", "2", "3"] + let m = of(["1", "2", "3"]) let mut s = "" let mut sum = 0 m.iteri( @@ -135,7 +131,7 @@ test "iteri" { } test "clear" { - let m : HashSet[String] = HashSet::["a", "b", "c"] + let m = of(["a", "b", "c"]) m.clear() @assertion.assert_eq(m.size, 0)? @assertion.assert_eq(m.capacity, 8)? @@ -173,8 +169,8 @@ test "grow" { } test "union" { - let m1 : HashSet[String] = HashSet::["a", "b", "c"] - let m2 : HashSet[String] = HashSet::["b", "c", "d"] + let m1 : HashSet[String] = of(["a", "b", "c"]) + let m2 : HashSet[String] = of(["b", "c", "d"]) let m = m1.union(m2) @assertion.assert_eq(m.size, 4)? @assertion.assert_true(m.contains("a"))? @@ -184,8 +180,8 @@ test "union" { } test "intersection" { - let m1 : HashSet[String] = HashSet::["a", "b", "c"] - let m2 : HashSet[String] = HashSet::["b", "c", "d"] + let m1 : HashSet[String] = of(["a", "b", "c"]) + let m2 : HashSet[String] = of(["b", "c", "d"]) let m = m1.intersection(m2) @assertion.assert_eq(m.size, 2)? @assertion.assert_false(m.contains("a"))? @@ -195,8 +191,8 @@ test "intersection" { } test "difference" { - let m1 : HashSet[String] = HashSet::["a", "b", "c"] - let m2 : HashSet[String] = HashSet::["b", "c", "d"] + let m1 : HashSet[String] = of(["a", "b", "c"]) + let m2 : HashSet[String] = of(["b", "c", "d"]) let m = m1.difference(m2) @assertion.assert_eq(m.size, 1)? @assertion.assert_true(m.contains("a"))? @@ -206,8 +202,8 @@ test "difference" { } test "symmetric_difference" { - let m1 : HashSet[String] = HashSet::["a", "b", "c"] - let m2 : HashSet[String] = HashSet::["b", "c", "d"] + let m1 : HashSet[String] = of(["a", "b", "c"]) + let m2 : HashSet[String] = of(["b", "c", "d"]) let m = m1.symmetric_difference(m2) @assertion.assert_eq(m.size, 2)? @assertion.assert_true(m.contains("a"))? @@ -218,7 +214,7 @@ test "symmetric_difference" { test "as_iter" { let buf = Buffer::make(20) - let map = HashSet::["a", "b", "c"] + let map = of(["a", "b", "c"]) map.as_iter().iter(fn(e) { buf.write_string("[\(e)]") }) inspect(buf, content="[a][b][c]")? buf.reset() diff --git a/hashset/test/moon.pkg.json b/hashset/test/moon.pkg.json new file mode 100644 index 00000000..4730cca1 --- /dev/null +++ b/hashset/test/moon.pkg.json @@ -0,0 +1,9 @@ +{ + "import": [ + "moonbitlang/core/builtin", + "moonbitlang/core/int", + "moonbitlang/core/array", + "moonbitlang/core/coverage", + "moonbitlang/core/hashset" + ] +} \ No newline at end of file diff --git a/hashset/test/test.mbt b/hashset/test/test.mbt new file mode 100644 index 00000000..64117f4a --- /dev/null +++ b/hashset/test/test.mbt @@ -0,0 +1,22 @@ +// Copyright 2024 International Digital Economy Academy +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +test "doc" { + let set = @hashset.of([3, 8, 1]) + set.insert(3) + set.insert(4) + inspect(set, content="@hashset.of([3, 4, 1, 8])")? +} +/// +/// diff --git a/hashset/test/test.mbti b/hashset/test/test.mbti new file mode 100644 index 00000000..777e1b85 --- /dev/null +++ b/hashset/test/test.mbti @@ -0,0 +1,10 @@ +package moonbitlang/core/hashset/test + +// Values + +// Types and methods + +// Traits + +// Extension Methods + diff --git a/hashset/types.mbt b/hashset/types.mbt index d53d8d6e..00500cc1 100644 --- a/hashset/types.mbt +++ b/hashset/types.mbt @@ -29,7 +29,7 @@ priv struct Entry[K] { /// # Example /// /// ``` -/// let set = HashSet::[(3, "three"), (8, "eight"), (1, "one")] +/// let set = @hashset.of([(3, "three"), (8, "eight"), (1, "one")]) /// set.insert(3) /// println(set.contains(3)) // output: true /// ``` diff --git a/immut/array/array.mbt b/immut/array/array.mbt index c559bdaa..6b69fb42 100644 --- a/immut/array/array.mbt +++ b/immut/array/array.mbt @@ -301,7 +301,7 @@ test "iteri" { let mut s = 0 v.iteri(fn(i, e) { s = s + i * e }) inspect(s, content="40")? - let vec = Array::new() + let vec = [] v.iteri(fn(i, _e) { vec.push(i) }) inspect(vec, content="[0, 1, 2, 3, 4]")? } @@ -538,7 +538,7 @@ test "mix" { for i = 0; i < large_const; i = i + 1 { v = v.push(i) } - let vec = Array::new() + let vec = [] v.iteri(fn(i, _e) { vec.push(i) }) for i = 0; i < large_const; i = i + 1 { @assertion.assert_eq(vec[i], i)? diff --git a/immut/sorted_map/map.mbt b/immut/sorted_map/map.mbt index 5393bcf2..2ebb2e59 100644 --- a/immut/sorted_map/map.mbt +++ b/immut/sorted_map/map.mbt @@ -111,7 +111,7 @@ pub fn filter_with_key[K : Compare, V]( /// Convert to an array of key-value pairs. pub fn to_array[K, V](self : Map[K, V]) -> Array[(K, V)] { - let arr = Array::new() + let arr = [] self.iter(fn(k, v) { arr.push((k, v)) }) arr }