From a838a55561e226e8b477d22864b8c986c1ab55e5 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Thu, 19 Oct 2023 14:36:05 -0500 Subject: [PATCH] added: `slice.group_by` --- core/container/slice.onyx | 27 +++++++++++++++++++++++++++ core/std.onyx | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/core/container/slice.onyx b/core/container/slice.onyx index 84947104..41d12081 100644 --- a/core/container/slice.onyx +++ b/core/container/slice.onyx @@ -620,6 +620,33 @@ chunks :: (arr: [] $T, width: i32) -> Iterator([] T) { } +#doc """ +""" +group_by :: #match #local {} + +#overload +group_by :: macro (arr: [] $T, comp: (T, T) -> bool, allocator := context.allocator) -> [..] [] T { + return #this_package.group_by(arr, [a, b](comp(a, b)), allocator); +} + +#overload +group_by :: macro (arr_: [] $T, comp: Code, allocator := context.allocator) -> [..] [] T { + out := make([..] [] T, allocator); + start := 0; + arr := arr_; + for i: 1 .. arr.count { + if !#unquote comp(arr[start], arr[i]) { + out << arr[start .. i]; + start = i; + } + } + + out << arr[start .. arr.count]; + return out; +} + + + #local HasEquals :: interface (t: $T) { { t == t } -> bool; } diff --git a/core/std.onyx b/core/std.onyx index 75d74d65..ab028a03 100644 --- a/core/std.onyx +++ b/core/std.onyx @@ -54,11 +54,11 @@ use runtime #load "./test/testing" #load "./time/time" +#load "./time/date" #load "./misc/arg_parse" #load "./misc/method_ops" -#load "./time/date" #load "./encoding/base64" #load "./encoding/hex" #load "./encoding/utf8" -- 2.25.1