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: add array range and reverse functions #7159

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

Conversation

DeVikingMark
Copy link

Description:

This PR adds two utility functions to the array module:

  • range(size: Field) - creates an array of numbers from 0 to size-1
  • reverse() - returns a new array with elements in reverse order

Resolves TODO comments from test_programs/noir_test_success/brillig_oracle/src/main.nr

Both functions are fully tested and documented with examples.

Copy link
Contributor

Thank you for your contribution to the Noir language.

Please do not force push to this branch after the Noir team have started review of this PR. Doing so will only delay us merging your PR as we will need to start the review process from scratch.

Thanks for your understanding.

/// assert(arr == [0, 1, 2, 3, 4]);
/// }
/// ```
pub fn range<T>(size: Field) -> [Field] {
Copy link
Contributor

Choose a reason for hiding this comment

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

The T generic parameter is not used, and technically this returns a slice, rather than an array.
size is usually expressed as u32.
Returning Field seems a bit specific; the library so far is completely generic.

Perhaps something like std::array::from_fn would be a more flexible alternative to make an actual array of a given size, with content created for each index given to a lambda as u32 between 0 and N. You could use it to cast to Field, or map it to a range other than 0..N, e.g. by an offset.

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