From: Brendan Hansen Date: Thu, 17 Mar 2022 18:55:12 +0000 (-0500) Subject: added variant of 'array.contains' and 'array.reverse' X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=ee19bb8d4fe1acff4608c3222209848b61ecd1e4;p=onyx.git added variant of 'array.contains' and 'array.reverse' --- diff --git a/core/container/array.onyx b/core/container/array.onyx index adad8989..745b078a 100644 --- a/core/container/array.onyx +++ b/core/container/array.onyx @@ -245,10 +245,17 @@ set :: (arr: [] $T, idx: i32, value: T) { arr.data[idx] = value; } -// Uses '==' to compare for equality. -contains :: (arr: [] $T, x: T) -> bool { - for it: arr do if it == x do return true; - return false; +contains :: #match { + // Uses '==' to compare for equality. + (arr: [] $T, x: T) -> bool { + for it: arr do if it == x do return true; + return false; + }, + + macro (arr: [] $T, $cmp: Code) -> bool { + for it: arr do if #insert cmp do return true; + return false; + } } // Uses '+' to sum. @@ -271,6 +278,12 @@ average :: (arr: [] $T) -> T { return sum / cast(T) arr.count; } +reverse :: (arr: [] $T) { + for i: arr.count / 2 { + arr[i], arr[arr.count - 1 - i] = arr[arr.count - 1 - i], arr[i]; + } +} + // // Simple insertion sort // cmp should return >0 if left > right