Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Key filter val filter#4917

Merged
sorawee merged 15 commits into
racket:masterfrom
dr-neptune:key-filter-val-filter
Mar 4, 2024
Merged

Key filter val filter#4917
sorawee merged 15 commits into
racket:masterfrom
dr-neptune:key-filter-val-filter

Conversation

@dr-neptune
Copy link
Copy Markdown
Contributor

Checklist
  • Feature
  • tests included
  • documentation

Description of change

This change adds the following 2 functions to racket/hash:

  • hash-filter-keys: given a hash table and a predicate, return a hash table where the predicate is true for the input hash tables keys
  • hash-filter-values: the same as for keys, but applies the predicate to the input hash tables values

This is my first contribution to racket/hash, please let me know if I am missing anything or if there are broader guidelines for contributing that are applicable here

@dr-neptune dr-neptune marked this pull request as draft January 30, 2024 14:07
Comment thread pkgs/racket-doc/scribblings/reference/hashes.scrbl
Comment thread pkgs/racket-doc/scribblings/reference/hashes.scrbl Outdated
Comment thread racket/collects/racket/hash.rkt Outdated
Comment thread pkgs/racket-doc/scribblings/reference/hashes.scrbl Outdated
Comment thread pkgs/racket-doc/scribblings/reference/hashes.scrbl Outdated
Comment thread pkgs/racket-doc/scribblings/reference/hashes.scrbl Outdated
Comment thread racket/collects/racket/hash.rkt Outdated
@sorawee
Copy link
Copy Markdown
Collaborator

sorawee commented Feb 2, 2024

What do you think of just providing one function hash-filter (or hash-filter/copy to be consistent with hash-map/copy)? The provided procedure would consume both key and value. That way, you only need one function instead of two.

@dr-neptune
Copy link
Copy Markdown
Contributor Author

What do you think of just providing one function hash-filter (or hash-filter/copy to be consistent with hash-map/copy)? The provided procedure would consume both key and value. That way, you only need one function instead of two.

While I agree it makes sense to have a function that consumes the key and value (hash-filter, hash-filter/copy), I do think it feels cleaner on the user side to still provide key-specific and value-specific keywords, i.e. providing hash-filter, hash-filter-keys, hash-filter-values. Putting the onus to handle both key and value in the provided lambda has lead me to default to the less ideal (map fn (hash-keys ht)) pattern in the past.

That said, I am open to whatever makes the most sense given the community and API philosophy in Racket, as I am still very new to the idea of adding functionality here

@sorawee sorawee added the api design Related to the design of core libraries and language features label Feb 2, 2024
Comment thread pkgs/racket-test-core/tests/racket/hash.rktl Outdated
Comment thread pkgs/racket-test-core/tests/racket/hash.rktl Outdated
Comment thread racket/collects/racket/hash.rkt Outdated
@dr-neptune
Copy link
Copy Markdown
Contributor Author

What do you think of just providing one function hash-filter (or hash-filter/copy to be consistent with hash-map/copy)? The provided procedure would consume both key and value. That way, you only need one function instead of two.

While I agree it makes sense to have a function that consumes the key and value (hash-filter, hash-filter/copy), I do think it feels cleaner on the user side to still provide key-specific and value-specific keywords, i.e. providing hash-filter, hash-filter-keys, hash-filter-values. Putting the onus to handle both key and value in the provided lambda has lead me to default to the less ideal (map fn (hash-keys ht)) pattern in the past.

That said, I am open to whatever makes the most sense given the community and API philosophy in Racket, as I am still very new to the idea of adding functionality here

I've updated the PR to include hash-filter and rewritten hash-filter-values/keys to use hash-filter

@AlexKnauth
Copy link
Copy Markdown
Member

AlexKnauth commented Feb 6, 2024

It seems weird to me that hash-filter would work on mutable hashes at all. Where I would think there would be 2 separate operations: hash-filter for immutable ones, and hash-filter! for mutable ones.

@sorawee
Copy link
Copy Markdown
Collaborator

sorawee commented Feb 6, 2024

@AlexKnauth That's my first thought too, but there's hash-map/copy that works in a similar way, so I don't think it's too "weird".

@dr-neptune
Copy link
Copy Markdown
Contributor Author

@sorawee @AlexKnauth should I try splitting this up into hash-filter! for mutable hash tables and hash-filter for immutable hash tables, or is this fine as is? Alternatively, should I rename the current hash-filter to hash-filter/copy since it makes a copy, and then build out the previously mentioned hash-filter, hash-filter!?

@dr-neptune dr-neptune marked this pull request as ready for review February 14, 2024 13:54
@AlexKnauth
Copy link
Copy Markdown
Member

