mirror of
https://github.com/Ed94/Odin.git
synced 2026-07-17 00:11:25 -07:00
Add more procedures to package slice
This commit is contained in:
@@ -252,3 +252,44 @@ get_ptr :: proc(array: $T/[]$E, index: int) -> (value: ^E, ok: bool) {
|
||||
as_ptr :: proc(array: $T/[]$E) -> ^E {
|
||||
return raw_data(array);
|
||||
}
|
||||
|
||||
|
||||
mapper :: proc(s: $S/[]$U, f: proc(U) -> $V, allocator := context.allocator) -> []V {
|
||||
r := make([]V, len(s), allocator);
|
||||
for v, i in s {
|
||||
r[i] = f(v);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
reduce :: proc(s: $S/[]$U, initializer: $V, f: proc(V, U) -> V) -> V {
|
||||
r := initializer;
|
||||
for v in s {
|
||||
r = f(r, v);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
filter :: proc(s: $S/[]$U, f: proc(U) -> bool, allocator := context.allocator) -> S {
|
||||
r := make([dynamic]S, 0, 0, allocator);
|
||||
for v in s {
|
||||
if f(v) {
|
||||
append(&r, v);
|
||||
}
|
||||
}
|
||||
return r[:];
|
||||
}
|
||||
|
||||
|
||||
|
||||
dot_product :: proc(a, b: $S/[]$T) -> T
|
||||
where intrinsics.type_is_numeric(T) {
|
||||
if len(a) != len(b) {
|
||||
panic("slice.dot_product: slices of unequal length");
|
||||
}
|
||||
r: T;
|
||||
#no_bounds_check for _, i in a {
|
||||
r += a[i] * b[i];
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user