Skip to content

feat(stdlib): add fold function #1192

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jakedipity
Copy link

@jakedipity jakedipity commented Dec 17, 2024

Summary

Adds a new fold function to reduce objects and arrays into a single value based on a closure and initial value.

Change Type

  • Bug fix
  • New feature
  • Non-functional (chore, refactoring, docs)
  • Performance

Is this a breaking change?

  • Yes
  • No

How did you test this PR?

Test examples in the fold function definition, existing function testing macros don't support functions with closures.

Does this PR include user facing changes?

  • Yes. Please add a changelog fragment based on
    our guidelines.
  • No. A maintainer will apply the "no-changelog" label to this PR.

Checklist

  • Our CONTRIBUTING.md is a good starting place.
  • If this PR introduces changes to LICENSE-3rdparty.csv, please
    run dd-rust-license-tool write and commit the changes. More details here.
  • For new VRL functions, please also create a sibling PR in Vector to document the new function.

References

closes: #1139

@pront pront self-assigned this Jan 2, 2025
@pront
Copy link
Member

pront commented Jan 2, 2025

Hi @jakedipity, thank you for this contribution!

I ran the CI checks and there are some failing tests. For fast local iterations you can use:

 ./scripts/checks.sh clippy tests vrl_tests

@pront
Copy link
Member

pront commented Jan 3, 2025

Also, please add a changelog. Feel free to ping me here when it's ready for another review.

@jakedipity jakedipity force-pushed the fold branch 2 times, most recently from fc6123e to 748918e Compare February 14, 2025 20:22
@jakedipity
Copy link
Author

@pront I've updated the PR to pass all tests and added a changelog fragment!

@pront
Copy link
Member

pront commented Feb 14, 2025

@pront I've updated the PR to pass all tests and added a changelog fragment!

Thank you @jakedipity, I will take a look on Monday 👍
Also, please rebase on latest origin/main.

Adds a new fold function to reduce objects and arrays into a single
value based on a closure and initial value.

Signed-off-by: Jacob Hull <[email protected]>
@jakedipity
Copy link
Author

Thanks @pront, I've rebased to latest on main.

Copy link
Member

@pront pront 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 contains a lot of changes to the closure internals, it would great to have more VRL tests like this one: https://github.com/vectordotdev/vrl/blob/main/lib/tests/tests/expressions/function_call/closure_scope_inheritance.vrl

Note, we are not limited to testing macros, we can write tests like so: https://github.com/vectordotdev/vrl/blob/main/src/stdlib/shannon_entropy.rs#L288

@@ -309,17 +309,41 @@ impl<'a> Builder<'a> {
//
// We set "bar" (index 0) to return bytes, and "baz" (index 1) to return an
// integer.
//
// If one of the arguments is dependant on a closure, the parameters initial
Copy link
Member

Choose a reason for hiding this comment

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

Typo

Suggested change
// If one of the arguments is dependant on a closure, the parameters initial
// If one of the arguments is dependent on a closure, the parameters initial

// A closure variable kind is not possible here but we need to
// satisfy all variants with a match arm.
VariableKind::Closure(_) => {
panic!("got unexpected variable kind")
Copy link
Member

Choose a reason for hiding this comment

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

Return Err instead.

@pront pront added the meta: awaiting author Pull requests that are awaiting their author. label Mar 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
meta: awaiting author Pull requests that are awaiting their author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fold enumeration function
2 participants