Skip to content

change return type of @sorted_map.{keys, values} to Iter #2123

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions immut/sorted_map/README.mbt.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Also, you can construct it from an array using `of()` or `from_array()`.
test {
let map = @sorted_map.of([("a", 1), ("b", 2), ("c", 3)])
assert_eq(map.values().collect(), [1, 2, 3])
assert_eq(map.keys(), ["a", "b", "c"])
assert_eq(map.keys_as_iter().collect(), ["a", "b", "c"])
}
```

Expand Down Expand Up @@ -131,10 +131,10 @@ test {
let map = @sorted_map.of([("a", 1), ("b", 2), ("c", 3)])
let map = map.filter((v) => { v > 1 })
assert_eq(map.values().collect(), [2, 3])
assert_eq(map.keys(), ["b", "c"])
assert_eq(map.keys_as_iter().collect(), ["b", "c"])
let map = map.filter_with_key((k, v) => { k > "a" && v > 1 })
assert_eq(map.values().collect(), [2, 3])
assert_eq(map.keys(), ["b", "c"])
assert_eq(map.keys_as_iter().collect(), ["b", "c"])
}
```

Expand All @@ -155,8 +155,8 @@ Use `keys()` to get all keys of the map in ascending order.
```moonbit
test {
let map = @sorted_map.of([("a", 1), ("b", 2), ("c", 3)])
let keys = map.keys() // ["a", "b", "c"]
assert_eq(keys, ["a", "b", "c"])
let keys = map.keys_as_iter() // ["a", "b", "c"]
assert_eq(keys.collect(), ["a", "b", "c"])
}
```

