Browse Source

Merge pull request #1389 from ap29600/slice_scanner

Add slice/scanner proc
gingerBill 3 years ago
parent
commit
4f77151ebc
1 changed files with 17 additions and 0 deletions
  1. 17 0
      core/slice/slice.odin

+ 17 - 0
core/slice/slice.odin

@@ -304,6 +304,23 @@ 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 {} }
+
+  res := make([]V, len(s), allocator)
+  p := as_ptr(s)
+  q := as_ptr(res)
+  r := initializer
+
+  for l := len(s); l > 0; l -= 1 {
+    r = f(r, p[0])
+    q[0] = r
+    p = p[1:]
+    q = q[1:]
+  }
+
+  return res
+}
 
 
 min :: proc(s: $S/[]$T) -> (res: T, ok: bool) where intrinsics.type_is_ordered(T) #optional_ok {