Skip to content

Commit b14746d

Browse files
committed
add Set::to_json
1 parent 7d69952 commit b14746d

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

builtin/builtin.mbti

+1
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ impl Set {
330330
size[K](Self[K]) -> Int
331331
symmetric_difference[K : Hash + Eq](Self[K], Self[K]) -> Self[K]
332332
to_array[K](Self[K]) -> Array[K]
333+
to_json[X : ToJson](Self[X]) -> Json
333334
to_string[K : Show](Self[K]) -> String
334335
union[K : Hash + Eq](Self[K], Self[K]) -> Self[K]
335336
}

builtin/json.mbt

+8
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ pub fn Map::to_json[K : Show, V : ToJson](self : Map[K, V]) -> Json {
134134
Object(object)
135135
}
136136

137+
pub fn Set::to_json[X : ToJson](self : Set[X]) -> Json {
138+
let array = []
139+
for v in self {
140+
array.push(v.to_json())
141+
}
142+
Array(array)
143+
}
144+
137145
pub fn Option::to_json[T : ToJson](self : T?) -> Json {
138146
match self {
139147
None => Null

builtin/linked_hash_set_wbtest.mbt renamed to builtin/linked_hash_set_test.mbt

+9
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,15 @@ test "insert_and_grow" {
146146
}
147147
assert_eq!(m.size(), 10)
148148
assert_eq!(m.capacity(), 16)
149+
@json.inspect!(m, content=["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"])
150+
}
151+
152+
test "array unique via Set" {
153+
let v = [1, 2, 3, 4, 5, 3, 2, 4, 5]
154+
let h = Set::from_iter(v.iter())
155+
let v = [..h]
156+
// @json.inspect!([..h])
157+
@json.inspect!(v, content=[1, 2, 3, 4, 5])
149158
}
150159

151160
test "remove_and_shift_back" {

0 commit comments

Comments
 (0)