update tests and Dict.keepShared semantics

This commit is contained in:
Brendan Hansknecht 2023-12-03 18:39:02 -08:00
parent a0854a10e7
commit 3f50f78eba
No known key found for this signature in database
GPG key ID: 0EA784685083E75B
3 changed files with 18 additions and 14 deletions

View file

@ -629,23 +629,26 @@ insertAll = \xs, ys ->
## Combine two dictionaries by keeping the [intersection](https://en.wikipedia.org/wiki/Intersection_(set_theory)) ## Combine two dictionaries by keeping the [intersection](https://en.wikipedia.org/wiki/Intersection_(set_theory))
## of all the key-value pairs. This means that we keep only those pairs ## of all the key-value pairs. This means that we keep only those pairs
## that are in both dictionaries. Note that where there are pairs with ## that are in both dictionaries. Both the key and value must match to be kept.
## the same key, the value contained in the first input will be retained,
## and the value in the second input will be removed.
## ``` ## ```
## first = ## first =
## Dict.single 1 "Keep Me" ## Dict.single 1 "Keep Me"
## |> Dict.insert 2 "And Me" ## |> Dict.insert 2 "And Me"
## |> Dict.insert 3 "Not this one"
## ##
## second = ## second =
## Dict.single 1 "Keep Me" ## Dict.single 1 "Keep Me"
## |> Dict.insert 2 "And Me" ## |> Dict.insert 2 "And Me"
## |> Dict.insert 3 "But Not Me" ## |> Dict.insert 3 "This has a different value"
## |> Dict.insert 4 "Or Me" ## |> Dict.insert 4 "Or Me"
## ##
## expect Dict.keepShared first second == first ## expected =
## Dict.single 1 "Keep Me"
## |> Dict.insert 2 "And Me"
##
## expect Dict.keepShared first second == expected
## ``` ## ```
keepShared : Dict k v, Dict k v -> Dict k v where k implements Hash & Eq keepShared : Dict k v, Dict k v -> Dict k v where k implements Hash & Eq, v implements Eq
keepShared = \xs, ys -> keepShared = \xs, ys ->
if len ys < len xs then if len ys < len xs then
keepShared ys xs keepShared ys xs
@ -654,10 +657,11 @@ keepShared = \xs, ys ->
xs xs
(withCapacity (len xs)) (withCapacity (len xs))
(\state, k, v -> (\state, k, v ->
if contains ys k then when get ys k is
insert state k v Ok yv if v == yv ->
else insert state k v
state _ ->
state
) )
## Remove the key-value pairs in the first input that are also in the second ## Remove the key-value pairs in the first input that are also in the second

View file

@ -436,7 +436,7 @@ fn keep_shared() {
#[test] #[test]
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))] #[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))]
fn keep_shared_prefer_first() { fn keep_shared_value_must_match() {
assert_evals_to!( assert_evals_to!(
indoc!( indoc!(
r#" r#"
@ -453,14 +453,14 @@ fn keep_shared_prefer_first() {
dict2 = dict2 =
Dict.empty {} Dict.empty {}
|> Dict.insert 0 100 |> Dict.insert 0 100
|> Dict.insert 2 200 |> Dict.insert 2 2
|> Dict.insert 4 300 |> Dict.insert 4 300
Dict.keepShared dict1 dict2 Dict.keepShared dict1 dict2
|> Dict.values |> Dict.values
"# "#
), ),
RocList::from_slice(&[2, 4]), RocList::from_slice(&[2]),
RocList<i64> RocList<i64>
); );
} }

View file

@ -120,7 +120,7 @@ fn union() {
|> Set.toList |> Set.toList
"# "#
), ),
RocList::from_slice(&[1, 2, 3, 4]), RocList::from_slice(&[1, 3, 4, 2]),
RocList<i64> RocList<i64>
); );
} }