From cac6146c38d557e7b0c208fd6b8fab74a5e9d21a Mon Sep 17 00:00:00 2001 From: iAmMichaelConnor Date: Mon, 27 Jan 2025 20:07:24 +0000 Subject: [PATCH 1/6] feat: array concat method --- noir_stdlib/src/array/mod.nr | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/noir_stdlib/src/array/mod.nr b/noir_stdlib/src/array/mod.nr index 85cc0580aae..025d4924c47 100644 --- a/noir_stdlib/src/array/mod.nr +++ b/noir_stdlib/src/array/mod.nr @@ -136,6 +136,29 @@ impl [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(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 + } } impl [T; N] From e7ebdeefb8be13869f4745ab1bac01e8652a8964 Mon Sep 17 00:00:00 2001 From: iAmMichaelConnor Date: Mon, 27 Jan 2025 20:20:16 +0000 Subject: [PATCH 2/6] docs --- docs/docs/noir/concepts/data_types/arrays.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/docs/docs/noir/concepts/data_types/arrays.md b/docs/docs/noir/concepts/data_types/arrays.md index 289145a8c4d..f831a5f57b8 100644 --- a/docs/docs/noir/concepts/data_types/arrays.md +++ b/docs/docs/noir/concepts/data_types/arrays.md @@ -255,6 +255,23 @@ fn main() { } ``` +### concat + +Concatenates this array with another array. + +```rust +fn concat(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]); +} +``` + ### as_str_unchecked Converts a byte array of type `[u8; N]` to a string. Note that this performs no UTF-8 validation - From b0222aed26cb2f94600925e241798e5163a62954 Mon Sep 17 00:00:00 2001 From: iAmMichaelConnor Date: Mon, 27 Jan 2025 20:24:31 +0000 Subject: [PATCH 3/6] add 'concat' to cspell --- cspell.json | 1 + 1 file changed, 1 insertion(+) diff --git a/cspell.json b/cspell.json index 1174a56dd33..c1cc19f8962 100644 --- a/cspell.json +++ b/cspell.json @@ -60,6 +60,7 @@ "combinators", "compinit", "comptime", + "concat", "cpus", "cranelift", "critesjosh", From 24b92f40764f7e0ce4cfb893f6f7bf759b866b83 Mon Sep 17 00:00:00 2001 From: iAmMichaelConnor Date: Mon, 27 Jan 2025 20:35:53 +0000 Subject: [PATCH 4/6] test: add a test --- noir_stdlib/src/array/mod.nr | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/noir_stdlib/src/array/mod.nr b/noir_stdlib/src/array/mod.nr index 025d4924c47..abb7b0b2172 100644 --- a/noir_stdlib/src/array/mod.nr +++ b/noir_stdlib/src/array/mod.nr @@ -255,4 +255,12 @@ 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]); + } } From d161a0dc6271db1f0b955cc86b961d6c3cb07e8a Mon Sep 17 00:00:00 2001 From: iAmMichaelConnor Date: Mon, 27 Jan 2025 20:40:46 +0000 Subject: [PATCH 5/6] fmt --- noir_stdlib/src/array/mod.nr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/noir_stdlib/src/array/mod.nr b/noir_stdlib/src/array/mod.nr index abb7b0b2172..51aec287508 100644 --- a/noir_stdlib/src/array/mod.nr +++ b/noir_stdlib/src/array/mod.nr @@ -262,5 +262,5 @@ mod test { 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]); - } + } } From 73585c36666dcc77dab76389f32064745bae8c33 Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Tue, 28 Jan 2025 11:59:50 +0000 Subject: [PATCH 6/6] Update noir_stdlib/src/array/mod.nr --- noir_stdlib/src/array/mod.nr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/noir_stdlib/src/array/mod.nr b/noir_stdlib/src/array/mod.nr index 51aec287508..7a74410a673 100644 --- a/noir_stdlib/src/array/mod.nr +++ b/noir_stdlib/src/array/mod.nr @@ -261,6 +261,6 @@ mod test { 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]); + assert_eq(concatenated_arr, [1, 2, 3, 4, 6, 7, 8, 9, 10, 11]); } }