Skip to content

Commit

Permalink
feat: array extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
olehmisar committed Oct 23, 2024
1 parent 9f36681 commit f51557a
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 25 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ name: Noir tests

on:
push:
branches:
- main
branches:
- main
pull_request:

env:
Expand All @@ -16,7 +16,7 @@ jobs:
strategy:
fail-fast: false
matrix:
toolchain: [0.34.0]
toolchain: [0.35.0]
steps:
- name: Checkout sources
uses: actions/checkout@v4
Expand All @@ -38,7 +38,7 @@ jobs:
- name: Install Nargo
uses: noir-lang/noirup@v0.1.3
with:
toolchain: 0.34.0
toolchain: 0.35.0

- name: Run formatter
run: nargo fmt --check
28 changes: 25 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,34 @@ use nodash::str_to_u64;
assert(str_to_u64("02345678912345678912") == 02345678912345678912);
```

### `array_concat`
### `ArrayExtensions`

#### `concat`

Concatenates two arrays.

```rs
use nodash::array_concat;
use nodash::ArrayExtensions;

assert([1, 2, 3].concat([4, 5]) == [1, 2, 3, 4, 5]);
```

#### `pad_start`

Pads the start of the array with a value.

```rs
use nodash::ArrayExtensions;

assert([1, 2, 3].pad_start::<5>(0) == [0, 0, 1, 2, 3]);
```

#### `pad_end`

Pads the end of the array with a value.

```rs
use nodash::ArrayExtensions;

assert(array_concat([1, 2, 3], [4, 5]) == [1, 2, 3, 4, 5]);
assert([1, 2, 3].pad_end::<5>(0) == [1, 2, 3, 0, 0]);
```
47 changes: 47 additions & 0 deletions src/array.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
impl<T, let N: u32> crate::ArrayExtensions<T, N> for [T; N] {
fn concat<let M: u32>(self, other: [T; M]) -> [T; N + M] {
let mut result = [self[0]; N + M];
for i in 0..N {
result[i] = self[i];
}
for i in 0..M {
result[i + N] = other[i];
}
result
}

fn pad_start<let M: u32>(self, pad_value: T) -> [T; M] {
assert(M >= N, "pad_start: array too long");
let mut res = [pad_value; M];
for i in 0..N {
res[i + M - N] = self[i];
}
res
}

fn pad_end<let M: u32>(self, pad_value: T) -> [T; M] {
assert(M >= N, "pad_end: array too long");
let mut res = [pad_value; M];
for i in 0..N {
res[i] = self[i];
}
res
}
}

mod tests {
#[test]
fn test_simple() {
assert([1, 2, 3].concat([4, 5]) == [1, 2, 3, 4, 5]);
}

#[test]
fn test_pad_start() {
assert([1, 2, 3].pad_start::<5>(0) == [0, 0, 1, 2, 3]);
}

#[test]
fn test_pad_end() {
assert([1, 2, 3].pad_end::<5>(0) == [1, 2, 3, 0, 0]);
}
}
24 changes: 8 additions & 16 deletions src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,18 @@ mod math;
mod solidity;
mod string;
mod tables;
mod array;

pub use string::{to_hex_string_bytes, str_to_u64};
pub use math::{clamp, div_ceil, sqrt::sqrt};

pub fn array_concat<T, let L1: u32, let L2: u32>(a: [T; L1], b: [T; L2]) -> [T; L1 + L2] {
let mut result = [a[0]; L1 + L2];
for i in 0..L1 {
result[i] = a[i];
}
for i in 0..L2 {
result[i + L1] = b[i];
}
result
trait ArrayExtensions<T, let N: u32> {
fn concat<let M: u32>(self, other: [T; M]) -> [T; N + M];
fn pad_start<let M: u32>(self, pad_value: T) -> [T; M];
fn pad_end<let M: u32>(self, pad_value: T) -> [T; M];
}

mod array_concat_tests {
use crate::array_concat;

#[test]
fn test_simple() {
assert(array_concat([1, 2, 3], [4, 5]) == [1, 2, 3, 4, 5]);
}
#[deprecated]
pub fn array_concat<T, let L1: u32, let L2: u32>(a: [T; L1], b: [T; L2]) -> [T; L1 + L2] {
a.concat(b)
}
4 changes: 2 additions & 2 deletions src/string.nr
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn some_test() {
let outer_hash = 0x0d67824fead966192029093a3aa5c719f2b80262c4f14a5c97c5d70e4b27f2bf;
let hex_challenge: [u8; 64] = to_hex_string_bytes(outer_hash);
let header_prefix: [u8; 26] = "subject:Re: Tx request: 0x".as_bytes();
let header: [u8; 90] = crate::array_concat(header_prefix, hex_challenge);
let header: [u8; 90] = header_prefix.concat(hex_challenge);
assert(
header == [
115, 117, 98, 106, 101, 99, 116, 58, 82, 101, 58, 32, 84, 120, 32, 114, 101, 113, 117, 101, 115, 116, 58, 32, 48, 120, 48, 100, 54, 55, 56, 50, 52, 102, 101, 97, 100, 57, 54, 54, 49, 57, 50, 48, 50, 57, 48, 57, 51, 97, 51, 97, 97, 53, 99, 55, 49, 57, 102, 50, 98, 56, 48, 50, 54, 50, 99, 52, 102, 49, 52, 97, 53, 99, 57, 55, 99, 53, 100, 55, 48, 101, 52, 98, 50, 55, 102, 50, 98, 102
Expand All @@ -49,6 +49,6 @@ fn some_test() {
#[test]
fn test_str_to_u64() {
let s = "13378584420".as_bytes();
assert(str_to_u64(crate::array_concat(s, [0; 9])) == 13378584420);
assert(str_to_u64(s.concat([0; 9])) == 13378584420);
assert(str_to_u64("02345678912345678912") == 02345678912345678912);
}

0 comments on commit f51557a

Please sign in to comment.