Add slice.min and add slice.max

This commit is contained in:
gingerBill
2021-07-08 11:23:07 +01:00
parent f7413ca974
commit 7acbf8b7b9

View File

@@ -1,10 +1,12 @@
package slice
import "intrinsics"
import "builtin"
import "core:math/bits"
import "core:mem"
_ :: intrinsics;
_ :: builtin;
_ :: bits;
_ :: mem;
@@ -292,6 +294,28 @@ filter :: proc(s: $S/[]$U, f: proc(U) -> bool, allocator := context.allocator) -
min :: proc(s: $S/[]$T) -> (res: T, ok: bool) where intrinsics.type_is_ordered(T) #optional_ok {
if len(s) != 0 {
res = s[0];
ok = true;
for v in s[1:] {
res = min(res, v);
}
}
return;
}
max :: proc(s: $S/[]$T) -> (res: T, ok: bool) where intrinsics.type_is_ordered(T) #optional_ok {
if len(s) != 0 {
res = s[0];
ok = true;
for v in s[1:] {
res = max(res, v);
}
}
return;
}
dot_product :: proc(a, b: $S/[]$T) -> T
where intrinsics.type_is_numeric(T) {
if len(a) != len(b) {