Skip to content

Commit

Permalink
Merge 98acf90 into 33a1e7d
Browse files Browse the repository at this point in the history
  • Loading branch information
jfecher authored Oct 21, 2024
2 parents 33a1e7d + 98acf90 commit ce26cd6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
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
15 changes: 12 additions & 3 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 mut ret = [first_elem; N];
let uninitialized = crate::mem::zeroed();
let mut ret = [uninitialized; N];

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), []);
}
}

0 comments on commit ce26cd6

Please sign in to comment.