added: `iter.find` and `slice.equal`
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 19 Jun 2023 14:39:22 +0000 (09:39 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 19 Jun 2023 14:39:22 +0000 (09:39 -0500)
CHANGELOG
core/container/iter.onyx
core/container/slice.onyx

index 080cbf2f18090c436849c6c0df205b5eb4cb39ce..bece8908163a1d8fa9ae90133717380054dcf0b9 100644 (file)
--- 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 `#()`.
index b39b5e9a128c79385a808064a26573f41d5f8649..ca4f8252861fa6b36196ebb2c6f70da78a1e1241 100644 (file)
@@ -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
index a54385f20ed47a014c2f7e58b522554053dfae30..9c94b64a35e3c4c9724822f63dadbc68212ce961 100644 (file)
@@ -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];