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

remove list conversions from map #403

Merged
merged 1 commit into from
May 16, 2024
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
34 changes: 1 addition & 33 deletions map/map.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -109,47 +109,15 @@ pub fn filter_with_key[K : Compare, V](
}
}

/// Convert to a list of key-value pairs.
pub fn to_list[K, V](self : Map[K, V]) -> List[(K, V)] {
self.to_asc_list()
}

/// Convert to an ascending list.
pub fn to_asc_list[K, V](self : Map[K, V]) -> List[(K, V)] {
self.foldr_with_key(fn(xs, k, v) { Cons((k, v), xs) }, init=List::[])
}

/// Convert to an descending list.
pub fn to_desc_list[K, V](self : Map[K, V]) -> List[(K, V)] {
self.foldl_with_key(fn(xs, k, v) { Cons((k, v), xs) }, init=List::[])
}

/// Convert to a vector of key-value pairs.
pub fn to_vec[K, V](self : Map[K, V]) -> @vec.Vec[(K, V)] {
let vec = @vec.new()
self.iter(fn(k, v) { vec.push((k, v)) })
vec
}

/// Return all keys of the map in ascending order.
pub fn keys[K, V](self : Map[K, V]) -> List[K] {
self.foldr_with_key(fn(xs, k, _v) { Cons(k, xs) }, init=List::[])
}

/// Return an immutable set of all keys of the map.
pub fn keys_set[K : Compare + Eq, V](
self : Map[K, V]
) -> @immutable_set.ImmutableSet[K] {
@immutable_set.from_list(self.keys())
}

/// Return all elements of the map in the ascending order of their keys.
pub fn elems[K, V](self : Map[K, V]) -> List[V] {
self.foldr_with_key(fn(xs, _k, v) { Cons(v, xs) }, init=List::[])
}

pub fn op_equal[K : Eq, V : Eq](self : Map[K, V], other : Map[K, V]) -> Bool {
self.to_list() == other.to_list()
self.to_vec() == other.to_vec()
}

/// The ratio between the sizes of the left and right subtrees.
Expand Down
7 changes: 0 additions & 7 deletions map/map.mbti
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package moonbitlang/core/map

alias @moonbitlang/core/immutable_set as @immutable_set
alias @moonbitlang/core/vec as @vec

// Values
Expand All @@ -12,7 +11,6 @@ fn singleton[K, V](K, V) -> Map[K, V]
type Map
impl Map {
debug_write[K : Debug, V : Debug](Self[K, V], Buffer) -> Unit
elems[K, V](Self[K, V]) -> List[V]
filter[K : Compare + Eq, V](Self[K, V], (V) -> Bool) -> Self[K, V]
filter_with_key[K : Compare + Eq, V](Self[K, V], (K, V) -> Bool) -> Self[K, V]
fold[K, V, T](Self[K, V], (T, V) -> T, ~init : T) -> T
Expand All @@ -23,18 +21,13 @@ impl Map {
is_empty[K, V](Self[K, V]) -> Bool
iter[K, V](Self[K, V], (K, V) -> Unit) -> Unit
iteri[K, V](Self[K, V], (Int, K, V) -> Unit) -> Unit
keys[K, V](Self[K, V]) -> List[K]
keys_set[K : Compare + Eq, V](Self[K, V]) -> @immutable_set.ImmutableSet[K]
lookup[K : Compare + Eq, V](Self[K, V], K) -> Option[V]
map[K, X, Y](Self[K, X], (X) -> Y) -> Self[K, Y]
map_with_key[K, X, Y](Self[K, X], (K, X) -> Y) -> Self[K, Y]
member[K : Compare + Eq, V](Self[K, V], K) -> Bool
op_equal[K : Eq, V : Eq](Self[K, V], Self[K, V]) -> Bool
remove[K : Compare + Eq, V](Self[K, V], K) -> Self[K, V]
size[K, V](Self[K, V]) -> Int
to_asc_list[K, V](Self[K, V]) -> List[Tuple[K, V]]
to_desc_list[K, V](Self[K, V]) -> List[Tuple[K, V]]
to_list[K, V](Self[K, V]) -> List[Tuple[K, V]]
to_vec[K, V](Self[K, V]) -> @vec.Vec[Tuple[K, V]]
}

Expand Down
34 changes: 33 additions & 1 deletion map/map_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,38 @@
// See the License for the specific language governing permissions and
// limitations under the License.

/// Return all elements of the map in the ascending order of their keys.
pub fn elems[K, V](self : Map[K, V]) -> List[V] {
self.foldr_with_key(fn(xs, _k, v) { Cons(v, xs) }, init=List::[])
}

/// Convert to a list of key-value pairs.
pub fn to_list[K, V](self : Map[K, V]) -> List[(K, V)] {
self.to_asc_list()
}

/// Convert to an ascending list.
pub fn to_asc_list[K, V](self : Map[K, V]) -> List[(K, V)] {
self.foldr_with_key(fn(xs, k, v) { Cons((k, v), xs) }, init=List::[])
}

/// Convert to an descending list.
pub fn to_desc_list[K, V](self : Map[K, V]) -> List[(K, V)] {
self.foldl_with_key(fn(xs, k, v) { Cons((k, v), xs) }, init=List::[])
}

/// Return all keys of the map in ascending order.
pub fn keys_list[K, V](self : Map[K, V]) -> List[K] {
self.foldr_with_key(fn(xs, k, _v) { Cons(k, xs) }, init=List::[])
}

/// Return an immutable set of all keys of the map.
fn keys_set[K : Compare + Eq, V](
self : Map[K, V]
) -> @immutable_set.ImmutableSet[K] {
@immutable_set.from_list(self.keys_list())
}

test "from_array" {
let m = Map::[(3, "three"), (8, "eight"), (1, "one"), (2, "two"), (0, "zero")]
inspect(
Expand Down Expand Up @@ -173,7 +205,7 @@ test "to_vec" {

test "keys" {
let m = Map::[(1, "one"), (2, "two"), (3, "three")]
@assertion.assert_eq(m.keys(), List::[1, 2, 3])?
@assertion.assert_eq(m.keys_list(), List::[1, 2, 3])?
}

test "keys_set" {
Expand Down
7 changes: 2 additions & 5 deletions map/moon.pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
"moonbitlang/core/builtin",
"moonbitlang/core/assertion",
"moonbitlang/core/tuple",
"moonbitlang/core/list",

"moonbitlang/core/string",
"moonbitlang/core/vec",
"moonbitlang/core/immutable_set",
"moonbitlang/core/coverage"
],
"test_import" : [

]
"test_import": ["moonbitlang/core/immutable_set","moonbitlang/core/list"]
}