added: `iter.counter`
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 8 Dec 2023 04:32:29 +0000 (22:32 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 8 Dec 2023 04:32:29 +0000 (22:32 -0600)
core/container/iter.onyx

index e37de430441b309d67d6ad7af4da5a3643377587..204c9089dc4f9f9f68aa893107e892d3f28af42f 100644 (file)
@@ -93,6 +93,25 @@ empty :: ($T: type_expr) -> Iterator(T) {
     };
 }
 
+
+#doc """
+    Helper function to create an infinite counting iterator.
+
+    Use `start` to configure the starting value.
+
+    Use `type` to configure the type used for the iterator.
+"""
+counter :: (start: type = 0, $type: type_expr = i32) -> Iterator(type) {
+    return generator(
+        &.{ i = start },
+        ctx => {
+            defer ctx.i += 1;
+            return ctx.i, true;
+        }
+    );
+}
+
+
 //
 // Implicit iterator creation
 //