arrays: switch panics to optionals (#11497)

pull/11510/head
JalonSolov 2021-09-15 08:17:55 -04:00 committed by GitHub
parent ef690dc06b
commit 7145461cc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 34 deletions

View File

@ -8,10 +8,10 @@ module arrays
// - window - get snapshots of the window of the given size sliding along array with the given step, where each snapshot is an array // - window - get snapshots of the window of the given size sliding along array with the given step, where each snapshot is an array
// - zip - concat two arrays into one map // - zip - concat two arrays into one map
// min returns the minimum // min returns the minimum value in the array
pub fn min<T>(a []T) T { pub fn min<T>(a []T) ?T {
if a.len == 0 { if a.len == 0 {
panic('.min called on an empty array') return error('.min called on an empty array')
} }
mut val := a[0] mut val := a[0]
for e in a { for e in a {
@ -22,10 +22,10 @@ pub fn min<T>(a []T) T {
return val return val
} }
// max returns the maximum // max returns the maximum the maximum value in the array
pub fn max<T>(a []T) T { pub fn max<T>(a []T) ?T {
if a.len == 0 { if a.len == 0 {
panic('.max called on an empty array') return error('.max called on an empty array')
} }
mut val := a[0] mut val := a[0]
for e in a { for e in a {
@ -36,10 +36,10 @@ pub fn max<T>(a []T) T {
return val return val
} }
// idx_min returns the index of the first minimum // idx_min returns the index of the minimum value in the array
pub fn idx_min<T>(a []T) int { pub fn idx_min<T>(a []T) ?int {
if a.len == 0 { if a.len == 0 {
panic('.idx_min called on an empty array') return error('.idx_min called on an empty array')
} }
mut idx := 0 mut idx := 0
mut val := a[0] mut val := a[0]
@ -52,10 +52,10 @@ pub fn idx_min<T>(a []T) int {
return idx return idx
} }
// idx_max returns the index of the first maximum // idx_max returns the index of the maximum value in the array
pub fn idx_max<T>(a []T) int { pub fn idx_max<T>(a []T) ?int {
if a.len == 0 { if a.len == 0 {
panic('.idx_max called on an empty array') return error('.idx_max called on an empty array')
} }
mut idx := 0 mut idx := 0
mut val := a[0] mut val := a[0]

View File

@ -1,45 +1,63 @@
module arrays module arrays
fn test_min() { fn test_min() ? {
a := [8, 2, 6, 4] a := [8, 2, 6, 4]
assert min<int>(a) == 2 mut ri := min(a) ?
assert min<int>(a[2..]) == 4 assert ri == 2
ri = min(a[2..]) ?
assert ri == 4
b := [f32(5.1), 3.1, 1.1, 9.1] b := [f32(5.1), 3.1, 1.1, 9.1]
assert min<f32>(b) == f32(1.1) mut rf := min(b) ?
assert min<f32>(b[..2]) == f32(3.1) assert rf == f32(1.1)
rf = min(b[..2]) ?
assert rf == f32(3.1)
c := [byte(4), 9, 3, 1] c := [byte(4), 9, 3, 1]
assert min<byte>(c) == byte(1) mut rb := min(c) ?
assert min<byte>(c[..3]) == byte(3) assert rb == byte(1)
rb = min(c[..3]) ?
assert rb == byte(3)
} }
fn test_max() { fn test_max() ? {
a := [8, 2, 6, 4] a := [8, 2, 6, 4]
assert max<int>(a) == 8 mut ri := max(a) ?
assert max<int>(a[1..]) == 6 assert ri == 8
ri = max(a[1..]) ?
assert ri == 6
b := [f32(5.1), 3.1, 1.1, 9.1] b := [f32(5.1), 3.1, 1.1, 9.1]
assert max<f32>(b) == f32(9.1) mut rf := max(b) ?
assert max<f32>(b[..3]) == f32(5.1) assert rf == f32(9.1)
rf = max(b[..3]) ?
assert rf == f32(5.1)
c := [byte(4), 9, 3, 1] c := [byte(4), 9, 3, 1]
assert max<byte>(c) == byte(9) mut rb := max(c) ?
assert max<byte>(c[2..]) == byte(3) assert rb == byte(9)
rb = max(c[2..]) ?
assert rb == byte(3)
} }
fn test_idx_min() { fn test_idx_min() ? {
a := [8, 2, 6, 4] a := [8, 2, 6, 4]
assert idx_min<int>(a) == 1 ri := idx_min(a) ?
assert ri == 1
b := [f32(5.1), 3.1, 1.1, 9.1] b := [f32(5.1), 3.1, 1.1, 9.1]
assert idx_min<f32>(b) == 2 rf := idx_min(b) ?
assert rf == 2
c := [byte(4), 9, 3, 1] c := [byte(4), 9, 3, 1]
assert idx_min<byte>(c) == 3 rb := idx_min(c) ?
assert rb == 3
} }
fn test_idx_max() { fn test_idx_max() ? {
a := [8, 2, 6, 4] a := [8, 2, 6, 4]
assert idx_max<int>(a) == 0 ri := idx_max(a) ?
assert ri == 0
b := [f32(5.1), 3.1, 1.1, 9.1] b := [f32(5.1), 3.1, 1.1, 9.1]
assert idx_max<f32>(b) == 3 rf := idx_max(b) ?
assert rf == 3
c := [byte(4), 9, 3, 1] c := [byte(4), 9, 3, 1]
assert idx_max<byte>(c) == 1 rb := idx_max(c) ?
assert rb == 1
} }
fn test_merge() { fn test_merge() {