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

fix: Allow array map on empty arrays #6305

Merged
merged 3 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/docs/noir/concepts/data_types/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ fn main() {

Same as fold, but uses the first element as the starting element.

Requires `self` to be non-empty.

```rust
fn reduce(self, f: fn(T, T) -> T) -> T
```
Expand Down
13 changes: 11 additions & 2 deletions noir_stdlib/src/array/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ impl<T, let N: u32> [T; N] {
/// assert_eq(b, [2, 4, 6]);
/// ```
pub fn map<U, Env>(self, f: fn[Env](T) -> U) -> [U; N] {
let first_elem = f(self[0]);
let first_elem = crate::mem::zeroed();
let mut ret = [first_elem; N];
jfecher marked this conversation as resolved.
Show resolved Hide resolved

for i in 1..self.len() {
for i in 0..self.len() {
ret[i] = f(self[i]);
}

Expand Down Expand Up @@ -79,6 +79,8 @@ impl<T, let N: u32> [T; N] {
}

/// Same as fold, but uses the first element as the starting element.
///
/// Requires the input array to be non-empty.
///
/// Example:
///
Expand Down Expand Up @@ -217,3 +219,10 @@ impl<let N: u32> From<str<N>> for [u8; N] {
s.as_bytes()
}
}

mod test {
#[test]
fn map_empty() {
assert_eq([].map(|x| x + 1), []);
}
}
Loading