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

feat(stdlib): List.Associative Submodule #2202

Merged
merged 4 commits into from
Jan 3, 2025

Conversation

spotandjake
Copy link
Member

@spotandjake spotandjake commented Nov 18, 2024

This pr adds a List.Assocative submodule which contains functions for working with key-value pairs in lists.

let data = [
  ("name", "Alice"),
  ("name", "Bob"),
  ("age", "30"),
]
assert List.Associative.get("name", data) == Some("Alice")

Use case

This is similar to the associative list methods in ocaml https://ocaml.org/manual/5.2/api/List.html#1_Associationlists

I wasn't quite sure if we wanted this in the standard library or not but I think the pattern occurs enough with record information that it makes sense. A few examples being Map.toList, JSONObject.
Currently duplicates are ignored and the first item is always treated as the one your referring too.

Alternative proposal

An alternative proposal to this pr is we implement a new Collection module, where collections are of type abstract type Collection<key, value> = List<(key, value)> and then in that case we can handle the duplication semantics when you do List.toCollection, I think this might be overkill though.

Copy link
Member

@ospencer ospencer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is beautiful, but the spacing in your PR description is a crime.

stdlib/list.gr Outdated Show resolved Hide resolved
stdlib/list.gr Outdated Show resolved Hide resolved
stdlib/list.gr Outdated Show resolved Hide resolved
stdlib/list.gr Outdated Show resolved Hide resolved
Copy link
Member

@ospencer ospencer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love this but would like to see additional input from @alex-snezhko or @phated.

@alex-snezhko
Copy link
Member

Yeah I'm totally in favor of this, I've basically had to implement this functionality myself several times so I think it makes sense to include in the stdlib

@ospencer ospencer added this pull request to the merge queue Jan 3, 2025
Merged via the queue into grain-lang:main with commit fbb08bf Jan 3, 2025
12 checks passed
@github-actions github-actions bot mentioned this pull request Jan 2, 2025
*
* @since v0.7.0
*/
provide let rec get = (key, list) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For cases where there are duplicate keys in the array what do you think about a getAll or something which would return a list of all matches. Could also do something similar for set and remove

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we just had an all=false defaulted argument.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't really do that because the return type would change

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair enough, in that case I think we should have the getAll, removeAll and we might as well add setAll though I feel like that one isn't quite as useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants