Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deprecate: inconsistent api @sorted_map.of #1593

Merged
merged 2 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions sorted_map/map.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,20 @@ pub fn new[K, V]() -> T[K, V] {
}

///|
/// Creates a sorted map from given entries.
/// @alert deprecated "Use @sorted_map.from_array instead"
pub fn of[K : Compare, V](entries : Array[(K, V)]) -> T[K, V] {
let map = { root: None, size: 0 }
entries.each(fn(e) { map.add(e.0, e.1) })
map
}

///| Creates a sorted map from a array of key-value pairs.
pub fn from_array[K : Compare, V](entries : Array[(K, V)]) -> T[K, V] {
let map = { root: None, size: 0 }
entries.each(fn(e) { map.add(e.0, e.1) })
map
}

///|
/// Inserts a key-value pair.
pub fn add[K : Compare, V](self : T[K, V], key : K, value : V) -> Unit {
Expand Down Expand Up @@ -454,7 +461,7 @@ test "new" {
}

test "of" {
let map = of([(3, "c"), (2, "b"), (1, "a")])
let map = from_array([(3, "c"), (2, "b"), (1, "a")])
inspect!(map.debug_tree(), content="([2]2,b,([1]1,a,_,_),([1]3,c,_,_))")
inspect!(map.size(), content="3")
}
Expand Down Expand Up @@ -514,14 +521,14 @@ test "add4" {
}

test "add duplicate key" {
let map = of([(3, "c"), (2, "b"), (1, "a")])
let map = from_array([(3, "c"), (2, "b"), (1, "a")])
inspect!(map.debug_tree(), content="([2]2,b,([1]1,a,_,_),([1]3,c,_,_))")
map.add(1, "x")
inspect!(map.debug_tree(), content="([2]2,b,([1]1,x,_,_),([1]3,c,_,_))")
}

test "remove" {
let map = of([
let map = from_array([
(1, "a"),
(2, "b"),
(3, "c"),
Expand Down Expand Up @@ -583,7 +590,7 @@ test "op_set" {
}

test "clear" {
let map = of([(3, "c"), (2, "b"), (1, "a")])
let map = from_array([(3, "c"), (2, "b"), (1, "a")])
map.clear()
inspect!(map.debug_tree(), content="_")
inspect!(map.size(), content="0")
Expand Down
40 changes: 23 additions & 17 deletions sorted_map/map_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
// limitations under the License.

test "remove2" {
let map = @sorted_map.of([(1, "a"), (2, "b")])
let map = @sorted_map.from_array([(1, "a"), (2, "b")])
inspect!(map.size(), content="2")
map.remove(1)
inspect!(map.size(), content="1")
}

test "remove3" {
let map = @sorted_map.of([(1, "a"), (2, "b"), (3, "c")])
let map = @sorted_map.from_array([(1, "a"), (2, "b"), (3, "c")])
map.remove(0)
inspect!(map.size(), content="3")
map.remove(4)
Expand All @@ -34,7 +34,7 @@ test "remove on empty map" {
}

test "get" {
let map = @sorted_map.of([(3, "c"), (2, "b"), (1, "a")])
let map = @sorted_map.from_array([(3, "c"), (2, "b"), (1, "a")])
inspect!(
map.get(1),
content=
Expand All @@ -57,15 +57,15 @@ test "get" {
}

test "contains" {
let map = @sorted_map.of([(3, "c"), (2, "b"), (1, "a")])
let map = @sorted_map.from_array([(3, "c"), (2, "b"), (1, "a")])
inspect!(map.contains(1), content="true")
inspect!(map.contains(2), content="true")
inspect!(map.contains(3), content="true")
inspect!(map.contains(4), content="false")
}

test "op_get" {
let map = @sorted_map.of([(3, "c"), (2, "b"), (1, "a")])
let map = @sorted_map.from_array([(3, "c"), (2, "b"), (1, "a")])
inspect!(
map[1],
content=
Expand All @@ -88,8 +88,8 @@ test "op_get" {
}

test "op_equal" {
let a = @sorted_map.of([(3, "c"), (2, "b"), (1, "a")])
let b = @sorted_map.of([(4, "d"), (5, "e"), (6, "f")])
let a = @sorted_map.from_array([(3, "c"), (2, "b"), (1, "a")])
let b = @sorted_map.from_array([(4, "d"), (5, "e"), (6, "f")])
inspect!(a == a, content="true")
inspect!(b == b, content="true")
inspect!(a == b, content="false")
Expand All @@ -104,31 +104,31 @@ test "is_empty" {
}

test "size" {
let map = @sorted_map.of([(3, "c"), (2, "b"), (1, "a")])
let map = @sorted_map.from_array([(3, "c"), (2, "b"), (1, "a")])
inspect!(map.size() == map.size(), content="true")
}

test "each" {
let map = @sorted_map.of([(3, "c"), (2, "b"), (1, "a")])
let map = @sorted_map.from_array([(3, "c"), (2, "b"), (1, "a")])
let buf = StringBuilder::new()
map.each(fn(k, v) { buf.write_string("\{k}\{v}") })
inspect!(buf.to_string(), content="1a2b3c")
}

test "eachi" {
let map = @sorted_map.of([(3, "c"), (2, "b"), (1, "a")])
let map = @sorted_map.from_array([(3, "c"), (2, "b"), (1, "a")])
let buf = StringBuilder::new()
map.eachi(fn(i, k, v) { buf.write_string("[\{i}]\{k}\{v}") })
inspect!(buf.to_string(), content="[0]1a[1]2b[2]3c")
}

test "keys" {
let map = @sorted_map.of([(3, "c"), (2, "b"), (1, "a")])
let map = @sorted_map.from_array([(3, "c"), (2, "b"), (1, "a")])
inspect!(map.keys(), content="[1, 2, 3]")
}

test "values" {
let map = @sorted_map.of([(3, "c"), (2, "b"), (1, "a")])
let map = @sorted_map.from_array([(3, "c"), (2, "b"), (1, "a")])
inspect!(
map.values(),
content=
Expand All @@ -138,7 +138,7 @@ test "values" {
}

test "to_array" {
let map = @sorted_map.of([(3, "c"), (2, "b"), (1, "a")])
let map = @sorted_map.from_array([(3, "c"), (2, "b"), (1, "a")])
inspect!(
map.to_array(),
content=
Expand All @@ -148,7 +148,7 @@ test "to_array" {
}

test "iter" {
let map = @sorted_map.of([(3, "c"), (2, "b"), (1, "a")])
let map = @sorted_map.from_array([(3, "c"), (2, "b"), (1, "a")])
inspect!(
map.iter().collect(),
content=
Expand Down Expand Up @@ -195,7 +195,7 @@ test "arbitrary" {
}

test "iter2" {
let map = @sorted_map.of([(3, "c"), (2, "b"), (1, "a")])
let map = @sorted_map.from_array([(3, "c"), (2, "b"), (1, "a")])
let buf = StringBuilder::new()
for k, v in map {
buf.write_string("[\{k}\{v}]")
Expand All @@ -204,7 +204,7 @@ test "iter2" {
}

test "iter" {
let map = @sorted_map.of([(3, "c"), (2, "b"), (1, "a")])
let map = @sorted_map.from_array([(3, "c"), (2, "b"), (1, "a")])
let buf = StringBuilder::new()
for kv in map {
buf.write_string("[\{kv.0}\{kv.1}]")
Expand All @@ -213,7 +213,13 @@ test "iter" {
}

test "range" {
let map = @sorted_map.of([(1, "a"), (2, "b"), (3, "c"), (4, "d"), (5, "e")])
let map = @sorted_map.from_array([
(1, "a"),
(2, "b"),
(3, "c"),
(4, "d"),
(5, "e"),
])
let result = map.range(2, 4)
let buf = StringBuilder::new()
for k, v in result {
Expand Down
4 changes: 3 additions & 1 deletion sorted_map/sorted_map.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package moonbitlang/core/sorted_map
alias @moonbitlang/core/quickcheck as @quickcheck

// Values
fn from_array[K : Compare, V](Array[(K, V)]) -> T[K, V]

fn from_iter[K : Compare, V](Iter[(K, V)]) -> T[K, V]

fn new[K, V]() -> T[K, V]

fn of[K : Compare, V](Array[(K, V)]) -> T[K, V]
fn of[K : Compare, V](Array[(K, V)]) -> T[K, V] //deprecated

// Types and methods
type T
Expand Down
Loading