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: concat3 #7200

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"combinators",
"compinit",
"comptime",
"concat",
"cpus",
"cranelift",
"critesjosh",
Expand Down
35 changes: 35 additions & 0 deletions docs/docs/noir/concepts/data_types/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,41 @@ fn main() {
}
```

### concat

Concatenates this array with another array.

```rust
fn concat<let M: u32>(self, array2: [T; M]) -> [T; N + M]
```

```rust
fn main() {
let arr1 = [1, 2, 3, 4];
let arr2 = [6, 7, 8, 9, 10, 11];
let concatenated_arr = arr1.concat(arr2);
assert(concatenated_arr == [1, 2, 3, 4, 6, 7, 8, 9, 10, 11]);
}
```

### concat3

Concatenates this array with another array.

```rust
fn concat3<let M: u32, let L: u32>(self, array2: [T; M], array3: [T; L]) -> [T; N + M + L]
```

```rust
fn main() {
let arr1 = [1, 2, 3];
let arr2 = [4, 5, 6];
let arr3 = [7, 8, 9];
let concatenated_arr = arr1.concat3(arr2, arr3);
assert(concatenated_arr == [1, 2, 3, 4, 5, 6, 7, 8, 9]);
}
```

### as_str_unchecked

Converts a byte array of type `[u8; N]` to a string. Note that this performs no UTF-8 validation -
Expand Down
72 changes: 72 additions & 0 deletions noir_stdlib/src/array/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,61 @@ impl<T, let N: u32> [T; N] {
}
ret
}

/// Concatenates this array with another array.
///
/// Example:
///
/// ```noir
/// fn main() {
/// let arr1 = [1, 2, 3, 4];
/// let arr2 = [6, 7, 8, 9, 10, 11];
/// let concatenated_arr = arr1.concat(arr2);
/// assert(concatenated_arr == [1, 2, 3, 4, 6, 7, 8, 9, 10, 11]);
/// }
/// ```
pub fn concat<let M: u32>(self, array2: [T; M]) -> [T; N + M] {
let mut result = [self[0]; N + M];
for i in 1..N {
result[i] = self[i];
}
for i in 0..M {
result[i + N] = array2[i];
}
result
}

/// Concatenates this array with two other arrays.
///
/// For unconstrained functions, this is more efficient than calling
/// `arr1.concat(arr2.concat(arr3))`.
///
/// Example:
///
/// ```noir
/// fn main() {
/// let arr1 = [1, 2, 3];
/// let arr2 = [4, 5, 6];
/// let arr3 = [7, 8, 9];
/// let concatenated_arr = arr1.concat3(arr2, arr3);
/// assert(concatenated_arr == [1, 2, 3, 4, 5, 6, 7, 8, 9]);
/// }
/// ```
pub fn concat3<let M: u32, let L: u32>(self, array2: [T; M], array3: [T; L]) -> [T; N + M + L] {
let mut result = [self[0]; N + M + L];
for i in 1..N {
result[i] = self[i];
}
let mut offset = N;
for i in 0..M {
result[offset + i] = array2[i];
}
offset += M;
for i in 0..L {
result[offset + i] = array3[i];
}
result
}
}

impl<T, let N: u32> [T; N]
Expand Down Expand Up @@ -232,4 +287,21 @@ mod test {
fn map_empty() {
assert_eq([].map(|x| x + 1), []);
}

#[test]
fn concat() {
let arr1 = [1, 2, 3, 4];
let arr2 = [6, 7, 8, 9, 10, 11];
let concatenated_arr = arr1.concat(arr2);
assert(concatenated_arr == [1, 2, 3, 4, 6, 7, 8, 9, 10, 11]);
}

#[test]
fn concat3() {
let arr1 = [1, 2, 3];
let arr2 = [4, 5, 6];
let arr3 = [7, 8, 9];
let concatenated_arr = arr1.concat3(arr2, arr3);
assert(concatenated_arr == [1, 2, 3, 4, 5, 6, 7, 8, 9]);
}
}
Loading