From: Brendan Hansen Date: Mon, 19 Jun 2023 14:39:22 +0000 (-0500) Subject: added: `iter.find` and `slice.equal` X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=cc9817b0ea3a9b547ae4e5b675d8da813d674a73;p=onyx.git added: `iter.find` and `slice.equal` --- diff --git a/CHANGELOG b/CHANGELOG index 080cbf2f..bece8908 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -11,17 +11,19 @@ Additions: - Use `runtime.info.get_stack_trace()` to get the current stack trace. - Used in assertions and heap allocator for better error reporting - `Optional.with` for running a block of code with the value in an Optional, if one is present. -- `iter.flatten` - `-Dvariable=value` command line option to add symbols to the `runtime.vars` package. - `--no-type-info` command line option to omit type information from the binary. -- `core.encoding.hex` - - Quickly convert a byte array to and from its hex equivalent. - `Allocator.move`. Moves a value into an allocator, returning a pointer to it. - This is a copy operation (and might be renamed later) +- `core.encoding.hex` package + - Quickly convert a byte array to and from its hex equivalent. - `os.path_clean` - `os.path_directory` - `os.path_extension` - `os.path_split` +- `slice.equal` +- `iter.find` +- `iter.flatten` Removals: - Remove old syntax for quoted blocks, `#quote` and `#()`. diff --git a/core/container/iter.onyx b/core/container/iter.onyx index b39b5e9a..ca4f8252 100644 --- a/core/container/iter.onyx +++ b/core/container/iter.onyx @@ -28,6 +28,7 @@ use core.intrinsics.types {type_is_struct} flatten :: flatten; enumerate :: enumerate; + find :: find; fold :: fold; count :: count; some :: some; @@ -717,6 +718,24 @@ as_iter :: (r: range) => generator( // Iterator reducing // +find :: #match #local {} + +#overload +find :: macro (it: $T/Iterable, predicate: $F) => + #this_package.find(#this_package.as_iter(it), predicate); + +#overload +find :: (it: Iterator($T), predicate: (T) -> bool) -> ? T { + for v: it { + if predicate(v) { + return v; + } + } + + return .{}; +} + + #doc """ Incremently calls `combine` on the yielded value and the accumulated value, producing a new accumulated value. Returns diff --git a/core/container/slice.onyx b/core/container/slice.onyx index a54385f2..9c94b64a 100644 --- a/core/container/slice.onyx +++ b/core/container/slice.onyx @@ -620,6 +620,21 @@ chunks :: (arr: [] $T, width: i32) -> Iterator([] T) { } +#local HasEquals :: interface (t: $T) { + { t == t } -> bool; +} + +equal :: (arr1: [] $T/HasEquals, arr2: [] T) -> bool { + if arr1.count != arr2.count do return false; + + for i: arr1.count { + if !(arr1[i] == arr2[i]) do return false; + } + + return true; +} + + #local fold_idx_elem :: (arr: [] $T, $cmp: Code) -> (i32, T) { idx := 0; elem := arr[0];