From: Brendan Hansen Date: Wed, 2 Sep 2020 02:14:22 +0000 (-0500) Subject: added core array functions X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=1effc85dc2f91952c9009132c43eb57298d2fb68;p=onyx.git added core array functions --- diff --git a/core/array.onyx b/core/array.onyx index 7717c058..9f0fb122 100644 --- a/core/array.onyx +++ b/core/array.onyx @@ -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 diff --git a/docs/plan b/docs/plan index 4d0d53ce..4e947f74 100644 --- 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? diff --git a/progs/poly_test.onyx b/progs/poly_test.onyx index eca29822..335d0052 100644 --- a/progs/poly_test.onyx +++ b/progs/poly_test.onyx @@ -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);