Skip to content

Commit

Permalink
Merge branch 'moonbitlang:main' into map/utils
Browse files Browse the repository at this point in the history
  • Loading branch information
yj-qin authored Mar 19, 2024
2 parents c770626 + 4b67410 commit 6736501
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion map/map.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn singleton[K, V](key : K, value : V) -> Map[K, V] {

/// Check if the map contains a key.
/// O(log n).
pub fn member[K : Compare + Show, V : Show](self : Map[K, V], key : K) -> Bool {
pub fn member[K : Compare, V : Show](self : Map[K, V], key : K) -> Bool {
loop self {
Empty => false
Tree(k, _, _, l, r) => {
Expand Down
2 changes: 0 additions & 2 deletions math/algebraic.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ test "maximum.ref" {
// We need another value that equals to x2 by value but not reference
let x2t = "v\(v2)"
@assertion.assert_is_not(x2, x2t).unwrap()

@assertion.assert_is(maximum(x1, x2), x2)?
@assertion.assert_is(maximum(x2, x1), x2)?
@assertion.assert_is(maximum(x2, x2t), x2t)?
Expand Down Expand Up @@ -85,7 +84,6 @@ test "minimum.ref" {
// We need another value that equals to x2 by value but not reference
let x2t = "v\(v2)"
@assertion.assert_is_not(x2, x2t).unwrap()

@assertion.assert_is(minimum(x1, x2), x1)?
@assertion.assert_is(minimum(x2, x1), x1)?
@assertion.assert_is(minimum(x2, x2t), x2)?
Expand Down
24 changes: 24 additions & 0 deletions vec/vec.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,30 @@ struct Vec[T] {
mut len : Int
}

pub fn to_string[T : Show](self : Vec[T]) -> String {
if self.len == 0 {
return "Vec::[]"
}
let first = self.buf[0]
for i = 1, init = "Vec::[\(first)"; i < self.len; {
let cur = self.buf[i]
continue i + 1, "\(init),\(cur)"
} else {
"\(init)]"
}
}

test "to_string" {
let empty : Vec[Int] = Vec::[]
@assertion.assert_eq(empty.to_string(), "Vec::[]")?
let a0 = Vec::[0]
@assertion.assert_eq(a0.to_string(), "Vec::[0]")?
a0.push(1)
@assertion.assert_eq(a0.to_string(), "Vec::[0,1]")?
a0.push(2)
@assertion.assert_eq(a0.to_string(), "Vec::[0,1,2]")?
}

/// Creates a new, empty vector.
pub fn Vec::new[T]() -> Vec[T] {
Vec::{ buf: UninitializedArray::make(0), len: 0 }
Expand Down

0 comments on commit 6736501

Please sign in to comment.