Skip to content

Commit 83e56c4

Browse files
authored
fix: apply proper types to function arguments (#17)
1 parent 0b33d71 commit 83e56c4

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/lib.nr

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ where
6161
**/
6262
pub fn sort_extended<T, let N: u32>(
6363
input: [T; N],
64-
sortfn: fn(T, T) -> bool,
64+
sortfn: unconstrained fn(T, T) -> bool,
6565
sortfn_assert: fn(T, T) -> (),
6666
) -> [T; N]
6767
where
@@ -82,7 +82,7 @@ pub struct SortResult<T, let N: u32> {
8282
}
8383
pub fn sort_advanced<T, let N: u32>(
8484
input: [T; N],
85-
sortfn: fn(T, T) -> bool,
85+
sortfn: unconstrained fn(T, T) -> bool,
8686
sortfn_assert: fn(T, T) -> (),
8787
) -> SortResult<T, N>
8888
where
@@ -107,6 +107,10 @@ mod test {
107107
a <= b
108108
}
109109

110+
unconstrained fn __sort_u32(a: u32, b: u32) -> bool {
111+
a <= b
112+
}
113+
110114
// unconditional_lt will cost fewer constraints than the `<=` operator
111115
// as we do not need to constrain the case where `a > b`, and assign a boolean variable to the result
112116
fn unconditional_lt(_a: u32, _b: u32) {
@@ -141,7 +145,7 @@ mod test {
141145
fn test_sort_extended() {
142146
let mut arr: [u32; 7] = [3, 6, 8, 10, 1, 2, 1];
143147

144-
let sorted = sort_extended(arr, sort_u32, unconditional_lt);
148+
let sorted = sort_extended(arr, __sort_u32, unconditional_lt);
145149

146150
let expected: [u32; 7] = [1, 1, 2, 3, 6, 8, 10];
147151
assert(sorted == expected);

src/quicksort/quicksort_explicit.nr

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ unconstrained fn partition<T, let N: u32>(
1414
arr: &mut [T; N],
1515
low: u32,
1616
high: u32,
17-
sortfn: fn(T, T) -> bool,
17+
sortfn: unconstrained fn(T, T) -> bool,
1818
) -> u32 {
1919
let pivot = high;
2020
let mut i = low;
@@ -32,7 +32,7 @@ unconstrained fn quicksort_recursive<T, let N: u32>(
3232
arr: &mut [T; N],
3333
low: u32,
3434
high: u32,
35-
sortfn: fn(T, T) -> bool,
35+
sortfn: unconstrained fn(T, T) -> bool,
3636
) {
3737
if low < high {
3838
let pivot_index = partition(arr, low, high, sortfn);
@@ -43,7 +43,10 @@ unconstrained fn quicksort_recursive<T, let N: u32>(
4343
}
4444
}
4545

46-
pub unconstrained fn quicksort<T, let N: u32>(_arr: [T; N], sortfn: fn(T, T) -> bool) -> [T; N] {
46+
pub unconstrained fn quicksort<T, let N: u32>(
47+
_arr: [T; N],
48+
sortfn: unconstrained fn(T, T) -> bool,
49+
) -> [T; N] {
4750
let mut arr: [T; N] = _arr;
4851
if arr.len() <= 1 {} else {
4952
quicksort_recursive(&mut arr, 0, arr.len() - 1, sortfn);

0 commit comments

Comments
 (0)