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

Adds a mapFindLower and mapFindUpper to map.mc #828

Merged
merged 6 commits into from
Mar 26, 2024

Conversation

br4sco
Copy link
Contributor

@br4sco br4sco commented Mar 21, 2024

This PR:

  • Adds two new functions to map.mc, mapFindLower, and mapFindUpper that find the nearest upper/lower key and associated value w.r.t. to a given key.
  • Adds a new function, mapSingleton, which creates a singleton map.
  • Re-organizes map.mc to clarify the map interface.

@johnwikman
Copy link
Contributor

LGTM, but this PR got me thinking, why are we limiting ourselves to k -> k -> Int comparisons for maps? Why not have something like k1 -> k2 -> Int if we want to compare different types? Feels like this feature would be great if you are looking for something in an integer keyed map and want to find the number closest to some float.

@br4sco
Copy link
Contributor Author

br4sco commented Mar 21, 2024

why are we limiting ourselves to k -> k -> Int comparisons for maps?

That would be nice. It is not immediately clear to me how you can get that expressiveness to work with AVL trees but maybe I'm not thinking hard enough.

@johnwikman
Copy link
Contributor

why are we limiting ourselves to k -> k -> Int comparisons for maps?

That would be nice. It is not immediately clear to me how you can get that expressiveness to work with AVL trees but maybe I'm not thinking hard enough.

I was thinking that you could have something like

let mapFindLowerOn : all k. all v. (k -> Int) -> Map k v -> Option (k, v) =
  ...

mexpr

let m : Map Int String = ... in

let x = 3.5 in

switch mapFindLowerOn (lam k. cmpf x (int2float k)) m
case Some (k, v) then ...
case _           then ...
end

I think this kind of custom comparison really only makes sense with this "find the best option" kind of operation when there might not be a direct conversion from lookup values to key values. For all other map operations you expect perfect matches, so a direct conversion should be possible.

@david-broman david-broman merged commit fcd993f into miking-lang:develop Mar 26, 2024
2 checks passed
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.

3 participants