Skip to content
This repository has been archived by the owner on Apr 20, 2023. It is now read-only.

Commit

Permalink
Adding more of Jake's suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
signorecello committed Mar 9, 2023
1 parent 918936f commit eeea57e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
20 changes: 10 additions & 10 deletions src/standard_library/array_methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn main() {

## sort

Returns a new sorted array with a built-in function. Original array remains untouched.
Returns a new sorted array. Original array remains untouched. Notice that this function will only work for arrays of fields or integers, not for any arbitrary type. This is because the sorting logic it uses internally is optimized specifically for these values. If you need a sort function to sort any type, you should use the below function `sort_via`.

```rust
fn sort<T, N>(_array: [T; N]) -> Self
Expand All @@ -40,7 +40,7 @@ fn main() {

## sort_via

Sorts the array with a custom sorting function
Sorts the array with a custom comparison function

```rust
fn sort_via<T, N>(mut a: [T; N], ordering: fn(T, T) -> bool) -> Self
Expand All @@ -52,7 +52,7 @@ example
fn main() {
let arr = [42, 32]
let sorted_ascending = arr.sort_via(|a, b| a < b);
constrain t == [32, 42]; // verifies
constrain sorted_ascending == [32, 42]; // verifies

let sorted_descending = arr.sort_via(|a, b| a > b);
constrain t == [32, 42]; // does not verify
Expand All @@ -63,7 +63,11 @@ fn main() {

Applies a function to each element of the array, returning the final accumulated value. The first parameter is the initial value.

One should know ihis is a left fold, meaning that the function is always applied to the leftiest element. So for a given call the expected result would be equivalent to:
```rust
fn fold<U>(mut accumulator: U, f: fn(U, T) -> U) -> U
```

This is a left fold, so the given function will be applied to the accumulator and first element of the array, then the second, and so on. For a given call the expected result would be equivalent to:

```rust
let a1 = [1];
Expand All @@ -76,10 +80,6 @@ a2.fold(10, f) //=> f(f(10, 1), 2)
a3.fold(10, f) //=> f(f(f(10, 1), 2), 3)
```

```rust
fn fold<U>(mut accumulator: U, f: fn(U, T) -> U) -> U
```

example:

``` rust
Expand Down Expand Up @@ -124,7 +124,7 @@ example:
fn main() {
let arr = [2,2,2,2,2]
let all = arr.all(|a| a == 2);
constrain all == true;
constrain all;
}
```

Expand All @@ -142,7 +142,7 @@ example:
fn main() {
let arr = [2,2,2,2,5]
let any = arr.any(|a| a == 5);
constrain any == true;
constrain any;
}

```
Expand Down
26 changes: 13 additions & 13 deletions src/standard_library/field_methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ fn to_le_bits<N>(_x : Field, _bit_size: u32) -> [u1; N]
example:

``` rust
fn main(field : pub Field) {
let t = field.to_le_bits(32);
constrain t == t;
fn main() {
const field = 2
let bits = field.to_le_bits(32);
}
```

Expand All @@ -31,9 +31,9 @@ fn to_le_bytes(_x : Field, byte_size: u32) -> [u8]
example:

```rust
fn main(field : pub Field) {
let t = field.to_le_bytes(4);
constrain t == t;
fn main() {
const field = 2
let bytes = field.to_le_bytes(4);
}
```

Expand All @@ -48,9 +48,9 @@ fn to_radix(_x : Field, _radix: u32, _result_len: u32) -> [u8]
example:

```rust
fn main(field : pub Field) {
let t = field.to_radix(256, 4);
constrain t == t;
fn main() {
const field = 2
let radix = field.to_radix(256, 4);
}
```

Expand All @@ -65,9 +65,9 @@ fn pow_32(self, exponent: Field) -> Field
example:

```rust
// field = 2
fn main(field : pub Field) {
let t = field.pow_32(4);
constrain t == 16;
fn main() {
const field = 2
let pow = field.pow_32(4);
constrain pow == 16;
}
```

0 comments on commit eeea57e

Please sign in to comment.