hash-filter should not mutate the hash table it's given.

Comment thread racket/collects/racket/hash.rkt Outdated
Comment thread racket/collects/racket/hash.rkt Outdated
Comment thread racket/collects/racket/hash.rkt Outdated
Comment thread racket/collects/racket/hash.rkt Outdated
Comment thread pkgs/racket-doc/scribblings/reference/hashes.scrbl Outdated
Comment thread pkgs/racket-doc/scribblings/reference/hashes.scrbl Outdated
Comment thread racket/collects/racket/hash.rkt Outdated
@dr-neptune dr-neptune closed this Feb 18, 2024
@dr-neptune dr-neptune reopened this Feb 18, 2024
@dr-neptune
Copy link
Copy Markdown
Contributor Author

What is needed to move this forward?

@sorawee
Copy link
Copy Markdown
Collaborator

sorawee commented Feb 18, 2024

I cannot merge it due to the merge conflicts. Can you pull the latest master, rebase your changes on top of it, squash them to one commit, and then force push?

Comment thread pkgs/racket-doc/scribblings/reference/hashes.scrbl
@sorawee sorawee force-pushed the key-filter-val-filter branch from 2d6faa3 to d30f2c0 Compare February 19, 2024 19:00
Comment thread pkgs/racket-doc/scribblings/reference/hashes.scrbl
@sorawee sorawee force-pushed the key-filter-val-filter branch from d30f2c0 to f22c002 Compare February 19, 2024 19:11
@dr-neptune dr-neptune requested a review from sorawee February 24, 2024 19:02
@dr-neptune
Copy link
Copy Markdown
Contributor Author

Hi @sorawee @AlexKnauth , is there anything that still needs to be done here?

@sorawee sorawee merged commit 8e71a40 into racket:master Mar 4, 2024
@sorawee
Copy link
Copy Markdown
Collaborator

sorawee commented Mar 4, 2024

Sorry, looks good to me. Merged.

@dr-neptune
Copy link
Copy Markdown
Contributor Author

Thanks so much!

@samth
Copy link
Copy Markdown
Member

samth commented Apr 3, 2024

This PR broke the following package: https://plt.cs.northwestern.edu/pkg-build/server/built/fail/odysseus.txt

@mflatt
Copy link
Copy Markdown
Member

mflatt commented Apr 9, 2024

@sorawee @dr-neptune — ping

There's always the possibility of backward incompatibility when an export is added to a module. Our general rule is to add an export only when it does not create a conflict with any package registered at pkgs.racket-lang.org. (This is a compromise between a completely consistent approach to backward compatibility and practical evolution of APIs, since code not registered as a package can still break.) Usually, when there's a conflict, we just don't make the addition to the original library — which may mean noyt having the binding at all, or it may mean an extra library or submodule. Sometimes, the remedy is to get a helpful package author to change code. In any case, this needs to be resolved, since we're trying to get the v8.13 release out.

@dr-neptune
Copy link
Copy Markdown
Contributor Author

dr-neptune commented Apr 9, 2024

My apologies, I didn't see the previous ping by @samth . I'm not super familiar with this process. My first guess based on the error message is that the Odysseus package has some version of hash-filter. Would it make sense to try to supersede it with hash-filter in base racket/hash? Odysseus looks to be a general purpose helper library:

https://pkgs.racket-lang.org/package/odysseus

My thought is that filtering a hash makes sense in a std lib as opposed to a loaded external lib.

What do you think @mflatt @sorawee @prozion ?

raco setup: /home/root/user/.local/share/racket/snapshot/pkgs/odysseus/odysseus/optimize.rkt:7:9: module: identifier already required
raco setup: at: hash-filter
raco setup: in: "hash.rkt"
raco setup: also provided by: racket/hash

Also, given the time component I understand if it would be better to roll back this change and chat about it further to prioritize getting 8.13 out

@samth
Copy link
Copy Markdown
Member

samth commented Apr 9, 2024

The best solution is for prozion/odysseus#2 to get merged (thanks @AlexKnauth). If @prozion doesn't respond about that then maybe we need to change the name or revert the addition.

@sorawee
Copy link
Copy Markdown
Collaborator

sorawee commented Apr 14, 2024

By April 16, if the odysseus PR is not merged, I think we should just revert this PR.

@sorawee
Copy link
Copy Markdown
Collaborator

sorawee commented Apr 16, 2024

Let's revert the PR for now.

sorawee added a commit to sorawee/racket that referenced this pull request Apr 16, 2024
@samth samth mentioned this pull request Apr 19, 2024
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api design Related to the design of core libraries and language features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants