-
-
Notifications
You must be signed in to change notification settings - Fork 115
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
Conversation
There was a problem hiding this 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.
Co-authored-by: Oscar Spencer <[email protected]>
There was a problem hiding this 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.
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 |
* | ||
* @since v0.7.0 | ||
*/ | ||
provide let rec get = (key, list) => { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
This pr adds a
List.Assocative
submodule which contains functions for working with key-value pairs in lists.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 typeabstract type Collection<key, value> = List<(key, value)>
and then in that case we can handle the duplication semantics when you doList.toCollection
, I think this might be overkill though.