Add slice/scanner proc

This commit is contained in:
Andrea Piseri
2021-12-23 12:49:40 +01:00
parent eec61c3f6f
commit 5d80e24224

View File

@@ -304,6 +304,27 @@ filter :: proc(s: $S/[]$U, f: proc(U) -> bool, allocator := context.allocator) -
return r[:]
}
scanner :: proc (s: $S/[]$U, initializer: $V, f: proc(V, U)->V, allocator := context.allocator) -> []V {
if len(s) == 0 { return {} }
p := as_ptr(s)
res := make([]V, len(s), allocator)
q := as_ptr(res)
l := len(res)
r := initializer
for l > 0 {
r = f(r, p^)
q^ = r
p = intrinsics.ptr_offset(p, 1)
q = intrinsics.ptr_offset(q, 1)
l -= 1
}
return res
}
min :: proc(s: $S/[]$T) -> (res: T, ok: bool) where intrinsics.type_is_ordered(T) #optional_ok {