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 get value from record with supplying default #1920

Merged
merged 1 commit into from
May 24, 2024

Conversation

olorin37
Copy link
Contributor

@olorin37 olorin37 commented May 17, 2024

As I see there is lacking the function in stdlib for getting values from the record with assumed default in case field is lacking. Or maybe there is another idiomatic method for that, which I miss?

The name of the function could be discussed...Personally for me get would be the best, but it is reserved already;-> do you have better ideas?

Here also documentation should be extended...

What do you, @yannham think about it?

@olorin37
Copy link
Contributor Author

Or maybe would be better to have dedicated syntax for that?

core/stdlib/std.ncl Outdated Show resolved Hide resolved
core/stdlib/std.ncl Outdated Show resolved Hide resolved
core/stdlib/std.ncl Outdated Show resolved Hide resolved
core/stdlib/std.ncl Outdated Show resolved Hide resolved
core/stdlib/std.ncl Outdated Show resolved Hide resolved
Copy link
Member

@yannham yannham left a comment

Choose a reason for hiding this comment

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

I originally wrote a comment but it's probably been lost in draft state, on the topic of having a dedicated syntax for that. I'm personally not a fan of making the syntax more complex just for a little convenience - especially in Nickel which we want to remain mostly readable to non-experts. Additionally, whether we add a new syntax or not at some point, it's still useful to have the function version, for example to pass to higher-order functions like array.map or record.map.

Beside the previous suggestions, looks good to me! The tests are currently failing because of my bogus suggestion, see the corresponding comment (it's r."%{field}", not field."%{r}"). We can merge once it's fixed.

@olorin37
Copy link
Contributor Author

🤦 actually I haven't time to just property analyse it... Just applied your suggestions...

@olorin37
Copy link
Contributor Author

Maybe also would be sensible to add std.array.at_or as it would be a mirror or std.record.get_or? @yannham?

@yannham
Copy link
Member

yannham commented May 21, 2024

Maybe also would be sensible to add std.array.at_or as it would be a mirror or std.record.get_or? @yannham?

Sure, it's probably not as usefulas get_or but it doesn't hurt. Given this PR is small, do you want to patch it directly @olorin37 ?

"default"
```
"%
= fun n default_value l =>
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
= fun n default_value l =>
= fun n default_value array =>

```
"%
= fun n default_value l =>
if n < %length% then
Copy link
Member

Choose a reason for hiding this comment

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

This primop is missing an argument.

Suggested change
if n < %length% then
if n < %length% array then

@olorin37
Copy link
Contributor Author

olorin37 commented May 21, 2024

Thanks for next review with obvious findings:) those %s misleads, and also I am used to jq.

According to names as you propose name "array" I'll do the same for "record".

@olorin37
Copy link
Contributor Author

OK, it seems that I should update those tests snapshots, as there are different line numbers in some cases... Am I right, @yannham ?

@yannham
Copy link
Member

yannham commented May 21, 2024

Ah, yes, we have a test which checks all the snippets in the manual, including their output, by actually running the code and capturing the result. Which means some error needs to be updated when they point to inside the stdlib and the line numbers in stdlib have changed. It's a bit annoying (but those tests are still very useful to make sure we keep the examples in the manual up to date). It seems this is what's failing in the CI.

You can trigger those tests with cargo test locally, if you want to iterate faster.

@olorin37
Copy link
Contributor Author

@yannham Hmm, I got some strange errors after adding at_or. Generally it is better to resolve such errors and understand it, I think in this case it is good opportunity to neglect it, as it is only a mirror of get_or and actually it is not as natural as get_or function.

@yannham
Copy link
Member

yannham commented May 24, 2024

I'm always fine with smaller changes 🙂 we can do at_or in a second row. Thanks for the addition!

@yannham yannham added this pull request to the merge queue May 24, 2024
Merged via the queue into tweag:master with commit 145fffd May 24, 2024
4 checks passed
@olorin37
Copy link
Contributor Author

Of course, only if you feel it valuable I can do next MR with at_or:-)

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.

2 participants