2 changes: 1 addition & 1 deletion immut/sorted_map/map_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ test "to_array" {
///|
test "keys" {
let m = @sorted_map.of([(1, "one"), (2, "two"), (3, "three")])
assert_eq(m.keys(), [1, 2, 3])
assert_eq(m.keys_as_iter().collect(), [1, 2, 3])
}

///|
Expand Down
2 changes: 2 additions & 0 deletions immut/sorted_map/sorted_map.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ fn[K : Compare, V] T::insert(Self[K, V], K, V) -> Self[K, V]
fn[K, V] T::is_empty(Self[K, V]) -> Bool
fn[K, V] T::iter(Self[K, V]) -> Iter[(K, V)]
fn[K, V] T::iter2(Self[K, V]) -> Iter2[K, V]
#deprecated
fn[K, V] T::keys(Self[K, V]) -> Array[K]
fn[K, V] T::keys_as_iter(Self[K, V]) -> Iter[K]
#deprecated
fn[K : Compare, V] T::lookup(Self[K, V], K) -> V?
fn[K, X, Y] T::map(Self[K, X], (X) -> Y) -> Self[K, Y]
Expand Down
8 changes: 8 additions & 0 deletions immut/sorted_map/utils.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,18 @@ pub fn[K : Compare, V] from_iter(iter : Iter[(K, V)]) -> T[K, V] {

///|
/// Return all keys of the map in ascending order.
#deprecated("Use `keys_as_iter` instead. `keys` will return `Iter[K]` instead of `Array[K]` in the future.")
#coverage.skip
pub fn[K, V] keys(self : T[K, V]) -> Array[K] {
self.iter().map(p => p.0).collect()
}

///|
/// Return all keys of the map in ascending order.
pub fn[K, V] keys_as_iter(self : T[K, V]) -> Iter[K] {
self.iter().map(p => p.0)
}

///|
/// Return all elements of the map in the ascending order of their keys.
pub fn[K, V] values(self : T[K, V]) -> Iter[V] {
Expand Down
4 changes: 2 additions & 2 deletions sorted_map/README.mbt.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ Get all keys or values from the map.
```moonbit
test {
let map = @sorted_map.from_array([(3, "three"), (1, "one"), (2, "two")])
assert_eq(map.keys(), [1, 2, 3])
assert_eq(map.values(), ["one", "two", "three"])
assert_eq(map.keys_as_iter().collect(), [1, 2, 3])
assert_eq(map.values_as_iter().collect(), ["one", "two", "three"])
}
```

Expand Down
18 changes: 18 additions & 0 deletions sorted_map/deprecated.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,21 @@ pub fn[K : Compare, V] of(entries : Array[(K, V)]) -> T[K, V] {
entries.each(e => map.add(e.0, e.1))
map
}

///|
#deprecated("Use `keys_as_iter` instead. `keys` will return `Iter[K]` instead of `Array[K]` in the future.")
#coverage.skip
pub fn[K, V] keys(self : T[K, V]) -> Array[K] {
let keys = Array::new(capacity=self.size)
self.each(fn(k, _v) { keys.push(k) })
keys
}

///|
#deprecated("Use `values_as_iter` instead. `values` will return `Iter[V]` instead of `Array[V]` in the future.")
#coverage.skip
pub fn[K, V] values(self : T[K, V]) -> Array[V] {
let values = Array::new(capacity=self.size)
self.each(fn(_k, v) { values.push(v) })
values
}
44 changes: 36 additions & 8 deletions sorted_map/map.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,46 @@ pub fn[K, V] eachi(

///|
/// Returns all keys in the map.
pub fn[K, V] keys(self : T[K, V]) -> Array[K] {
let keys = Array::new(capacity=self.size)
self.each((k, _v) => keys.push(k))
keys
pub fn[K, V] keys_as_iter(self : T[K, V]) -> Iter[K] {
Iter::new(fn(yield_) {
fn go(x : Node[K, V]?) {
match x {
None => IterContinue
Some({ left, right, key, .. }) =>
if go(left) is IterEnd {
IterEnd
} else if yield_(key) is IterEnd {
IterEnd
} else {
go(right)
}
}
}

go(self.root)
})
}

///|
/// Returns all values in the map.
pub fn[K, V] values(self : T[K, V]) -> Array[V] {
let values = Array::new(capacity=self.size)
self.each((_k, v) => values.push(v))
values
pub fn[K, V] values_as_iter(self : T[K, V]) -> Iter[V] {
Iter::new(fn(yield_) {
fn go(x : Node[K, V]?) {
match x {
None => IterContinue
Some({ left, right, value, .. }) =>
if go(left) is IterEnd {
IterEnd
} else if yield_(value) is IterEnd {
IterEnd
} else {
go(right)
}
}
}

go(self.root)
})
}

///|
Expand Down
4 changes: 2 additions & 2 deletions sorted_map/map_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,14 @@ test "eachi" {
///|
test "keys" {
let map = @sorted_map.from_array([(3, "c"), (2, "b"), (1, "a")])
inspect(map.keys(), content="[1, 2, 3]")
inspect(map.keys_as_iter(), content="[1, 2, 3]")
}

///|
test "values" {
let map = @sorted_map.from_array([(3, "c"), (2, "b"), (1, "a")])
inspect(
map.values(),
map.values_as_iter(),
content=(
#|["a", "b", "c"]
),
Expand Down
4 changes: 4 additions & 0 deletions sorted_map/sorted_map.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,19 @@ fn[K : Compare, V] T::get(Self[K, V], K) -> V?
fn[K, V] T::is_empty(Self[K, V]) -> Bool
fn[K, V] T::iter(Self[K, V]) -> Iter[(K, V)]
fn[K, V] T::iter2(Self[K, V]) -> Iter2[K, V]
#deprecated
fn[K, V] T::keys(Self[K, V]) -> Array[K]
fn[K, V] T::keys_as_iter(Self[K, V]) -> Iter[K]
#deprecated
fn[K : Compare, V] T::op_get(Self[K, V], K) -> V?
fn[K : Compare, V] T::op_set(Self[K, V], K, V) -> Unit
fn[K : Compare, V] T::range(Self[K, V], K, K) -> Iter2[K, V]
fn[K : Compare, V] T::remove(Self[K, V], K) -> Unit
fn[K, V] T::size(Self[K, V]) -> Int
fn[K, V] T::to_array(Self[K, V]) -> Array[(K, V)]
#deprecated
fn[K, V] T::values(Self[K, V]) -> Array[V]
fn[K, V] T::values_as_iter(Self[K, V]) -> Iter[V]
impl[K, V] Default for T[K, V]
impl[K : Eq, V : Eq] Eq for T[K, V]
impl[K : Show, V : Show] Show for T[K, V]
Expand Down
Loading