From 90c5d182b578b6d512e4b5dc2c07810e6815f31e Mon Sep 17 00:00:00 2001 From: jfecher Date: Wed, 9 Aug 2023 13:36:27 -0500 Subject: [PATCH] feat: Add slice append (#2241) Add slice concat --- .../tests/execution_success/slices/src/main.nr | 5 +++++ noir_stdlib/src/slice.nr | 10 +++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/crates/nargo_cli/tests/execution_success/slices/src/main.nr b/crates/nargo_cli/tests/execution_success/slices/src/main.nr index cda6657b4ff..ca1c4ac2966 100644 --- a/crates/nargo_cli/tests/execution_success/slices/src/main.nr +++ b/crates/nargo_cli/tests/execution_success/slices/src/main.nr @@ -43,6 +43,11 @@ fn main(x : Field, y : pub Field) { assert(remove_slice[3] == 3); assert(remove_slice.len() == 4); + let append = [1, 2].append([3, 4, 5]); + assert(append.len() == 5); + assert(append[0] == 1); + assert(append[4] == 5); + regression_2083(); } diff --git a/noir_stdlib/src/slice.nr b/noir_stdlib/src/slice.nr index 8e344a40f5e..4f73f3a2994 100644 --- a/noir_stdlib/src/slice.nr +++ b/noir_stdlib/src/slice.nr @@ -32,5 +32,13 @@ impl [T] { /// the removed element #[builtin(slice_remove)] fn remove(_self: Self, _index: Field) -> (Self, T) { } -} + // Append each element of the `other` slice to the end of `self`. + // This returns a new slice and leaves both input slices unchanged. + fn append(mut self, other: Self) -> Self { + for elem in other { + self = self.push_back(elem); + } + self + } +}