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

Add functions to the Map module and reformat #133

Merged
merged 5 commits into from
Nov 1, 2024
Merged

Conversation

lukaszcz
Copy link
Contributor

  • Closes Add merge function for Map #132
  • Adds the intersection/difference/union functions to the Map module.
  • Reformats the standard library for the recent changes in the Juvix formatter.

@lukaszcz lukaszcz self-assigned this Oct 30, 2024
@lukaszcz lukaszcz requested review from janmasrovira and paulcadman and removed request for janmasrovira October 30, 2024 16:12
@paulcadman
Copy link
Collaborator

Perhaps @heueristik could review the new API to see if meets what is needed for applications?

--- 𝒪(n log n). Intersection of two maps. Returns data in the first map for the
--- keys existing in both maps.
{-# inline: true #-}
intersection
{Key Value} {{Ord Key}} (map1 map2 : Map Key Value) : Map Key Value :=
case map1, map2 of mkMap s1, mkMap s2 := mkMap (Set.intersection s1 s2);
--- 𝒪(n log n). Return elements of `map1` with keys not existing in `map2`.
{-# inline: true #-}
difference
{Key Value} {{Ord Key}} (map1 map2 : Map Key Value) : Map Key Value :=
case map1, map2 of mkMap s1, mkMap s2 := mkMap (Set.difference s1 s2);
--- 𝒪(n log n). Returns a ;Map; containing the elements that are in `map1` or
--- `map2`. This is a left-biased union of `map1` and `map2` which prefers
--- `map1` when duplicate keys are encountered.
{-# inline: true #-}
unionLeft {Key Value} {{Ord Key}} (map1 map2 : Map Key Value) : Map Key Value :=
case map1, map2 of mkMap s1, mkMap s2 := mkMap (Set.unionLeft s1 s2);
--- 𝒪(n log n). Returns a ;Map; containing the elements that are in `map1` or `map2`.
{-# inline: true #-}
union {Key Value} {{Ord Key}} (map1 map2 : Map Key Value) : Map Key Value :=
case map1, map2 of mkMap s1, mkMap s2 := mkMap (Set.union s1 s2);
{-# specialize: [1, 2] #-}
unions
{Key Value Container}
{{Ord Key}}
{{Foldable Container (Map Key Value)}}
(maps : Container)
: Map Key Value :=
for (acc := empty) (map in maps) {
union acc map
};
--- O(n log n). If `map1` and `map2` are disjoint (have no common keys), then
--- returns `ok map` where `map` is the union of `map1` and `map2`. If `map1`
--- and `map2` are not disjoint, then returns `error k` where `k` is a common
--- key.
{-# inline: true #-}
disjointUnion
{Key Value}
{{Ord Key}}
(map1 map2 : Map Key Value)
: Result Key (Map Key Value) :=
case map1, map2 of
mkMap s1, mkMap s2 :=
case Set.disjointUnion s1 s2 of
| error x := error (key x)
| ok s := ok (mkMap s);

@paulcadman paulcadman requested a review from heueristik October 31, 2024 13:48
@paulcadman
Copy link
Collaborator

I'll merge this now, @heueristik can let us know if something else is required.

@paulcadman paulcadman merged commit deaafbb into main Nov 1, 2024
1 check passed
@paulcadman paulcadman deleted the improve-map branch November 1, 2024 10:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add merge function for Map
2 participants