added core array functions
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 2 Sep 2020 02:14:22 +0000 (21:14 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 2 Sep 2020 02:14:22 +0000 (21:14 -0500)
core/array.onyx
docs/plan
progs/poly_test.onyx

index 7717c0584befa792375931b526b0080434a0e6fd..9f0fb12278c3995130b01dd6ba088d4e1b8709fe 100644 (file)
@@ -114,3 +114,13 @@ array_sort :: proc (arr: ^[..] $T, cmp: proc (T, T) -> i32) {
         arr.data[j + 1] = x;
     }
 }
+
+array_fold :: proc (arr: ^[..] $T, init: $R, f: proc (T, R) -> R) -> R {
+    val := init;
+    for i: 0, arr.count do val = f(arr.data[i], val);
+    return val;
+}
+
+array_map :: proc (arr: ^[..] $T, f: proc (T) -> T) {
+    for i: 0, arr.count do arr.data[i] = f(arr.data[i]);
+}
\ No newline at end of file
index 4d0d53ced3139363a71272be38995cbfb08d5af5..4e947f7444d0453eb7701943989b844d5f38f832 100644 (file)
--- a/docs/plan
+++ b/docs/plan
@@ -273,7 +273,7 @@ HOW:
 
         [ ] All code paths return correct value
 
-        [ ] Arrays need to be much better
+        [X] Arrays need to be much better
             - Currently, they are basically just a pointer.
             - The length should be stored with the pointer
             - Dynamic resizing?
index eca29822170c324581f1482eca6ec043ef0805ce..335d0052c4f507876b3e03c5675cb3c79ac958a3 100644 (file)
@@ -89,6 +89,9 @@ main :: proc (args: [] cstring) {
     print_arr_details(^s.a);
     print_arr_details(^s.b);
 
+    print("Array A sum: ");
+    print(array_fold(^s.a, 0, proc (x: i32, acc: i32) -> i32 do return x + acc;));
+    print("\n\n");
 
     map : PtrMap;
     ptrmap_init(^map);